Skip to content

Row Grouping

Configure rowGrouping (RowGroupingOptions<T>) to organize client rows into multi-level expandable groups with an auto-generated group column.

Multi-Level Grouping & Totals
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 {
const grid = new Grid<SaleRow>({
container: stage,
columnDefs: [
{
key: 'region',
name: 'Region',
width: 105,
isSortable: true,
},
{
key: 'country',
name: 'Country',
width: 115,
isSortable: true,
},
{
key: 'quarter',
name: 'Quarter',
width: 90,
isSortable: true,
},
{
key: 'product',
name: 'Product',
width: 110,
},
{
key: 'units',
name: 'Units',
width: 85,
cellDataType: 'number',
},
{
key: 'revenue',
name: 'Revenue',
width: 120,
cellDataType: 'number',
formatter: ({ value }) =>
`$${Number(value).toLocaleString('en-US')}`,
},
],
rowData: createSales(),
getRowId: (row) => row.id,
rowGrouping: {
columns: ['region', 'country'],
autoColumn: {
name: 'Territory',
width: 200,
pinned: 'start',
},
},
valueColumns: [
{
colId: 'units',
aggFunc: 'sum',
},
{
colId: 'revenue',
aggFunc: 'sum',
},
],
groupTotals: true,
grandTotal: true,
defaultExpandedDepth: 1,
aggregateFormatter: ({ value, colId }) =>
value === null
? ''
: colId === 'revenue'
? `$${Number(value).toLocaleString('en-US')}`
: String(value),
});
const stopTheme = syncGridTheme(stage);
const updateMeta = () => {
const counts = grid.getHierarchyCounts();
if (counts)
meta.textContent = `Visible nodes: ${counts.visibleNodes} · Source rows: ${counts.sourceRows ?? 0}`;
};
grid.addEventListener('hierarchyChanged', updateMeta);
addButton(controls, 'Expand all', () => grid.expandAllGroups());
addButton(controls, 'Collapse all', () => grid.collapseAllGroups());
updateMeta();
return () => {
grid.destroy();
stopTheme();
};
}
Option Type Default Description
rowGrouping.columns readonly string[] — (required) Ordered existing colIds (never generated group/selection columns)
rowGrouping.keyGetter (params) => HierarchyKeyValue cell value Custom key extractor for objects or non-primitive values
rowGrouping.autoColumn AutoGroupColumnOptions<T> {} Configures generated group column (name, width, minWidth, maxWidth, flex, pinned, isMovable, isResizable)
defaultExpandedDepth number | 'all' 0 Initial expansion depth (0 collapsed, 'all' fully expanded)
groupTotals boolean false Appends subtotal rows inside expanded groups
grandTotal boolean false Appends a grand total row at the end of the display
  • Runtime Updates: Call grid.setRowGrouping(options | false) or grid.setHierarchyOptions(options | false) to change or disable grouping atomically.
  • Sorting & Filtering: Groups keep source encounter order unless their grouping column is sorted; leaf sorting runs within siblings. Filters match real rows and keep matching ancestor branches visible.
  • Canonical Keys: Keys use typed canonical segments (string, finite number, boolean, Date, blank bucket for null/undefined).