DataTable: How to filter over a column without a field ? (with ng-template)

UI Components for Angular
Post Reply
TheParadoX
Posts: 8
Joined: 11 Jul 2017, 15:52

19 Sep 2017, 16:10

Hello,

This is my column:

Code: Select all

    <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>
Examples of user.roles JSON:

Code: Select all

[ { "id": 3, "name": "A single role" } ]
[ { "id": 4, "name": "A role name" }, { "id": 6, "name": "Another role name" } ]
I would like to add a filter on it - I tried adding this ng-template:

Code: Select all

      <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>
The multiselect's options are in a separate roles array:

Code: Select all

this.roles = [];
this.roles.push({ label: 'A role', value: 'aRole' });
this.roles.push({ label: 'Another role', value: 'anotherRole' });
... but for some reason, I get this error:
Cannot read property 'roles' of undefined
- How can I achieve this ?
- How can I debug inline function calls like "dt.filter($event.value,test.roles,col.filterMatchMode)" ?

Cheers,
Para

paulswan
Posts: 94
Joined: 27 Apr 2017, 08:19

21 Sep 2017, 01:22

Because you are using a multiSelect your filterMatchMode should be "in" instead of "contains". Also try the following changes to your filter template:

Code: Select all

      <ng-template pTemplate="filter" let-col>
        <p-multiSelect [options]="roles" defaultLabel="All roles" (onChange)="dt.filter($event.value,col.field,col.filterMatchMode)"
          styleClass="ui-column-filter"></p-multiSelect>
      </ng-template>

TheParadoX
Posts: 8
Joined: 11 Jul 2017, 15:52

21 Sep 2017, 09:29

The problem is that my column does not have a field. So giving the filter method a col.field reference won't work.

paulswan
Posts: 94
Joined: 27 Apr 2017, 08:19

22 Sep 2017, 00:45

All columns should have a field or you cannot perform a filter on the column.
The inputs for the filter() function are as follows so you will need to use a field.

Code: Select all

filter(value, field, matchMode)

Post Reply

Return to “PrimeNG”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 6 guests