LyteNyte Grid logo for light mode. Links back to the documentation home page.
Paginated Data Loading

Paginated Rows

Use the server data source to paginate rows using a cursor-based offset to load rows one page at a time.

Note

Pagination uses the server data source. This guide assumes you’re familiar with the concepts covered in the Server Row Overview guide.

Server-Side Row Pagination

Paginate rows by combining the server data source with a page cursor. Set up pagination in three steps:

  1. Define a page value in React state, for example using the useState hook.
  2. Include the page value in the queryKey property of the server data source.
  3. Pass a queryFn callback that loads rows for the current page.

When the page value changes, LyteNyte Grid calls queryFn to fetch the next page of rows.

The demo below shows basic pagination with caching, so previously loaded pages render without refetching when you return to them.

Row Pagination

Fork code on stack blitzFork code on code sandbox

The server returns both the current page’s data and the total row count, allowing you to calculate the total number of available pages.

const responseCache = useRef<Record<number, DataResponse[]>>({});
const ds = useServerDataSource<GridSpec["data"], [page: number]>({
queryFn: async ({ requests, queryKey }) => {
const page = queryKey[0];
if (responseCache.current[page]) {
return responseCache.current[page].map((x) => ({ ...x, asOfTime: Date.now() }));
}
const result = await Server(requests, page, pageSize);
responseCache.current[page] = result.pages;
setCount(result.count);
return result.pages;
},
queryKey: [page],
});

Pagination Row Size

Configure the rows per page by sending pageSize to the server with the current page value. Including pageSize in the server data source’s queryKey ensures that any change in page size resets the grid view and triggers a new server fetch.

In the demo below, use the Row per page menu to change the page size. Changing the page size clears the response cache and resets the page number to one.

Rows Per Page

Fork code on stack blitzFork code on code sandbox

Next Steps