【问题标题】:How to filter data with dynamic created checkboxs in Angular 2如何在Angular 2中使用动态创建的复选框过滤数据
【发布时间】:2017-08-08 05:31:57
【问题描述】:

我创建了一个复选框列表,我希望当我点击一个或多个复选框时,结果应该根据选中的复选框进行过滤。 任何人都可以帮助我如何存档这个。我使用 Angular 2。

【问题讨论】:

    标签: angular


    【解决方案1】:

    你可以应用如下逻辑,可能你需要修改一些东西。

    public selectedItems: any = [];
    

    将数据源分配给复选框后:(添加 IsSelected 字段,以便我们可以在需要时对数据应用过滤器)

    this.dataSource.forEach((row: any) => {
        row.IsSelected = false;
    });
    

    CkeckBox 点击事件:

    public OnCheckBoxClicked(value: any): any {
        this.selectedItems = [];       
        setTimeout(() => {
            let selectedRows = this.dataSource.filter((row: any) => row.IsSelected == true);
            if (selectedRows != null && selectedRows.length > 0) {
                this.selectedItems = selectedRows;                
            }
            console.log(this.selectedItems);
        }, 300);
    }       
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-27
      • 2016-06-13
      • 2019-12-11
      • 2019-09-15
      • 1970-01-01
      • 2014-09-14
      • 2017-02-03
      • 1970-01-01
      相关资源
      最近更新 更多