I have this filter。有2个字段:name
和group
。每一个都可以正常工作(分别在类别上)。但是,如果我尝试结合使用tim
和a
等过滤器,则我期望[]
为结果(因为tim
不属于组{{1 }}),但a
将立即覆盖a
。
此外,如果我选中tim
,然后取消选中它,我希望原始数组再次出现。
.ts
tim
.html
changeGroup(event) {
const group = event.target.value;
const index = this.groupValue.indexOf(group);
if (index > -1) {
this.groupValue.splice(index, 1);
} else {
this.groupValue.push(group);
}
this.transform(this.usersList, 'group', this.groupValue)
if (this.groupValue.length == 0) {
this.transform(this.usersList, '', this.groupValue)
}
}
changeUser(event) {
const user = event.target.value;
const index = this.userValue.indexOf(user);
if (index > -1) {
this.userValue.splice(index, 1);
} else {
this.userValue.push(user);
}
this.transform(this.usersList, 'name', this.userValue)
if (this.userValue.length == 0) {
this.transform(this.usersList, '', this.userValue)
}
}
transform(items: any[], field: string, value: string[]): any[] {
if (!items) {
return [];
}
if (!field || !value || value.length <= 0) {
return items
}
this.newArray = items.filter(singleItem => {
return (singleItem != null && singleItem[field] != null && singleItem[field] != undefined && value.indexOf(singleItem[field]) >= 0);
});
return this.newArray
}
如何修改我的代码以获得我想要的东西?谢谢您的时间!