使用多个复选框条件的angular4数组过滤

时间:2019-06-11 07:32:05

标签: angular angular-pipe

我有一个包含100个以上对象的数组,并且我使用* ngFor来显示对象。如何过滤具有多个复选框条件的这些对象?

*如何仅选择巴黎,伦敦以及30岁以下的员工?当相关复选框为真*

在DOM更改中获得最佳性能的最佳方法是什么?

export class showEmployees implements OnInit {



  constructor() { }

  ngOnInit() { }


  employees: any[] = [
    { city: 'paris', gender: 'male', age: 27 },
    { city: 'london', gender: 'female', age: 55 },
    { city: 'chicago', gender: 'male', age: 26 },
    { city: 'paris', gender: 'female', age: 56 },
    { city: 'london', gender: 'male', age: 60 },
    { city: 'chicago', gender: 'female', age: 24 },
    { city: 'london', gender: 'male', age: 54 },
    { city: 'paris', gender: 'female', age: 22 },
  ];

  list = this.employees;

  employeesFiltered = this.employees.slice();



  filterByParis(event) {

    if (event.target.value === 'paris' && event.target.checked) {
      this.employeesFiltered = this.employees.filter(
        item => item.city === 'paris'
      );

      this.list = this.employeesFiltered
    } else {
      this.list = this.employees
    }

  }

  filterByOld(event) {
    if (event.target.value === 'old' && event.target.checked) {
      this.employeesFiltered = this.employees.filter(
        item => item.age > 50
      );

      this.list = this.employeesFiltered
    } else {
      this.list = this.employees
    }
  }


}
<ul>
  <li> <input type="checkbox" value="paris" (change)="filterByParis($event)">paris</li>
  <li> <input type="checkbox" value="london" (change)="filterByLondon($event)">london </li>
  <li> <input type="checkbox" value="chicago" (change)="filterByChicago($event)">chicago </li>
  <li> <input type="checkbox" value="male" (change)="filterByMale($event)">male </li>
  <li> <input type="checkbox" value="female" (change)="filterByFemale($event)">female </li>
  <li> <input type="checkbox" value="old" (change)="filterByOld($event)"> greater than 50 </li>
  <li> <input type="checkbox" value="young" (change)="filterByYoung($event)">under 30 </li>
</ul>

<table>
  <thead>
    <th>city</th>
    <th>gender</th>
    <th>age</th>
  </thead>
  <tbody>
    <tr *ngFor="let member of list">
      <td>{{member.city}}</td>
      <td>{{member.gender}}</td>
      <td>{{member.age}}</td>
    </tr>
  </tbody>
</table>

在这种情况下角管是最佳实践吗? 弯管在这种情况下是最佳实践吗?

1 个答案:

答案 0 :(得分:1)

不使用管道,但是您只需要一个函数过滤器,并使用数组和[[ngModel)]重新考虑代码。假设您有一个像这样的对象数组

selects=[
{description:"París",type:"pais",data:"paris",value:false},
{description:"London",type:"pais",data:"london",value:false},
..
{description:"Male",type:"gender",data:"male",value:false},
{description:"Female",type:"gender",data:"female",value:false}
...
]

您可以将.html设为

<ul>
  <li *ngFor="let select of selects>
     <input type="checkbox" [(ngModel)]="select.value"
         (change)="refresh()">{{select.data}}
  </li>
</ul>

您的函数refresh()可以像

refresh()
{
    if (this.selects.find(x=>x.value)) //if there are al least one filter
    {
       this.employeesFiltered = null;
       this.selects.forEach(select=>{
        if (select.value)
        {
           switch(select.type)
           {
              case "pais":
                this.employeesFiltered=this.employeesFiltered ?
                     this.employeesFiltered.filter(e=>e.pais==select.data) :
                     this.employees.filter(e=>e.pais==select.data)
                break;
              case "gender":
                    ....
              ...
            }
           }
         }
       })
    }
    else
       this.employeesFiltered = this.employees;
}