Skip to content

Server Pivot

Configure rowModelType: 'serverSide' alongside pivot to let your backend compute pivot schemas, totals, and aggregated result blocks.

Server-Side Pivot Matrix
import {
Grid,
encodeHierarchyKey,
type RemoteDatasource,
} from 'next-grid-js';
import type { SaleRow } from './data/sales';
import { syncGridTheme } from './theme';
import type { DemoMountContext } from './types';
export function mount({ stage, meta }: DemoMountContext): () => void {
const datasource: RemoteDatasource<SaleRow> = {
getRows: async (request) => {
await new Promise((r) => setTimeout(r, 140));
const pivot = request.pivot;
if (!pivot) throw new Error('Expected pivot query');
const schemaKey = JSON.stringify([
'pivot:1',
pivot.rowGroupColumns,
pivot.pivotColumns,
pivot.measures.map(({ measureId, colId, aggFunc }) => [
measureId,
colId,
aggFunc,
]),
]);
const measureId = pivot.measures[0].measureId;
const quarters = ['Q1', 'Q2', 'Q3', 'Q4'] as const;
const schema = quarters.map((q) => {
const pivotPath = [encodeHierarchyKey(q)];
const colId = JSON.stringify([
'pivotColumn',
schemaKey,
pivotPath,
measureId,
'none',
]);
return {
colId,
key: {
schemaKey,
pivotPath,
measureId,
totalKind: 'none' as const,
},
name: q,
};
});
const regions = [
{
name: 'EMEA',
vals: [18400, 21200, 19800, 24600],
},
{
name: 'AMER',
vals: [22100, 23900, 25400, 28100],
},
{
name: 'APAC',
vals: [14200, 16500, 17900, 20300],
},
];
const nodes = regions
.slice(request.startRow, request.endRow)
.map((r) => {
const values: Record<string, number> = {};
schema.forEach((col, i) => {
values[col.colId] = r.vals[i];
});
return {
groupPath: [encodeHierarchyKey(r.name)],
childCount: {
kind: 'final' as const,
count: 0,
},
status: 'complete' as const,
values,
};
});
return {
kind: 'pivot',
startRow: request.startRow,
groupPath: request.groupKeys,
schemaKey,
schemaRevision: 'schema-1',
aggregateRevision: 'facts-1',
schema,
rowCount: {
kind: 'final',
count: regions.length,
},
nodes,
};
},
};
const grid = new Grid<SaleRow>({
container: stage,
columnDefs: [
{
key: 'region',
name: 'Region',
width: 120,
},
{
key: 'quarter',
name: 'Quarter',
width: 95,
},
{
key: 'revenue',
name: 'Revenue',
width: 120,
cellDataType: 'number',
},
],
rowModelType: 'serverSide',
datasource,
getRowId: (row) => row.id,
pivot: {
rowGroupColumns: ['region'],
pivotColumns: ['quarter'],
measures: [
{
measureId: 'revenue',
colId: 'revenue',
aggFunc: 'sum',
},
],
},
pivotGroupColumn: {
name: 'Region',
width: 165,
pinned: 'start',
},
pivotValueFormatter: ({ value }) =>
value === null ? '—' : `$${Number(value).toLocaleString('en-US')}`,
});
const stopTheme = syncGridTheme(stage);
grid.addEventListener('pivotChanged', () => {
const counts = grid.getServerPivotCounts();
const req = grid.getPivotRequestState();
if (counts)
meta.textContent = `Remote cols: ${counts.resultColumns} · Request: ${req.status}`;
});
return () => {
grid.destroy();
stopTheme();
};
}
  • Request: RemoteDatasource.getRows(request) receives request.pivot, request.rowGroupCols, request.groupKeys, and [startRow, endRow).
  • Response (RemotePivotResult): Return kind: 'pivot' with startRow, groupPath, schemaKey, schemaRevision, aggregateRevision, schema (required on first root response), rowCount, nodes, and optional totals.
  • Monitoring & Recovery: Inspect grid.getServerPivotCounts() and grid.getPivotRequestState() ('idle' | 'pending' | 'error'). Call grid.retryFailedRows() after transient failures or grid.refreshRows() on schema/aggregate revision conflicts.