0

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 ?

1 Answer 1

1

NOT use a pipe, but you need has only a function filter and re-think your code using arrays and [(ngModel)]. Imagine you has an array of object like

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}
...
]

You can make a .html like

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

Your function refresh() can be like

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you . are you tested this code ? can you filter members are in paris and also are female and also are under 30
sorry, I'm not tested the code. There was an error: we need check if select.value -just corrected in code-, Im in hurry ::glups::

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.