简体   繁体   English

带有 PrimeNG 的表格过滤器

[英]Table filter with PrimeNG

I am trying to implement a filter table ( https://primefaces.org/primeng/#/table/filter ), but the JSON that I receive have many levels, exemple:我正在尝试实现一个过滤器表( https://primefaces.org/primeng/#/table/filter ),但我收到的 JSON 有很多级别,例如:

{
  "id": "123",
  "categorie": "nice",
  "place": {
    "rank": "first",
    "person": {
      "name": "Joe"
    }
  }
}

And the documentation of the PrimeNG make this in the component: PrimeNG 的文档在组件中进行了说明:

export class DynamicColumnsDemo implements OnInit {

    cars: Car[];

    cols: any[];

    constructor(private carService: CarService) { }

    ngOnInit() {
        this.carService.getCarsSmall().then(cars => this.cars = cars);

        this.cols = [
            { field: 'vin', header: 'Vin' },
            { field: 'year', header: 'Year' },
            { field: 'brand', header: 'Brand' },
            { field: 'color', header: 'Color' }
        ];
    }
}

And this on the HTML:这在 HTML 上:

<p-table [value]="cars">
    <ng-template pTemplate="header">
        <tr>
            <th *ngFor="let col of cols">
                {{col.header}}
            </th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-car>
        <tr>
            <td *ngFor="let col of cols">
                    {{car[col.field]}}
            </td>
        </tr>
    </ng-template>
</p-table>

But, How can I put on "field" property a JSON with more levels, exemple:但是,如何将“字段”属性置于具有更多级别的 JSON,例如:

this.cols = [
  { field: 'place.person.name', header: 'Name' },
];

I already try N ways to do that and I don't know if this is possible...我已经尝试了 N 种方法来做到这一点,我不知道这是否可能......

If anyone can help me, I thanks!如果有人可以帮助我,我谢谢!

Regards.问候。

Assuming that we'd have the same structure for the Car interface but with an additional nested field, like so:假设我们对 Car 接口有相同的结构,但有一个额外的嵌套字段,如下所示:

export interface Car {
  vin?;
  year?;
  brand?;
  color?;
  price?;
  saleDate?;
  owner?: {
    name?;
    age?;
  };
}

And we'd want to search by car.owner.name , the way to do it is by creating a custom filter for that specific column.我们希望通过car.owner.name进行搜索,这样做的方法是为该特定列创建自定义过滤器。

The columns are:列是:

this.cols = [
  { field: "vin", header: "Vin" },
  { field: "color", header: "Color" },
  { field: "price", header: "Price" },
  { field: "ownerName", header: "Owner name" } // The field does not matter
];

So the column's content in the HTML file would be:所以 HTML 文件中列的内容将是:

<ng-template pTemplate="body" let-rowData let-columns="columns">
    <tr [pSelectableRow]="rowData">
        <td *ngFor="let col of columns">
            <ng-container *ngIf="col.field === 'ownerName'; else simpleTemplate">
                {{rowData.owner.name}}
            </ng-container>
            <ng-template #simpleTemplate>
                {{rowData[col.field]}}
            </ng-template>
        </td>
    </tr>
</ng-template>

The filter header content for this specific column:此特定列的过滤器标题内容:

    <input *ngSwitchCase="'ownerName'" pInputText type="text" 
           placeholder="Custom - Nested object" 
           (input)="dt.filter($event.target.value, 'owner', 'customNestedObj')">

Where 'customNestedObj' is actually a new method appended to the FilterUtils class from the library like so (in constructor/ngOnInit):其中“customNestedObj”实际上是从库中附加到 FilterUtils 类的一个新方法,如下所示(在构造函数/ngOnInit 中):

 FilterUtils["customNestedObj"] = (value, filter): boolean => {
      value = value.name;
      return FilterUtils.contains(value,filter);
    };

Stackblitz: https://stackblitz.com/edit/primeng-tablefilter-demo-9v5bhg?file=src%2Fapp%2Fapp.component.ts Stackblitz: https ://stackblitz.com/edit/primeng-tablefilter-demo-9v5bhg ? file = src%2Fapp%2Fapp.component.ts

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

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