MatTable上的多个过滤器

kal*_*l93 5 typescript angular-material angular

我一直在尝试应用多列过滤,即列标题中的文本输入将仅对列的内容进行过滤。到目前为止,我已经能够通过覆盖使其工作filterPredicateMatTableDataSource但是一旦我覆盖了默认过滤,即跨列不再有效。

export class TableFilteringExample implements OnInit
{
  displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
  dataSource = new MatTableDataSource(ELEMENT_DATA);

  positionFilter = new FormControl();
  nameFilter = new FormControl();

  filteredValues =
  {
    position: '',
    name: '',
    weight: '',
    symbol: ''
  };

  ngOnInit()
  {
    this.positionFilter.valueChanges.subscribe((positionFilterValue) =>
    {
      this.filteredValues['position'] = positionFilterValue;
      this.dataSource.filter = JSON.stringify(this.filteredValues);
    });

    this.nameFilter.valueChanges.subscribe((nameFilterValue) =>
    {
      this.filteredValues['name'] = nameFilterValue;
      this.dataSource.filter = JSON.stringify(this.filteredValues);
    });

    this.dataSource.filterPredicate = this.customFilterPredicate();
  }

  applyFilter(filterValue: string)
  {
    this.dataSource.filter = filterValue.trim().toLowerCase();
    this.dataSource.filter = filterValue;
  }

  customFilterPredicate()
  {
    const myFilterPredicate = function(data: PeriodicElement, filter: string): boolean
    {
      let searchString = JSON.parse(filter);

      return data.position.toString().trim().indexOf(searchString.position) !== -1 && data.name.toString().trim().toLowerCase().indexOf(searchString.name)!== -1;
    }

    return myFilterPredicate;
  }
}
Run Code Online (Sandbox Code Playgroud)

我要寻找的是一旦应用了列过滤器,默认过滤器应该更新现有的过滤条件并返回进一步的过滤数据。

StackBlitz

Ran*_*nan 13

我想,你只是忘了打电话toLowerCase()searchString.name

data.name.toString()。trim()。toLowerCase()。indexOf(searchString.name.toLowerCase())!== -1;


编辑:一种方法是在Component类中创建一个全局过滤器字段。

globalFilter = '';
Run Code Online (Sandbox Code Playgroud)
<mat-form-field>
  <input matInput [ngModel]="globalFilter" (ngModelChange)="applyFilter($event)" placeholder="Filter">
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
applyFilter(filter) {
    this.globalFilter = filter;
    this.dataSource.filter = JSON.stringify(this.filteredValues);
}
Run Code Online (Sandbox Code Playgroud)

然后尝试在其他字段之前先使用全局过滤器进行过滤。

  customFilterPredicate() {
    const myFilterPredicate = (data: PeriodicElement, filter: string): boolean => {
      var globalMatch = !this.globalFilter;

      if (this.globalFilter) {
        // search all text fields
        globalMatch = data.name.toString().trim().toLowerCase().indexOf(this.globalFilter.toLowerCase()) !== -1;
      }

      if (!globalMatch) {
        return;
      }

      let searchString = JSON.parse(filter);
      return data.position.toString().trim().indexOf(searchString.position) !== -1 &&
        data.name.toString().trim().toLowerCase().indexOf(searchString.name.toLowerCase()) !== -1;
    }
    return myFilterPredicate;
  }
Run Code Online (Sandbox Code Playgroud)

这是工作中的应用程序:https : //stackblitz.com/edit/angular-hbakxo-5jeaic