gpt4 book ai didi

angular - 如何在延迟加载的 primeng turbotable 中使用过滤器?

转载 作者:太空狗 更新时间:2023-10-29 19:26:38 24 4
gpt4 key购买 nike

我正在使用带有延迟加载选项的 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)}`);
})
}

}

结果: enter image description here

最佳答案

举个例子:

模板

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

组件.ts

  @ViewChild('dt') dataTable: Table;
columnFilter(event: any, field) {
this.dataTable.filter(event.target.value, field, 'contains');
}

关于angular - 如何在延迟加载的 primeng turbotable 中使用过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49236929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com