简体   繁体   English

Angular 7:*ngFor 中的动态过滤器

[英]Angular 7 : Dynamic filters in *ngFor

I am displaying list like below我正在显示如下列表

<tr role="row" *ngFor="let record of recordList | filterColumn: {AccessCode: filters.accessCode} | filterColumn: {Organization: filters.organization} | filterColumn: {LastName: filters.lastName}"></tr>

Now what i want to do is generate | filterColumn: {AccessCode: filters.accessCode} | filterColumn: {Organization: filters.organization}现在我想做的是生成| filterColumn: {AccessCode: filters.accessCode} | filterColumn: {Organization: filters.organization} | filterColumn: {AccessCode: filters.accessCode} | filterColumn: {Organization: filters.organization} | filterColumn: {AccessCode: filters.accessCode} | filterColumn: {Organization: filters.organization} from code/component & add it to for loop. | filterColumn: {AccessCode: filters.accessCode} | filterColumn: {Organization: filters.organization}来自代码/组件并将其添加到 for 循环。

These filters are conditional & i am creating generic grid.这些过滤器是有条件的,我正在创建通用网格。 So i want to do these filters dynamically所以我想动态地做这些过滤器

How can i achieve this我怎样才能做到这一点

Here are two ways of doing it:这里有两种方法:

1) Modify the array your looping through in your component with your filters. 1)使用过滤器修改组件中循环的数组。 If you press a button to activate a filter, do the filtering on the array in the component when the button is pressed.如果按下按钮激活过滤器,则在按下按钮时对组件中的数组进行过滤。

2) Do your loop in a ng-container element, and have ngIf nested underneath: 2) 在 ng-container 元素中执行循环,并将 ngIf 嵌套在下面:

 <ng-container *ngFor="let record of recordList">
    <tr role="row" *ngIf="record.AccessCode === filters.accessCode"></tr>
 </ng-container>

Use wgnx-filter pipe to make your dynamic filter conditions.使用wgnx-filter管道来创建动态过滤条件。

In the filter method, you can be configure fields to apply the filter and sent to pipe component.在过滤器方法中,您可以配置字段以应用过滤器并发送到管道组件。

In html, use:在 html 中,使用:

<tr *ngFor="let invoice of filterFunction(invoices), index as i">

In typescript function, see the sample:在打字稿功能中,请参阅示例:

filterFunction(collection: any[]): any[] {
    return this.pipefilter.transform(collection, [
      { field: "general.number_invoice", value: this.filterInvoice }, // nested property
      { field: "note_invoice", value: this.filterInvoice },
      { field: "customer_invoice", value: this.filterInvoice },
      { field: "payment_invoice", value: this.filterInvoice }
    ]);
}

You can assemble the parameter array with the fields that will be used in the filter dynamically.您可以将参数数组与将在过滤器中动态使用的字段组合在一起。

For a complete example, see stackblitz有关完整示例,请参阅stackblitz

For get component documentation, got to npm package wngx-filter要获取组件文档,请访问 npm package wngx-filter

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM