我有一个数据如下:
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>
在我的方法中,我想对我的任务应用所有选定的过滤器。我怎样才能做到这一点?
我试着帮忙。您需要先创建原始数组的副本。我没有包括这个,但我假设你已经将它复制到了一个名为original_tasks
. 之后是一个简单的Array.filter调用。如果两者都已定义,则它们都必须匹配。请注意,如果how_important_task
或how_often
可以是一个评估为“不真实”的值,您需要更新 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;
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句