过滤表数据

时间:2019-07-05 03:24:35

标签: javascript angular typescript filter datatable

我想根据几个条件过滤表格。.下面是示例图像 sample

我尝试过的代码

    this.reportData.filter(it => {
        if (
            it.startTimeFilter.includes(this.startdatefilter) &&
            it.endTimeFilter.includes(this.enddatefilter) &&
            it.status.toLowerCase().includes(this.status)
        ) {
            this.filteredData.push(it);
        }
    });

2 个答案:

答案 0 :(得分:1)

好吧,由于我没有完整的代码,因此我可以给您一些提示以实现此目标。确保this.reportData从未更改,因为我们需要所有数据进行过滤


 applyFiltering(){
    this.dataToShowOnUI = getFilteredData();
 }

 getFilteredData(): any[]{
    let filteredData = JSON.parse(JSON.stringify(this.reportData));
    if(this.startdatefilter && this.enddatefilter){
      filteredData = filteredData.filter(it => 
            it.startTimeFilter.includes(this.startdatefilter) &&
            it.endTimeFilter.includes(this.enddatefilter) 
      );
    }
    if(this.status){
     filteredData = filteredData.filter(data => data.status.toLowerCase().includes(this.status))
    }
    if(this.operatingSystem){
        filteredData = filteredData.filter(data => data.operatingSystem.toLowerCase().includes(this.operatingSystem))
    }
    // and so on ...
    return filteredData;
 }

答案 1 :(得分:0)

我假设this.reportDatathis.filteredData是数组。然后,使用filter方法的正确方法如下:

this.filteredData = this.reportData.filter(it => 
    it.startTimeFilter.includes(this.startdatefilter) &&
    it.endTimeFilter.includes(this.enddatefilter) &&
    it.status.toLowerCase().includes(this.status)
);

基本上,filter的参数应该是一个返回布尔值的函数(该值指示元素是否应作为结果保留),并返回过滤后的新数组而不修改给定数组。