简体   繁体   English

如何编写Angular ngFor管道来按对象属性过滤对象数组?

[英]How can I write an Angular ngFor pipe to filter array of objects by object property?

I have 2 selects. 我有2个选择。

One for Leagues and one for Divisions 一个用于Leagues ,一个用于Divisions

I want to create a Pipe that will filter Divisions depending on what League is selected. 我想根据选择的League创建一个过滤DivisionsPipe

Giving the data below. 提供以下数据。 If I select Random Beer League only TwoFour and SixPack should show up as options for the Divisions select. 如果我选择Random Beer League只有TwoFourSixPack应显示为Divisions选项的选项。

leagues = [
  {id: 1, leagueName: 'Recreation League' },
  {id: 2, leagueName: 'Random Beer League' } 
];

divisions = [
  {id: 1, divisionName: 'SoGreen', leagueId: 1, leagueName: 'Recreation League' },
  {id: 2, divisionName: 'Yellow', leagueId: 1, leagueName: 'Recreation League' },
  {id: 3, divisionName: 'TwoFour', leagueId: 2, leagueName: 'Random Beer League' },
  {id: 4, divisionName: 'SixPack', leagueId: 2, leagueName: 'Random Beer League' }
];

PLUNKER to show the setup PLUNKER显示设置

*Note - data in plunker is hard coded but in my app setup its Observable. *注意 - plunker中的数据是硬编码的,但在我的应用程序中设置了Observable。

I have create a custom pipe and used it to filter division dropdown. 我创建了一个自定义管道并用它来过滤部门下拉列表。

@Pipe({
    name: 'myfilter',
    pure: false
})

export class MyFilterPipe implements PipeTransform {
    transform(items: any[], args: any[]): any {
        return items.filter(item => item.leagueName.indexOf(args.leagueName) > -1);
    }
}


<option *ngFor="let division of divisions | myfilter:leagueSelected" [ngValue]="division">{{division.divisionName}}</option>

Please look into this Plnkr 请看看这个Plnkr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何过滤作为对象数组中对象属性的电子邮件域? - How can I filter an email domain that is a property of an object that is in an array of objects? 如何在 Angular 4 中使用 *ngFor 实现自定义过滤器管道? - How to implement Custom Filter Pipe with *ngFor in Angular 4? 如何使用 ngfor 循环遍历作为 angular 对象数组的属性 - how to use ngfor to loop over a property that is an array of objects in angular Angular:我如何通过对象数组按属性过滤Rxjs? - Angular: How do I Rxjs filter by property through an array of objects? 如何在 angular 中创建 pipe 以过滤具有嵌入式数组的对象数组 - How to create a pipe in angular to filter array of objects with embedded array 如何通过对象数组中的任何属性过滤数据? - How can I filter data by any property within an array of objects? 如何在VueJS中按对象属性过滤对象数组 - How to filter array of objects by object property in VueJS Angular 4中用于Json对象数组的自定义管道过滤器 - Custom Pipe Filter For Array Of Json Objects In Angular 4 如何将过滤器 pipe 添加到具有未定义值的 object 数组的属性中 - How to add filter pipe to a property of an array of object having undefined value Angular2-根据对象属性的值过滤对象数组 - Angular2 - Filter array of objects based on value of an object property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM