Skip to content

Server-Side Grouping

Combine rowModelType: 'serverSide' with rowGrouping to fetch top-level group summaries first and lazy-load child branches or leaf rows only when expanded.

Lazy Server-Side Row Grouping
import {
Grid,
decodeHierarchyKey,
encodeHierarchyKey,
type RemoteDatasource,
} from 'next-grid-js';
import { createSales, type SaleRow } from './data/sales';
import { syncGridTheme } from './theme';
import type { DemoMountContext } from './types';
export function mount({ stage, meta }: DemoMountContext): () => void {
const allSales = createSales();
const datasource: RemoteDatasource<SaleRow> = {
getRows: async (request) => {
await new Promise((r) => setTimeout(r, 140));
if (request.groupKeys.length === 0) {
const regions = [...new Set(allSales.map((s) => s.region))];
const nodes = regions
.slice(request.startRow, request.endRow)
.map((region) => {
const matching = allSales.filter((s) => s.region === region);
return {
kind: 'group' as const,
key: encodeHierarchyKey(region),
label: region,
childCount: {
kind: 'final' as const,
count: matching.length,
},
childAggregate: {
revision: 'v1',
status: 'complete' as const,
values: {
revenue: matching.reduce(
(sum, r) => sum + r.revenue,
0,
),
},
},
};
});
return {
startRow: request.startRow,
rowCount: {
kind: 'final',
count: regions.length,
},
nodes,
};
}
const region = String(decodeHierarchyKey(request.groupKeys[0]));
const children = allSales.filter((s) => s.region === region);
const nodes = children
.slice(request.startRow, request.endRow)
.map((row) => ({
kind: 'leaf' as const,
data: row,
}));
return {
startRow: request.startRow,
rowCount: {
kind: 'final',
count: children.length,
},
nodes,
};
},
};
const grid = new Grid<SaleRow>({
container: stage,
columnDefs: [
{
key: 'region',
name: 'Region',
width: 105,
isVisible: false,
},
{
key: 'country',
name: 'Country',
width: 120,
},
{
key: 'quarter',
name: 'Quarter',
width: 95,
},
{
key: 'product',
name: 'Product',
width: 110,
flex: 1,
},
{
key: 'revenue',
name: 'Revenue',
width: 125,
cellDataType: 'number',
formatter: ({ value }) =>
`$${Number(value).toLocaleString('en-US')}`,
},
],
rowModelType: 'serverSide',
datasource,
getRowId: (row) => row.id,
rowGrouping: {
columns: ['region'],
autoColumn: {
name: 'Region',
width: 170,
pinned: 'start',
},
},
valueColumns: [
{
colId: 'revenue',
aggFunc: 'sum',
},
],
aggregateFormatter: ({ value }) =>
value === null ? '' : `$${Number(value).toLocaleString('en-US')}`,
});
const stopTheme = syncGridTheme(stage);
meta.textContent = 'Expand a region to lazy-load its child rows';
return () => {
grid.destroy();
stopTheme();
};
}
  • Request: RemoteDatasource.getRows(request) receives request.rowGroupCols, request.valueCols, request.groupKeys (encoded canonical path of the expanded parent, [] for root), and [startRow, endRow).
  • Response (RemoteHierarchyResult<T>): Return { startRow, rowCount, nodes, parentAggregate? } where each node is either { kind: 'group', key, label, childCount, childAggregate? } (using encodeHierarchyKey(...) for key) or { kind: 'leaf', data }.
  • Metadata Limit: remoteOptions.maxHierarchyMetadataEntries defaults to 10,000 and can be adjusted at runtime via grid.setHierarchyMetadataLimit(limit).
  • Error Recovery: Failed child group fetches surface on the branch without losing sibling expansion; call grid.retryFailedRows() to retry.