I am trying to implement a lazy load for a large set of data with primeng <p-datatable>
. I have done everything documented on the official site, however I could not make it work.
The onLazyLoad
callback runs only once at the time of loading the table. It doesn't trigger on each scroll as expected.
<div style="max-height:300px; border:1px solid black;overflow-y:auto">
<p-dataTable #pocListref [value]="data" rowHover="true" [(selection)] = "selectedData" scrollable="true" scrollHeight="200px" [rows]="4"
[style]="{'margin-top':'30px'}" [paginator]="true" [rowsPerPageOptions]="[5,10,20]"
[lazy]="true" [totalRecords]="totalRecords" (onLazyLoad)="lazyLoad($event)">
<p-column header="Id">
<ng-template pTemplate="body" let-index="rowIndex">
{{index}}
</ng-template>
</p-column>
<p-column selectionMode="multiple" [style]="{'width':'5%'}"></p-column>
<p-column field="name" header="Name"[sortable]="true"></p-column>
<p-column field="age" header="Age" [sortable]="true"></p-column>
<p-column field="company" header="Company" [sortable]="true"></p-column>
</p-dataTable>
</div>
The same works fine with the paginator implementation. I have noticed that it works with paginator only if I do not use virtualScroll
attribute, which makes sense, but somehow virtualScroll doesn't make any effect on scrolling.
I know there is already a similar question but it is still answered.
Has someone used the virtual scroll successfully with lazy loading. Any suggestions will be useful.
Try like this my example :
here is my updated code without paginator and using virtualScroll
<p-dataTable [value]="resultsArr" scrollable="true" scrollHeight="100px" [rows]="4" virtualScroll="virtualScroll" [style]="{'margin-top':'30px'}" [lazy]="true" [totalRecords]="results.length" (onLazyLoad)="lazyLoad($event)">
<p-column header="Id">
<ng-template pTemplate="body" let-index="rowIndex">
{{index}}
</ng-template>
</p-column>
<p-column selectionMode="multiple" [style]="{'width':'5%'}"></p-column>
<p-column field="name" header="Name"[sortable]="true"></p-column>
<p-column field="age" header="Age" [sortable]="true"></p-column>
<p-column field="company" header="Company" [sortable]="true"></p-column>
</p-dataTable>
component.ts
export class Component {
private results: Array<any> = []; // have 15+ objects in this array
private resultsArr: Array<any> = [];
constructor() {
this.results = [{
name: "david",
age: 26,
company: "XYz Company"
}, {
name: "david",
age: 26,
company: "XYz Company"
}, {
name: "david",
age: 26,
company: "XYz Company"
}, {
name: "david",
age: 26,
company: "XYz Company"
}, {
name: "david",
age: 26,
company: "XYz Company"
}, {
name: "david",
age: 26,
company: "XYz Company"
}]
}
lazyLoad(event: any) {
setTimeout(() => {
if (this.results) {
this.resultsArr = this.results.slice(event.first, (event.first + event.rows));
}
}, 250);
}
}
component.html
<p-dataTable [value]="resultsArr" [lazy]="true" [totalRecords]="results.length" [responsive]="true"
[paginator]="true" (onLazyLoad)="lazyLoad($event)" [rows]="5" [filterDelay]="500"
[rowsPerPageOptions]="[5,10,20]" sortField="first_name" [sortOrder]="1">
<p-column field="id" header="#" [sortable]="true" styleClass="columnId" [filter]="true"></p-column>
<p-column field="first_name" header="First Name" [sortable]="true"></p-column>
<p-column field="last_name" header="Last Name" [sortable]="true"></p-column>
</p-dataTable>