Skip to content

Tree Data

Use treeData (mutually exclusive with rowGrouping) when rows already form a hierarchy via path arrays or nested children.

Path-Based Tree Data & Rollups
import { Grid } from 'next-grid-js';
import { EMPLOYEES, type EmployeeRow } from './data/employees';
import { syncGridTheme } from './theme';
import type { DemoMountContext } from './types';
import { addButton } from './ui';
export function mount({ stage, controls }: DemoMountContext): () => void {
const grid = new Grid<EmployeeRow>({
container: stage,
columnDefs: [
{
key: 'name',
name: 'Name',
width: 165,
},
{
key: 'title',
name: 'Role',
width: 175,
flex: 1,
},
{
key: 'salary',
name: 'Salary',
width: 120,
cellDataType: 'number',
formatter: ({ value }) =>
`$${Number(value).toLocaleString('en-US')}`,
},
],
rowData: [...EMPLOYEES],
getRowId: (row) => row.id,
treeData: {
getDataPath: (row) => row.path,
autoColumn: {
name: 'Department',
width: 210,
pinned: 'start',
},
},
valueColumns: [
{
colId: 'salary',
aggFunc: 'sum',
},
],
defaultExpandedDepth: 'all',
aggregateFormatter: ({ value }) =>
value === null ? '' : `$${Number(value).toLocaleString('en-US')}`,
});
const stopTheme = syncGridTheme(stage);
addButton(controls, 'Expand all', () => grid.expandAllGroups());
addButton(controls, 'Collapse all', () => grid.collapseAllGroups());
return () => {
grid.destroy();
stopTheme();
};
}
  • Path Mode (getDataPath): Return a non-empty string[] from each row (getDataPath: (row) => row.path). Missing intermediate ancestor paths become synthetic filler nodes; real data rows can occupy internal parent nodes as well as leaves.
  • Nested Mode (getChildren): Return readonly T[] | undefined (getChildren: (row) => row.children) plus getTreeKey (or stable getRowId/rowKey).
  • Real Parents vs. Fillers: Every real node contributes once to aggregates and source row counts; synthetic fillers never double-count.
  • Cycles & Duplicates: Cycles, repeated membership, and duplicate keys/paths reject atomically.
  • Export: Exporting tree data includes real parent rows alongside leaf rows (unlike rowGrouping, which exports leaves with group paths).