这是我的专栏:
<p-column header="Roles" sortable="true" [filter]="true" filterMatchMode="contains">
<ng-template let-user="rowData" pTemplate="body">
<div *ngFor="let role of user.roles">
{{role.name}}
</div>
</ng-template>
</p-column>
user.roles JSON 示例:
[ { "id": 3, "name": "A single role" } ]
[ { "id": 4, "name": "A role name" }, { "id": 6, "name": "Another role name" } ]
我想在上面添加一个过滤器 - 我尝试添加这个 ng-template:
<ng-template pTemplate="filter" let-col let-test="rowData">
<p-multiSelect [options]="roles" defaultLabel="All roles" (onChange)="dt.filter($event.value,test.roles,col.filterMatchMode)"
styleClass="ui-column-filter"></p-multiSelect>
</ng-template>
多选的选项在一个单独的 Angular 色数组中:
this.roles = [];
this.roles.push({ label: 'A role', value: 'aRole' });
this.roles.push({ label: 'Another role', value: 'anotherRole' });
...但出于某种原因,我收到此错误:
Cannot read property 'roles' of undefined
- 我怎样才能做到这一点?
- 如何调试内联函数调用,如 dt.filter($event.value,test.roles,col.filterMatchMode)?
- 我认为我的主要问题是我的列没有可用于过滤的“字段”值。
最佳答案
根据官方论坛,我想要的是不可能的。需要一个“字段”。作为一种解决方法,我将我的 Angular 色数组展平为一个字符串并将其显示在一个字段中。我现在可以使用匹配模式“包含”过滤它
关于angular - 如何使用 ng-template 过滤 PrimeNg DataTable 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46305376/