有效过滤数据

时间:2018-04-30 16:32:53

标签: angular ionic-framework ionic3 angular-pipe

我从服务器收到的示例数据:

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

我正在展示这些任务的模板以及我想在其上应用的过滤器:

  <ion-split-pane>
    <ion-grid>
      <ion-row padding>
        <h5>Filter tasks according to</h5>
      </ion-row>

      <ion-row padding>
        <ion-col>
          <ion-list>
            <ion-item>
              <ion-label>Frequence of performance</ion-label>
              <ion-select>
                <ion-option *ngFor="let frequency of filer_per_frequency" value="{{frequency.value}}">{{frequency.title}}</ion-option>
              </ion-select>
            </ion-item>
          </ion-list>
        </ion-col>
        <ion-col>
          <ion-list>
            <ion-item>
              <ion-label>Importance</ion-label>
              <ion-select [(ngModel)]="importance">
                <ion-option *ngFor="let importance of filer_per_importance" value="{{importance.value}}">{{importance.title}}</ion-option>
              </ion-select>
            </ion-item>
          </ion-list>
        </ion-col>
        <ion-col>
          <ion-list>
            <ion-item>
              <ion-label>Importance of improvement</ion-label>
              <ion-select>
                <ion-option *ngFor="let improvement of filer_per_improvement" value="{{improvement.value}}">{{improvement.title}}</ion-option>
              </ion-select>
            </ion-item>
          </ion-list>
        </ion-col>
        <ion-col>
          <ion-list>
            <ion-item>
              <ion-label>Stakeholder</ion-label>
              <ion-select>
                <ion-option *ngFor="let stakeholder of filer_per_stakeholder" value="{{stakeholder.value}}">{{stakeholder.title}}</ion-option>
              </ion-select>
            </ion-item>
          </ion-list>
        </ion-col>
      </ion-row>
      <ion-item-divider color="light"></ion-item-divider>

      <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>
    </ion-grid>


  </ion-split-pane>

我应该能够同时应用可用的多个过滤器。

如何在组件中处理,因为管道会降低性能?

1 个答案:

答案 0 :(得分:1)

Angular开发人员实际上建议不要使用管道。您可以在其网站上详细了解相关信息(https://angular.io/guide/pipes#no-filter-pipe

相反,您应该在加载数据时在组件中执行此操作。

作为一个非常粗略的例子

// Assumes you load your data in ngOnInit from some service
ngOnInit() {
    this.myService.loadData().subscribe(data => {
        // Filters out all null items
        this.filtered = data.filter((item) => item != null);

    })
}