Server-Side Row Model
rowModelType: 'serverSide' shares the block cache and RemoteDatasource protocol with 'infinite', and adds support for lazy server-side row grouping and server-side pivot.
import { Grid, type ColumnDef } from 'next-grid-js';import { createFakeRemoteDatasource } from './data/fake-server';import type { OrderRow } from './data/orders';import { syncGridTheme } from './theme';import type { DemoMountContext } from './types';import { addButton } from './ui';
export function mount({ stage, controls, meta,}: DemoMountContext): () => void { const { datasource } = createFakeRemoteDatasource(180);
const columnDefs: readonly ColumnDef<OrderRow>[] = [ { key: 'id', name: 'ID', width: 96, pinned: 'start', isSortable: true, }, { key: 'customer', name: 'Customer', width: 175, flex: 1, isSortable: true, }, { key: 'region', name: 'Region', width: 105, isSortable: true, }, { key: 'amount', name: 'Amount', width: 115, cellDataType: 'number', isSortable: true, formatter: ({ value }) => `$${Number(value).toLocaleString('en-US')}`, }, { key: 'status', name: 'Status', width: 105, }, ];
const grid = new Grid<OrderRow>({ container: stage, columnDefs, rowModelType: 'serverSide', datasource, remoteOptions: { blockSize: 30, maxBlocks: 6, context: { regionFilter: 'all' }, }, getRowId: (row) => row.id, }); const stopTheme = syncGridTheme(stage);
let ctxSeq = 1; addButton(controls, 'Update context', () => { ctxSeq += 1; grid.setDatasourceContext({ revision: ctxSeq }); });
grid.addEventListener('datasourceChanged', ({ data }) => { if (data.rowModelType !== 'viewport') { meta.textContent = `Gen: ${data.generation} · Loaded: ${data.loadedRows} · Pending: ${data.pendingRequests}`; } });
return () => { grid.destroy(); stopTheme(); };}Key Differences from 'infinite'
Section titled “Key Differences from 'infinite'”| Capability | 'infinite' |
'serverSide' |
|---|---|---|
| Flat block caching & prefetch | ✓ | ✓ |
setRemoteDatasource / setDatasourceContext |
✓ | ✓ |
Lazy server row grouping (rowGrouping) |
✗ | ✓ |
Server-side pivot (pivot) |
✗ | ✓ |
Updating Query Context & Swapping Datasources
Section titled “Updating Query Context & Swapping Datasources”grid.setDatasourceContext(context): Updatesrequest.context, bumps the datasource generation, aborts obsolete in-flight requests, and reloads visible blocks.grid.setRemoteDatasource(nextDatasource): Replaces the active datasource cleanly and destroys the previous one ifdestroy()is implemented.