材料表 - 以编程方式触发过滤器

LeO*_*LeO 2 angular-material angular-material-table

根据这个示例,我知道我可以编写自己的this.dataSource.filterPredicate. 只要我搜索字符串,这就可以正常工作。我想根据使用的state(= myFilterState) 进行额外过滤。

我尝试使用谓词

this.dataSource.filterPredicate = 
    (data: MyElement, filter: string) => data.myType.useState === this.myFilterState;
Run Code Online (Sandbox Code Playgroud)

我面临的问题是,当我更改this.myFilterState过滤器时,直到我更改filter. 只要filter保持为空,就filterPredicate不会触发。

有没有办法手动触发它 - 尽管 的值filter

LeO*_*LeO 5

经过调查问题后,我发现只有当具有值filterpredicate时,源代码才会触发。filter否则不会被触发。

因此我想出了用覆盖内部的解决_filterData方案

this.dataSource._filterData = (data: PropertyCompact[]) => {
        this.dataSource.filteredData = data.filter(obj => this.dataSource.filterPredicate(obj, this.dataSource.filter));
            return this.dataSource.filteredData;
        };
Run Code Online (Sandbox Code Playgroud)

如果发生变化,需要触发更新

this.dataSource._updateChangeSubscription();
Run Code Online (Sandbox Code Playgroud)