I have an array with over 100 object and I use *ngFor for show the objects . how can filter these objects with multiple checkboxes condistion ?
* how can select just employees are in paris and london and also under 30? when related checkboxe is true *
what is the best way for best performance in DOM changing ?
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>
is angular pipe best practice for this scenario ? is angular pipe best practice for this scenario ?