PrimeNG Dynamic Column Filtering
Asked Answered
S

2

7

I currently have the following PrimeNG TurboTable:

<p-table [value]="People" >
  <ng-template pTemplate="header">
    <tr>
      <th>Name</th>
      <th>Age</th>
      <th>Height</th>
    </tr>
  </ng-template>
  <ng-template pTemplate="body" let-col>
    <tr>
      <td>{{col.Name}}</td>
      <td>{{col.Age}}</td>
      <td>{{col.Height}}</td>
    </tr>
  </ng-template>
</p-table>   

I need to be able to filter by the Age column when the page loads, how would I do this? All the examples I could find show use an (input) or (onChange) tag like so (taken from their website):

<input type="text" pInputText size="50" placeholder="Global Filter" (input)="dt.filterGlobal($event.target.value, 'contains')" style="width:auto">

How can I filter by column on load rather than on an element changing?

Here's the page I'm referencing: https://www.primefaces.org/primeng/#/table/filter

Thank you!

Scarab answered 21/2, 2018 at 22:57 Comment(2)
Execute the filterGlobal() on ngOnInit() with filter criteria as requiredAtabrine
Unfortunately the filterGlobal() function will filter everything, whereas I only want to filter a specific column leaving the global filter still available. Or do you have an example how to filter a single column using the filterGlobal method?Scarab
C
13

When you use static columns you have to specify the column name for the filter at the table level. Add [globalFilterFields]="['Age']" at table level.

 <p-table #dt [value]="data" [globalFilterFields]="['Age']">
                <ng-template pTemplate="caption">
                    <div style="text-align: right">
                        <i class="fa fa-search" style="margin:4px 4px 0 0"></i>
                        <input type="text" pInputText size="50" placeholder="Global Filter" (input)="dt.filterGlobal($event.target.value, 'contains')"
                            style="width:auto">
                    </div>
                </ng-template>
                <ng-template pTemplate="header">
                    <tr>
                        <th>Name</th>
                        <th>Age</th>
                        <th>Height</th>
                    </tr>
                </ng-template>
                <ng-template pTemplate="body" let-col>
                    <tr>
                        <td>{{col.Name}}</td>
                        <td>{{col.Age}}</td>
                        <td>{{col.Height}}</td>
                    </tr>
                </ng-template>
            </p-table>
Colis answered 26/7, 2018 at 7:3 Comment(1)
Remember that the field names like aFieldName you give are the dataItem.aFieldName in the above case with let-col they are the fields contained into colCatherin
A
8
<ng-template pTemplate="header" let-columns>
    <tr>
        <th *ngFor="let col of columns">
            {{col.header}}
        </th>
    </tr>
    <tr>
        <th *ngFor="let col of columns" [ngSwitch]="col.field">
            <input *ngSwitchCase="'vin'" pInputText type="text" (input)="dt.filter($event.target.value, col.field, col.filterMatchMode)">
            <div *ngSwitchCase="'year'">
                {{yearFilter}}
                <i class="fa fa-close" (click)="yearFilter=null;dt.filter(null, col.field, col.filterMatchMode)" style="cursor:pointer"></i>
                <p-slider [style]="{'width':'100%','margin-top':'8px'}" [(ngModel)]="yearFilter" [min]="1970" [max]="2010" (onChange)="onYearChange($event, dt)"></p-slider>
            </div>
            <p-dropdown *ngSwitchCase="'brand'" [options]="brands" [style]="{'width':'100%'}" (onChange)="dt.filter($event.value, col.field, 'equals')"></p-dropdown>
            <p-multiSelect *ngSwitchCase="'color'" [options]="colors" defaultLabel="All Colors" (onChange)="dt.filter($event.value, col.field, 'in')"></p-multiSelect>
        </th>
    </tr>
</ng-template>

For Global Filter - (input)="dt.filterGlobal($event.target.value, 'contains')"

For Column Filter - (input)="dt.filter($event.target.value, col.field, col.filterMatchMode)"

Atabrine answered 19/3, 2018 at 5:11 Comment(1)
This was copied from the primeng docs. primefaces.org/primeng/#/table/filterVansickle

© 2022 - 2024 — McMap. All rights reserved.