angular - 如何根据列的布尔值过滤此表?
问题描述
我正在尝试根据我的“项目”是否处于活动状态来过滤表格。我目前有一个表,其中“活动”是一列,它会根据项目状态说真或假。我希望能够根据这个布尔值过滤我的表。
所以我目前正在使用一个选择菜单,它将根据选择返回真或假。从我一直在尝试的情况来看,该表不会过滤布尔值,只会过滤字符串。所以我有一个过滤的谓词,它试图只在我的活动列上强制过滤,但是它目前没有按预期工作。
//.ts file
dataSource: MatTableDataSource<OrderTemplateInterfaceWithId> = new MatTableDataSource([]);
displayedColumns = [
'dayOfWeek',
'startTime',
'endTime',
'technician',
'frequency',
'startDate',
'endDate',
'active',
'actions'
];
ngOnInit() {
this.dataSource.filterPredicate = (data: OrderTemplateInterfaceWithId, filter: string) => {
return data.active === (filter === "true") || data.active === (filter === "false");
};
}
applyFilter(filterValue: string) {
console.log(this.dataSource);
this.dataSource.filter = filterValue;
}
//.html file
<mat-form-field>
<mat-select (selectionChange)="applyFilter($event.value)">
<mat-option value='true'>Active</mat-option>
<mat-option value='false'>InActive</mat-option>
</mat-select>
</mat-form-field>
因此,我希望结果会根据我的选择菜单按活动列过滤我的表格。任何帮助将不胜感激,谢谢!
解决方案
就这么简单:
过滤谓词定义
ngInit(){
this.dataSource.filterPredicate = (data: any, filter: string) => !filter || data.checked == Boolean(filter);
}
过滤功能为:
apllyfilter(){
this.dataSource.filter = "true";
}
推荐阅读
- c++ - 未知的 GCC 链接器错误,但构建成功
- firebase - 如何在安全规则中将我的文档中的字段设置为时间戳或 null?
- reactjs - 将映射状态反应到列表项
- html - HTML CSS,SVG FILL 不工作,完整示例和 jsfiddle
- python - Flask DateTime 包
- python - if 语句:数组是否有单位/维度(品脱)
- javascript - 获取网站的活动 Websocket 可能吗?
- python - Django Web App Heroku Deployement:没有为 decouple==0.0.7 找到匹配的分布
- java - 实现网络线程比使用加载器或 AsyncTask 更好的方法
- hive - Apache Hive 分区和分桶结构