I'm using TurboTable of PrimeNG with lazy load option to display data from DB. This works perfect. Now, I need to add few filters to the request that is sending to the server. How to do that ?
Here the code that works :
<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>
Component .ts code:
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)}`);
})
}
}