关注此sample我知道我可以编写自己的 this.dataSource.filterPredicate
。只要我搜索字符串,这就可以正常工作。我想根据使用的 state
(=myFilterState
) 进行额外过滤。
我尝试使用谓词,例如
this.dataSource.filterPredicate =
(data: MyElement, filter: string) => data.myType.useState === this.myFilterState;
我面临的问题是,当我更改 this.myFilterState
时,在更改 filter
的字符串之前,不会重新评估过滤器。只要 filter
保持为空,filterPredicate
就不会被触发。
有没有办法手动触发它 - 不管filter
的值如何?
最佳答案
经过对问题的调查,我发现只有当 filter
有值时,filterpredicate
才会由源代码触发。否则不会被触发。
因此我想出了用 覆盖内部 _filterData
的解决方案
this.dataSource._filterData = (data: PropertyCompact[]) => {
this.dataSource.filteredData = data.filter(obj => this.dataSource.filterPredicate(obj, this.dataSource.filter));
return this.dataSource.filteredData;
};
如果发生变化,需要触发更新
this.dataSource._updateChangeSubscription();
关于angular-material - Material 表 - 以编程方式触发过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66986507/