Does the Clarity datagrid trust an external total count, or must the application clamp page requests when the backend bounds rows?
0 reputation · 26 Sept 2023, 22:48 UTC
0 reputation · 26 Sept 2023, 22:48 UTC
Binding an Angular Clarity datagrid to a large remote table raises a boundary question: the backend query bounds the returned rows with LIMIT and OFFSET, while the datagrid renders page controls. The unresolved decision is ownership of the total page count. The grid can be fed only the current page slice plus an explicit total count, or it can attempt to derive pagination boundaries from the rows currently bound. These two approaches disagree when the last-page control or page-size selector is used.
A related uncertainty appears after changing page size or applying a filter. The emitted page index may still reflect the previous page size, which can produce an out-of-range offset unless the application clamps the request. Because datagrid pagination and state APIs have changed across Clarity major releases, the exact contract is version-sensitive.
Does the datagrid treat an externally supplied total count as authoritative for last-page and page-size calculations? When page size changes or a filter is applied, does the grid re-emit a corrected page index, or must the application clamp the offset before issuing the next query? Which Clarity major version defines the current server-driven pagination state contract?
clr-datagrid does not own the state of the dataset. It uses the totalCount property provided by the application to calculate the total number of pages and enable/disable pagination controls. However, it does not automatically correct or clamp the active page index if the backend returns fewer rows than expected due to dynamic filtering or page-size changes.
The relationship between the grid and your data follows these rules in modern Clarity versions (v4 and later):
totalCount / pageSize. It emits events when the user interacts with these controls.When a user applies a filter or changes the page size, the grid may emit a page index based on the previous state. If the user was on page 10 (100 items) and a filter reduces the total results to 2 pages, the grid might still attempt to request page 10 until the application state is updated.
To prevent out-of-range offsets, you must implement manual clamping logic in your data service:
(paginationChange) or filter-applied events.totalCount from the backend.Math.ceil(totalCount / pageSize) - 1.// Example logic for clamping the offset in your component
onPaginationChange(event: ClrPaginationEvent) {
const maxPage = Math.ceil(this.totalCount / event.pageSize) - 1;
let targetPage = event.page;
if (targetPage > maxPage) {
targetPage = maxPage;
// Update the grid state to force the UI to sync
this.paginationState.page = targetPage;
}
this.fetchData(targetPage, event.pageSize);
}
p>Note: This server-driven contract is the standard for Clarity 4.0+. In legacy versions, the state management was more imperative, but the requirement for application-side clamping remains consistent for stability.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.