angular - 使用数组内容过滤 MatTableDataSource

标签 angular angular-material

我的 Angular 应用程序中有一个表,它在其行中显示用户数据。我希望能够使用代表用户电子邮件的字符串数组来过滤此表。如果数组中存在用户的电子邮件,那么我不希望用户显示在表中。

我研究过过滤 Angular Material 表,specifically here他们使用单个字符串来过滤表。我似乎找到的所有示例都涉及使用单个字符串,例如用户通过输入指定过滤器字符串。

但是,我找不到任何使用字符串数组来过滤数据的解决方案。我是否需要将整个数组连接成一个字符串,然后将其传递到 filterPredicate 属性中?

    applyAddedUserFilter() {
        this._usersService.addedUsers$.subscribe((addedUsers: AddedUser[]) => {
          let addedUserEmails: string[] = addedUsers.map(({ email }) => email);

          this.users.filterPredicate = (user: GoogleUser, filter: string) => { //<-- What exactly do I need to do here to use my string[] to filter my table's results?
            //Unsure what I need to do here.
          }
        });
      }

此外,一旦设置了过滤谓词,我需要在哪里调用此方法。一旦表构建完成就能够调用该方法就足够了吗?

最佳答案

我会这样做:

applyAddedUserFilter() {
    this._usersService.addedUsers$.subscribe((addedUsers: AddedUser[]) => {
      let addedUserEmails: string[] = addedUsers.map(user => user.email);

      this.dataSource.data = this.users.filter((user: GoogleUser) => !(addedUserEmails.indexOf(user.email) > -1));
    });
  }

这是一个堆栈 Blitz :Material Table Filter

关于angular - 使用数组内容过滤 MatTableDataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57374235/

相关文章:

Angular 2 ngfor第一个,最后一个,索引循环

angular - 错误 TS2339 : Property 'takeUntil' does not exist on type 'Observable<Foo>' and other rxjs v. 6 个错误

javascript - Angular 绑定(bind)在可观察调用之前没有改变

angular - 重建应用程序后,Ionic Native Storage 不会持续存在

javascript - 如何在 Angular Material 设计中为md-datepicker添加 "floating label"?

angularjs - 如何仅向 Angular ngMaterial 中的布局容器添加填充或边距?

javascript - HTML Range 元素值未使用 Angular6 函数更新

javascript - Angular/Material 中的样式化 mat-form-field 输入

angular - 在 Angular Material 表中更改时计算并显示输入字段的总计

angular - ngx-datatable rowClass 不工作