angularMatTableDataSource的filterPredicate不会在筛选更新后更新表格。
创始人
2024-10-28 12:01:23
0

一种解决方案是在筛选更新后手动重新分配数据源的数据。在组件中注入 ChangeDetectorRef 并在 filter 发生更改时执行 markForCheck() 命令来通知 Angular 检查更改并重新加载数据。 以下是示例代码:

import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';

@Component({
  selector: 'app-my-table',
  templateUrl: './my-table.component.html',
  styleUrls: ['./my-table.component.css']
})
export class MyTableComponent implements OnInit {

  displayedColumns = ['name', 'age', 'gender'];
  dataSource = new MatTableDataSource();

  constructor(private cdr: ChangeDetectorRef) { }

  ngOnInit(): void {
    // set initial data
    this.dataSource.data = [
      { name: 'Alice', age: 25, gender: 'F' },
      { name: 'Bob', age: 30, gender: 'M' },
      { name: 'Charlie', age: 35, gender: 'M' },
      { name: 'Danielle', age: 40, gender: 'F' },
    ];

    // set filter predicate
    this.dataSource.filterPredicate = (data, filter) => {
      const searchString = filter.trim().toLowerCase();
      return data.name.toLowerCase().indexOf(searchString) !== -1;
    };
  }

  applyFilter(event: Event) {
    const filterValue = (event.target as HTMLInputElement).value;
    this.dataSource.filter = filterValue;

    // mark for check to ensure data source is updated
    this.cdr.markForCheck();
  }
}

在组件模板中,我们可以通过绑定到一个 input 元素上来实现筛选:



  ...

相关内容

热门资讯

透视app!wepoker私人... 透视app!wepoker私人局外卦(透视)wepoker透视脚本免费-都是有挂(哔哩哔哩)wepo...
更值得关注的是!aapoker... 更值得关注的是!aapoker辅助器是真的吗(透视)hhpoker万能辅助器-都是真的是有挂(哔哩哔...
透视能赢!wepokerplu... 透视能赢!wepokerplus到底是挂了吗(透视)wepoker游戏的安装教程-竟然真的是有挂(哔...
现有关情况通报如下!智星德州插... 现有关情况通报如下!智星德州插件(透视)智星德州辅助译码插件靠谱吗-好像真的有挂(哔哩哔哩)1、智星...
透视软件!wepoker辅助器... 透视软件!wepoker辅助器(透视)wepoker轻量版透视-都是是有挂(哔哩哔哩)1、进入到we...
透视教程!wpk透视最怕三个东... 透视教程!wpk透视最怕三个东西(透视)wpk透视怎么安装-果然真的是有挂(哔哩哔哩)1、完成wpk...
2026版辅助挂!wpk透视是... 2026版辅助挂!wpk透视是真的吗(透视)wpk有那种辅助吗-确实是有挂(哔哩哔哩)1、下载好wp...
透视安卓版!智星德州插件最新版... 透视安卓版!智星德州插件最新版本更新内容详解(透视)智星德州可以透视吗-切实是有挂(哔哩哔哩)小薇(...
透视真的!德普软件(透视)德普... 透视真的!德普软件(透视)德普之星透视辅助软件-竟然真的有挂(哔哩哔哩)1、游戏颠覆性的策略玩法,独...
透视中牌率!aapoker透视... 透视中牌率!aapoker透视插件(透视)hhpoker德州牛仔视频-其实是有挂(哔哩哔哩)暗藏猫腻...