【问题标题】:Reset Angular's Mat Data Table search filter重置 Angular 的 Mat 数据表搜索过滤器
【发布时间】:2019-04-06 02:17:01
【问题描述】:

我的网络应用程序使用 Material Data Tale (https://code-maze.com/angular-material-table/) 来显示一些现有数据。它还具有各种过滤器,包括 Mat Data Table 的内置搜索过滤器。我的问题是我可以在按下“清除所有过滤器”按钮时重置所有其他自行实现的过滤器,但我找不到清除搜索过滤器并重置过滤数据的方法。

  // This is my data source that is used in the template
  dataSource = new MatTableDataSource<MyObj>();

  // ... more code here ...

  clearFilters(){
        //clear other filters here

        //I want clear dataSource's search filter... but how?
  }

我还没有在其他任何地方看到此帖子,并且在执行 dataSource.filter = ""dataSource.filter = null 之类的操作时,更新观察者不会清除文本字段或产生任何结果。

【问题讨论】:

    标签: angular typescript angular-material-table


    【解决方案1】:

    将过滤器属性设置为空字符串。

    clearFilters(){
       this.dataSource.filter = '';
    }
    


    除了 bind a model 到元素之外,还重置过滤器输入值。

    模板:

    <mat-form-field class="this-is-a-class" floatLabel="never">
      <mat-icon matPrefix>search</mat-icon>
      <input matInput type="text" [(ngModel)]="filter" #ctrl="ngModel" (keyup)="applySearchFilter($event.target.value)" placeholder="Search by ID or Address..." autocomplete="off"> </mat-form-field>
    

    TS:

    filter:string;
    
    clearFilters(){
       this.dataSource.filter = '';
       this.filter = '';
    }
    

    【讨论】:

    • 就像我在问题末尾所说的那样,这不起作用。文本保留在搜索字段中。
    • @JamieTaylorSangerman : 你能分享你的模板和 ts 文件吗
    • 我不知道(或不认为)您可以像这样将值绑定到 ngModel。奇迹般有效。谢谢!
    • 为我工作并增加了改动。在 我添加了按钮 。进一步在我的 .module.ts 文件中导入了 MatIconModule 并在我的 .scss 文件中添加了一个 @import url("fonts.googleapis.com/icon?family=Material+Icons"); 以呈现一个小十字图标而不是按钮。
    猜你喜欢
    • 2014-02-03
    • 2017-04-02
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    • 2020-05-16
    • 1970-01-01
    • 2021-09-18
    相关资源
    最近更新 更多