Angular6 如何在特定列的角度材质数据表中进行过滤

Angular6 如何在特定列的角度材质数据表中进行过滤,angular6,angular-material-6,Angular6,Angular Material 6,我正在尝试角材料数据表 正如我们所知,默认情况下对每一行进行过滤 如果我想过滤特定于列的内容,那么我应该怎么做 我是否必须编写获取所有记录的方法,然后对其进行迭代并比较特定列 组件技术 component.html 您应该使用的filterPredicate属性 初始化this.dataSource后,定义自定义filterPredicate函数,如下所示 this.dataSource = new MatTableDataSource(results); this.dataSource.s

我正在尝试角材料数据表

正如我们所知,默认情况下对每一行进行过滤

如果我想过滤特定于列的内容,那么我应该怎么做

我是否必须编写获取所有记录的方法,然后对其进行迭代并比较特定列

组件技术


component.html



您应该使用的filterPredicate属性

初始化this.dataSource后,定义自定义filterPredicate函数,如下所示

this.dataSource = new MatTableDataSource(results);
this.dataSource.sort = this.sort;
this.dataSource.filterPredicate = function(data: any, filterValue: string) {
  return data.specificColumn /** replace this with the column name you want to filter */
    .trim()
    .toLocaleLowerCase().indexOf(filterValue.trim().toLocaleLowerCase()) >= 0;
};
检查教程和工作状态


这将完全匹配…如果我想匹配任何用户名的一部分。。。假设mohan是用户名,如果我输入moh……这并不表示我已经更新了答案以包含部分匹配。请看一看。查看本教程了解材质列过滤器
<mat-form-field class="search-form-field">
      <input matInput [(ngModel)]="searchKey" placeholder="search by userName" (keyup)="applyFilter()">
    </mat-form-field>
this.dataSource = new MatTableDataSource(results);
this.dataSource.sort = this.sort;
this.dataSource.filterPredicate = function(data: any, filterValue: string) {
  return data.specificColumn /** replace this with the column name you want to filter */
    .trim()
    .toLocaleLowerCase().indexOf(filterValue.trim().toLocaleLowerCase()) >= 0;
};