角度阵列滤波器应用多个

时间:2018-04-30 20:01:01

标签: angular typescript ionic-framework ionic2 ionic3

我的数据如下:

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

我的组件中有两个模型和一个方法:

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

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

包含过滤器和列表的模板:

<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>

在我的方法中,我想在我的任务中应用所有选定的过滤器。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

我接受了帮助。您需要先创建原始数组的副本。我没有包括这个,但我假设你已经将它复制到一个名为original_tasks的变量。之后,这是一个简单的Array.filter电话。如果两者都被定义,则两者必须匹配。请注意,如果how_important_taskhow_often可以是一个可以评价为&#34;而不是真实的值。你需要更新if语句来解释它。

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;
});