如何根据选定的下拉值过滤 *ngFor 结果
how to filter *ngFor result based on a selected dropdown value
选择类别:
<ion-select name="categories">
<ion-option *ngFor="let category of categories;">
{{category}}
</ion-option>
</ion-select>
列出项目:
<ion-item-sliding *ngFor="let item of items; let idx = index;">
<ion-item>
<h2>{{item.title}}</h2>
</ion-item>
...
</ion-item-sliding>
如何使用 (select) 中的值来选择要列出的类别?我尝试添加一个 ngModule 并将其作为条件传递到滑动 ngFor 中,但它不起作用。
您可以使用管道来实现此目的
<ion-item-sliding *ngFor="let item of items | categorySelected; let idx = index;">
<ion-item>
<h2>{{item.title}}</h2>
</ion-item>
...
</ion-item-sliding>
在你的转换方法中你可以执行你的逻辑
transform(s[],cat){
///logic
return filteredArray
}
更新 1:完整答案
自定义管道将具有以下代码,
transform(records: Array<any>, property:any): any {
let sortedArray=[];
if(property){
sortedArray =_.filter(records, {'office':property.Name]);
console.log(sortedArray);
return sortedArray
}
}
下拉列表和项目列表
<div>
<select [(ngModel)]="selectedElement">
<option *ngFor="let type of types" [ngValue]="type">
{{type.Name}}</option>
</select>
<span *ngFor="let x of array | orderBy:selectedElement">{{x.firstName}}</span>
</div>
你也可以简单地把
ngOnChanges() {
this.itemsForDisplay = this.items.filter(element => element.category = this.category);
}
并在模板中显示 itemsForDisplay
而不是 items
您可以使用 ngx-pipes 库在数组和字符串中进行令人难以置信的过滤和转换。
对于您的问题,您可以使用 ngx-pipes
库中的 filterBy 管道。
<ion-option *ngFor="let category of categories | filterBy:['name']:selectedVariable;">
选择类别:
<ion-select name="categories">
<ion-option *ngFor="let category of categories;">
{{category}}
</ion-option>
</ion-select>
列出项目:
<ion-item-sliding *ngFor="let item of items; let idx = index;">
<ion-item>
<h2>{{item.title}}</h2>
</ion-item>
...
</ion-item-sliding>
如何使用 (select) 中的值来选择要列出的类别?我尝试添加一个 ngModule 并将其作为条件传递到滑动 ngFor 中,但它不起作用。
您可以使用管道来实现此目的
<ion-item-sliding *ngFor="let item of items | categorySelected; let idx = index;">
<ion-item>
<h2>{{item.title}}</h2>
</ion-item>
...
</ion-item-sliding>
在你的转换方法中你可以执行你的逻辑
transform(s[],cat){
///logic
return filteredArray
}
更新 1:完整答案
自定义管道将具有以下代码,
transform(records: Array<any>, property:any): any {
let sortedArray=[];
if(property){
sortedArray =_.filter(records, {'office':property.Name]);
console.log(sortedArray);
return sortedArray
}
}
下拉列表和项目列表
<div>
<select [(ngModel)]="selectedElement">
<option *ngFor="let type of types" [ngValue]="type">
{{type.Name}}</option>
</select>
<span *ngFor="let x of array | orderBy:selectedElement">{{x.firstName}}</span>
</div>
你也可以简单地把
ngOnChanges() {
this.itemsForDisplay = this.items.filter(element => element.category = this.category);
}
并在模板中显示 itemsForDisplay
而不是 items
您可以使用 ngx-pipes 库在数组和字符串中进行令人难以置信的过滤和转换。
对于您的问题,您可以使用 ngx-pipes
库中的 filterBy 管道。
<ion-option *ngFor="let category of categories | filterBy:['name']:selectedVariable;">