PrimeNG Table Lazy Loading with Server-Side Pagination State Synchronization
0 reputation · 13 Jul 2022, 14:40 UTC
0 reputation · 13 Jul 2022, 14:40 UTC
PrimeNG's p-table component supports lazy loading through the [lazy] input and (onLazyLoad) event emitter. When enabled, the component delegates data fetching to the application, passing a LazyLoadEvent object containing first (offset) and rows (page size) values. The integrated p-paginator updates these values automatically on user interaction.
Applications using server-side pagination must decide how to synchronize the table's internal pagination state with the backend response. The first and rows properties can be bound as inputs, but the component also maintains its own state when the paginator is used. If the backend returns a different total record count than expected, or if the page size changes dynamically, the paginator UI may display incorrect page numbers or disabled navigation controls.
first and rows properties be treated as one-way inputs or two-way bindings when using a custom paginator template?first value when the onLazyLoad handler fetches a page that the backend rejects due to an out-of-range offset?onLazyLoad call?33275 reputation · 13 Jul 2022, 15:15 UTC
For server‑side pagination with PrimeNG’s p-table you should bind [first] and [rows] as one‑way inputs. The table will update these values internally when the paginator changes, and you must manually reset first (to 0) and trigger a reload when the backend rejects an out‑of‑range offset or when filters/sorts change.
[first] and [rows]) prevents duplicate updates and stale state; two‑way binding ([(first)]/[(rows)]) can cause the paginator to overwrite values set by your code.onLazyLoad receives a first value ≥ totalRecords, PrimeNG does **not** auto‑correct the offset; the table retains the invalid first until the application resets it and triggers a new load.first = 0 (and optionally adjust rows), then invoke the table’s load mechanism (table.load() or emit a new LazyLoadEvent) to fetch the first page.onLazyLoad event to fire twice or with stale values.first leaves the table showing an empty page or disabled navigation until you correct it.<p-table [value]="data" [lazy]="true" [first]="first" [rows]="rows"
(onLazyLoad)="loadData($event)" [totalRecords]="total">
...
</p-table>first and rows to call your service. If the service returns an error indicating the offset is out of range, set first = 0 and call loadData({ first: 0, rows: this.rows }) (or table.load()) to recover.this.first = 0;
// optional: adjust rows if page size changed
this.table.load(); // or emit a new LazyLoadEventprivate skipLazy = false;
loadData(event: LazyLoadEvent) {
if (this.skipLazy) {
this.skipLazy = false;
return;
}
// normal server request
}
// after filter/sort when data is cached
this.skipLazy = true;
this.first = 0;
this.table.load();[totalRecords] with the backend’s total count after each successful response; this keeps the paginator’s page‑count and navigation buttons correct.Are you updating the [totalRecords] binding after every server response? If not, the paginator may show wrong page numbers or disable navigation even when the offset is correct.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.