Skip to content

Client Pivot

Pivot turns source records into collapsible row groups and generated measure columns across pivotColumns. Pivot is supported on client rowData and serverSide datasources (and is incompatible with treeData, rowSource, infinite, and viewport).

Client-Side Pivot & Toggle
import { Grid } from 'next-grid-js';
import { createSales, type SaleRow } from './data/sales';
import { syncGridTheme } from './theme';
import type { DemoMountContext } from './types';
import { addButton } from './ui';
export function mount({
stage,
controls,
meta,
}: DemoMountContext): () => void {
let pivotEnabled = true;
const pivotConfig = {
rowGroupColumns: ['region', 'country'],
pivotColumns: ['quarter'],
measures: [
{
measureId: 'revenue',
colId: 'revenue',
aggFunc: 'sum',
},
],
rowTotals: true,
columnTotals: true,
};
const grid = new Grid<SaleRow>({
container: stage,
columnDefs: [
{
key: 'region',
name: 'Region',
width: 100,
},
{
key: 'country',
name: 'Country',
width: 110,
},
{
key: 'quarter',
name: 'Quarter',
width: 90,
},
{
key: 'revenue',
name: 'Revenue',
width: 115,
cellDataType: 'number',
},
],
rowData: createSales(),
getRowId: (row) => row.id,
pivot: pivotConfig,
pivotGroupColumn: {
name: 'Territory',
width: 185,
pinned: 'start',
},
pivotValueFormatter: ({ value }) =>
value === null ? '—' : `$${Number(value).toLocaleString('en-US')}`,
});
const stopTheme = syncGridTheme(stage);
const updateMeta = () => {
const counts = grid.getPivotCounts();
meta.textContent = counts
? `Result rows: ${counts.visibleResultRows} · Result cols: ${counts.resultColumns}`
: 'Source view';
};
grid.addEventListener('pivotChanged', updateMeta);
const toggleBtn = addButton(controls, 'Exit pivot', () => {
pivotEnabled = !pivotEnabled;
grid.setPivot(pivotEnabled ? pivotConfig : false);
toggleBtn.textContent = pivotEnabled ? 'Exit pivot' : 'Enable pivot';
});
addButton(controls, 'Expand all', () => grid.expandAllGroups());
updateMeta();
return () => {
grid.destroy();
stopTheme();
};
}
grid.setPivot({
rowGroupColumns: ['region', 'country'],
pivotColumns: ['quarter'],
measures: [{ measureId: 'revenue', colId: 'revenue', aggFunc: 'sum' }],
aggregationScope: 'filtered', // 'filtered' | 'all'
rowTotals: true,
columnTotals: true,
groupTotals: false,
});
// Return to source view (restores suspended source columns, sort, selection, focus)
grid.setPivot(false);
  • Axis & Presentation Options: Customize pivot aggregation with pivotAggregateFunctions, pivotKeyGetter, pivotValueFormatter, and pivotGroupColumn.
  • Filters & Sorting: Filters and quickFilter apply to source rows before aggregation; column sorting targets generated result columns and sorts siblings.
  • State Persistence: GridState v5 saves both active pivot configuration/expansion and the suspended source view so exiting pivot restores prior layout and selection.