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

Paginated Rows

Pagination lets you load rows one page at a time using a cursor-based offset.

Note

Paginated row loading uses the server data source in LyteNyte Grid. You must already be familiar with the server data source before you read this guide.

Basic Pagination

Paginate rows by combining the server data source with a page cursor. Set up pagination in a two-step process:

  • Define a page value in React state, e.g. by using the useState hook.
  • Include the page value in the queryKey property of the server data source.

Then pass a queryFn function to LyteNyte Grid’s server data source. The queryFn function loads the data for the current page. When page changes, LyteNyte Grid calls queryFn to fetch the next slice of rows.

The demo below demonstrates the basic pagination setup with caching included, so that pages that have already been loaded resolve instantly when navigating back to a previous page.

Basic Paginated Implementation

Fork code on stack blitzFork code on code sandbox

The demo retrieves the data for the current page. The server also sends the total row count, which you can use to compute the number of pages available.

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 Size

You can configure the number of rows requested per page by sending the page size state (pageSize) to the server, alongside the current page value. If you include the page size in the queryKey of the server data source, changes to the page size will reset the grid view and re-request pages from the server.

Sizing Pagination

Fork code on stack blitzFork code on code sandbox

The page size is a normal piece of state. The Show per page menu can be used to update the page size. When the page size is updated, the demo will reset the response cache, reset the page number, and apply the new page size.

Since the pageSize value is included in the queryKey for the server data source, the grid will reset and request a new set of rows for the new page size.

Next Steps