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

Server Row Sorting

LyteNyte Grid requests sorted rows by sending a defined sort model. The server applies the sort and returns the requested data slice.

Sort Rows On The Server

To sort rows on the server, define a sort model and include it in each data request. The useServerDataSource hook accepts a queryKey property, which is an array of dependencies for data fetching. When a value in this array changes, the data source resets and fetches a new set of rows.

Include your sort model in the queryKey dependency array. The data source passes the queryKey values to the queryFn. Your queryFn then sends the sort model to the server, which applies the sort before returning the data.

The following demo passes a custom sort model to the queryKey property. Clicking a column header updates that column’s sort state.

Server Row Sorting

Fork code on stack blitzFork code on code sandbox

The demo applies sort state to individual columns. You can use the grid’s API to update each column’s sort property. The component then derives the sort model from the column definitions and passes it to the queryKey dependency array.

const [columns, setColumns] = useState(initialColumns);
const queryFn: UseServerDataSourceParams<
GridSpec["data"],
[{ columnId: string; isDescending: boolean } | null]
>["queryFn"] = useCallback((params) => {
// Pass the sort model (queryKey[0]) to your server request
return Server(params.requests, params.queryKey[0]);
}, []);
const sort = useMemo(() => {
const columnWithSort = columns.find((x) => x.sort);
if (!columnWithSort) return null;
return { columnId: columnWithSort.id, isDescending: columnWithSort.sort === "desc" };
}, [columns]);
const ds = useServerDataSource({
queryFn,
queryKey: [sort] as const,
blockSize: 50,
});

Tip

LyteNyte Grid is unopinionated about sort model structures. Align your client-side sort model with your database schema to minimize translation logic when building server queries.

Next Steps