我的 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/