PrimeNG动态列过滤

时间:2018-02-21 22:57:43

标签: angular typescript angular5 primeng primeng-turbotable

我目前有以下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>   

我需要能够在页面加载时按年龄列进行过滤,我该怎么做?我可以找到的所有示例都使用(输入)或(onChange)标签(从他们的网站上获取):

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

如何在加载时按列过滤而不是在元素更改上进行过滤?

这是我引用的页面: https://www.primefaces.org/primeng/#/table/filter

谢谢!

2 个答案:

答案 0 :(得分:5)

使用静态列时,必须在表级别指定过滤器的列名。在表级别添加[globalFilterFields] =“ ['Age']”。

 <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>

答案 1 :(得分:2)

<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>

对于全局过滤器 - (input)="dt.filterGlobal($event.target.value, 'contains')"

对于列过滤器 - (input)="dt.filter($event.target.value, col.field, col.filterMatchMode)"

相关问题