5

我正在使用带有延迟加载选项的 PrimeNG TurboTable 来显示来自数据库的数据。这很完美。现在,我需要向发送到服务器的请求添加一些过滤器。怎么做 ?

这是有效的代码:

<p-table [columns]="cols" [value]="persons" [paginator]="true" [rows]="10" [lazy]="true" (onLazyLoad)="loadPersonsLazy($event)" 
            [totalRecords]="totalRecords" [loading]="loading">
  <ng-template pTemplate="header" let-columns>
    <tr>
      <th *ngFor="let col of columns">
        {{col.header}}
      </th>
    </tr>   
  </ng-template>
  <ng-template pTemplate="body" let-rowData let-columns="columns">
    <tr>
      <td *ngFor="let col of columns">
        {{rowData[col.field]}}
      </td>
    </tr>
  </ng-template>
</p-table>

组件 .ts 代码:

import { Component, OnInit } from '@angular/core';

import{IPerson} from '../iperson'
import{PrimengServiceService} from '../primeng-service.service'
import {LazyLoadEvent} from 'primeng/api'; 
import {FilterMetadata} from 'primeng/api';

@Component({
  selector: 'app-person',
  templateUrl: './person.component.html',
  styleUrls: ['./person.component.css']
})
export class PersonComponent implements OnInit {

  constructor(private _primengService: PrimengServiceService) { }

  persons:IPerson[] =[]; 
  cols: any[];
  totalRecords:number=100;
  loading: boolean;

  ngOnInit() {

    this.cols = [
      { field: 'FirstName', header: 'First Name' },
      { field: 'LastName', header: 'Last Name' },
      { field: 'EMail', header: 'EMail' },
      { field: 'Phone', header: 'Phone' }
  ];

  }


  loadPersonsLazy(event: LazyLoadEvent) {
    this.loading = true;
    this._primengService.getPersonList(event.first,event.rows).subscribe(res=>this.persons=res,
      error => {
        this.loading = false;
        console.log(`Error - getPersonList method: ${error}`);
      },
      () => {
        this.loading = false;
        console.log(`Info - getPersonList method succeeded. Results: ${JSON.stringify(this.persons)}`);
      })
  }

}

结果 : 在此处输入图像描述

4

1 回答 1

1

这是一个例子:

template

  <ng-template pTemplate="header" let-columns>
    <tr>
      <th *ngFor="let col of columns" [style.width]="columnWidthTable(col.field)" [pSortableColumn]="col.field" [pSortableColumnDisabled]="!col.sortable">
        <p-sortIcon *ngIf="col.sortable" [field]="col.field"></p-sortIcon>
        {{col.field | translate}}
        <input (click)="$event.stopPropagation()" [style.width]="'90%'" *ngIf="col.filterable" pInputText type="text" (keyup.enter)="columnFilter($event, col.field)">
      </th>
    </tr>
  </ng-template>

component.ts

  @ViewChild('dt') dataTable: Table;
  columnFilter(event: any, field) {
    this.dataTable.filter(event.target.value, field, 'contains');
  }
于 2018-04-23T15:52:37.823 回答