【问题标题】:Material table - trigger filter programatically材料表 - 以编程方式触发过滤器
【发布时间】:2021-04-08 16:14:47
【问题描述】:

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

我尝试使用谓词 like

this.dataSource.filterPredicate = 
    (data: MyElement, filter: string) => data.myType.useState === this.myFilterState;

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

有没有办法手动触发它——尽管filter 的值是多少?

【问题讨论】:

    标签: angular-material angular-material-table


    【解决方案1】:

    调查问题后发现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;
            };
    

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

    this.dataSource._updateChangeSubscription();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 2020-10-08
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      相关资源
      最近更新 更多