带有复选框的角度过滤器将覆盖先前的复选框值

时间:2018-12-05 13:09:33

标签: javascript html angular filter angular6

I have this filter。有2个字段:namegroup。每一个都可以正常工作(分别在类别上)。但是,如果我尝试结合使用tima等过滤器,则我期望[]为结果(因为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
 }

如何修改我的代码以获得我想要的东西?谢谢您的时间!

0 个答案:

没有答案