Filtering specific column in Angular Material table in angular 5

前端 未结 8 794
太阳男子
太阳男子 2020-12-01 07:11

In Angular material official website it is mentioned that filterPredicate: ((data: T, filter: string) => boolean) will filter data based on specific field. But don\'t gettin

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 07:42

    You can get to filter by a dynamic column, as in no hardcoded column name, doing the following:

    // On input focus: setup filterPredicate to only filter by input column
    setupFilter(column: string) {
      this.dataSource.filterPredicate = (d: TableDataSourceType, filter: string) => {
        const textToSearch = d[column] && d[column].toLowerCase() || '';
        return textToSearch.indexOf(filter) !== -1;
      };
    }
    
    applyFilter(filterValue: string) {
      this.dataSource.filter = filterValue.trim().toLowerCase();
    }
    

    In the template you can have something like this:

    
      
        
      
    
    

    Or a more complex example, dynamically create a header row with per-column filtering:

    
         
           

    Be aware that you cannot have multiple header rows with the same keys, so this will not work:

    
    
    

提交回复
热议问题