0

I have a data as follows:

   tasks=  [
        {
        "id":8,
        "title":"Eight",    
        "how_often":"DS",
        "how_important_task":"",
        "how_important_improvement":"",
        "stakeholder":2,
        "project":2
        },

        {
        "id":9,
        "title":"Nine",
        "how_often":"",
        "how_important_task":"",
        "how_important_improvement":"",
        "stakeholder":2,
        "project":2
        },

        {
        "id":21,
        "title":"Seventeen",
        "how_often":"",
        "how_important_task":"",
        "how_important_improvement":"",
        "stakeholder":2,
        "project":2
        }
    ]

I have two models and a method in my component:

  public how_important_task: string= "";
  public how_often: string = "";

  applyFilters(){
    this.tasks = this.tasks.filter(task => {})
  }

And template with filters and list:

<ion-select [(ngModel)]="how_often" (ionChange)="applyFilters()">
                <ion-option value="">None</ion-option>
                <ion-option *ngFor="let frequency of filer_per_frequency" value="{{frequency.value}}">{{frequency.title}}</ion-option>
              </ion-select>
<ion-select [(ngModel)]="how_important_task" (ionChange)="applyFilters()">
                <ion-option *ngFor="let importance of filer_per_importance" value="{{importance.value}}">{{importance.title}}</ion-option>
              </ion-select>

...



<ion-list>
        <button detail-none (click)="expandItem(task)" ion-item *ngFor="let task of tasks">

          <h2>{{task.title}}</h2>
          <expandable [expandHeight]="itemExpandHeight" [expanded]="task.expanded">
            <hr><p>{{task.how_often | fullform}}</p>
            <p>{{task.how_important_task | fullform}}</p>
            <p>{{task.why_perform_task}}</p>
            <p>{{task.sequence_of_actions}}</p>
          </expandable>
        </button>
      </ion-list>

In my method I want to apply all selected filters on my Tasks. How can I achieve that?

2
  • Are you asking how to implement this.tasks.filter(task => {}) ? Commented Apr 30, 2018 at 20:06
  • That's correct, to check and filter according to multiple ngModel values Commented Apr 30, 2018 at 20:11

1 Answer 1

1

I took a stab at helping. You need to first create a copy of the original array. I did not include this but I assume that you have copied this somewhere to a variable called original_tasks. After that it's a simple Array.filter call. If both are defined they both must match. Please note that if how_important_task or how_often can be a value that would evaulate to "not truthy" you need update the if statement to account for it.

this.tasks = this.original_tasks.filter(task => {

     if(this.how_important_task && this.how_often){
          return task.how_important_task === this.how_important_task &&  task.how_often === this.how_often
     }

     if(this.how_important_task){
         return task.how_important_task === this.how_important_task;
     }

     if(this.how_often){
         return task.how_often === this.how_often;
     }

     return true;
});
Sign up to request clarification or add additional context in comments.

Comments

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.