Behavior when totalRecords changes
PrimeNG’s <p-table> calculates the number of pages for its paginator from the totalRecords value supplied during a lazy load event. The component does not automatically adjust its internal page index when that value changes between successive requests. As a result:
- Decrease in totalRecords – If the current page index is now beyond the last page, the paginator shows the old page number while the table renders empty rows or a partial dataset.
- Increase in totalRecords – New pages become available only after the component’s
totalRecords property is updated and the paginator is refreshed; otherwise the extra pages stay invisible.
- In
virtualScroll or infiniteScroll modes the same limitation applies: the scroll handler relies on the last known totalRecords to decide when to stop loading.
What developers need to do
To keep the UI consistent you must:
- Update the
totalRecords input on the <p-table> whenever the backend reports a new count.
- Call
table.reset() (or set table.first = 0) after the property change. This forces the paginator to recalculate its page count and reload the first page.
- Ensure the backend can handle offsets that may now be outside the new data range and return an empty or truncated dataset gracefully.
- If using virtual scroll, trigger
table.refresh() or a similar method to re‑render the visible rows after a totalRecords change.
When to reset
Reset the table in any of the following situations:
- The backend count has dropped below the current page’s first row index.
- The count has increased and you want the new pages to be reachable immediately.
- You switch between data sets that have different total counts.
Additional considerations
• In PrimeNG 13–15 the paginator uses Math.ceil(totalRecords / rows); older releases (<12) may not expose totalRecords at all, requiring manual pagination logic.
• The component does not emit an event when totalRecords changes, so the developer must explicitly update the property.
• If you are observing stale page numbers or blank rows, check that you are calling reset() after updating totalRecords and that your backend returns an empty array for out‑of‑bounds offsets.
Need more context?
To tailor the recommendation further, let us know which PrimeNG version you are using, as the internal implementation of the paginator can differ between releases.