Configure the grid to display structured data by providing self-referential records where each record contains a reference to the id of its parent. This is the most performant way to provide and update hierarchical data to the grid, as the rowData is processed in a single pass.
Providing Hierarchy Copy Link
Each row in the data can contain a field containing the id of its parent row. The treeDataParentIdField property is used to specify the field containing the parent row id.
The below structure demonstrates a simple hierarchy, wherein the treeDataParentIdField grid option would specify "parentId" as the field containing the parent row id:
const data = [
{ id: 'A' },
{ id: 'B', parentId: 'A' },
{ id: 'C', parentId: 'A' },
{ id: 'D' },
{ id: 'E', parentId: 'D' },
{ id: 'F', parentId: 'E' },
]In the above hierarchy, the 'A' row is the parent of 'B' and 'C'. The 'D' row is the parent of 'E' which is the parent of 'F'.
getRowId callback must be provided when using treeDataParentIdField to ensure each row has a unique identifier that can be correctly referenced.
Ensure your data is correctly structured, as cycles and missing parent rows are not allowed.
For example, the following data structure would not be valid as the 'A' row is missing:
const data = [
{ id: 'B', parentId: 'A' },
{ id: 'C', parentId: 'A' },
]The following data structure would not be valid as it creates a cycle:
const data = [
{ id: 'A', parentId: 'B' },
{ id: 'B', parentId: 'A' },
] Providing Group Values Copy Link
When providing a self-referential hierarchy, the grid will use the row ID as the group value by default. To provide a custom value, the field property of the autoGroupColumnDef grid option can be used.
The example below demonstrates a case where the autoGroupColumnDef field is set to name to display a group value:
import {
ClientSideRowModelModule,
GridApi,
GridOptions,
ModuleRegistry,
createGrid,
enableDevValidations,
} from "ag-grid-community";
import { TreeDataModule } from "ag-grid-enterprise";
import { getData } from "./data";
// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
enableDevValidations();
}
ModuleRegistry.registerModules([ClientSideRowModelModule, TreeDataModule]);
let gridApi: GridApi;
const gridOptions: GridOptions = {
columnDefs: [
{
field: "modified",
},
{
field: "created",
},
],
defaultColDef: {
flex: 1,
},
autoGroupColumnDef: {
headerName: "Name",
field: "name",
cellRendererParams: {
suppressCount: true,
},
},
rowData: getData(),
getRowId: (params) => params.data.id, // This is required
treeData: true, // enable Tree Data mode
treeDataParentIdField: "parentId", // the field that points to the parent row id
groupDefaultExpanded: -1, // expand all groups by default
};
// wait for the document to be loaded, otherwise
// AG Grid will not find the div in the document.
// lookup the container we want the Grid to use
const eGridDiv = document.querySelector<HTMLElement>("#myGrid")!;
// create the grid passing in the div to use together with the columns & data we want to use
gridApi = createGrid(eGridDiv, gridOptions);
const data = [
{
id: '1',
name: 'Desktop',
type: 'folder',
size: 2097152,
items: 1,
},
{
id: '2',
parentId: '1',
name: 'ProjectAlpha',
type: 'folder',
size: 1572864,
},
{
id: '3',
parentId: '2',
name: 'Proposal.docx',
size: 512000,
created: '2023-07-10',
modified: '2023-08-01',
items: 1,
},
{
id: '4',
parentId: '2',
name: 'Timeline.xlsx',
size: 1048576,
created: '2023-07-12',
modified: '2023-08-03',
items: 1,
},
{
id: '5',
parentId: '1',
name: 'ToDoList.txt',
size: 51200,
created: '2023-08-05',
modified: '2023-08-10',
items: 1,
},
{
id: '6',
parentId: '1',
name: 'MeetingNotes_August.pdf',
size: 460800,
created: '2023-08-15',
modified: '2023-08-15',
items: 1,
},
];
export function getData() {
return data;
}
<div id="myGrid" style="height: 100%"></div>
The following snippet demonstrates how to provide nested siblings with a custom group value:
const gridOptions = {
treeData: true,
treeDataParentIdField: 'parentId',
getRowId: (params) => params.data.id,
autoGroupColumnDef: {
field: 'name',
},
// other grid options ...
} Supplied vs Aggregated Copy Link
When using Tree Data, columns defined with an aggregation function will always perform aggregations on the group nodes. This means any supplied group data will be ignored in favour of the aggregated values.
import {
ClientSideRowModelModule,
GridApi,
GridOptions,
ModuleRegistry,
createGrid,
enableDevValidations,
} from "ag-grid-community";
import { TreeDataModule } from "ag-grid-enterprise";
import { getData } from "./data";
// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
enableDevValidations();
}
ModuleRegistry.registerModules([ClientSideRowModelModule, TreeDataModule]);
let gridApi: GridApi;
const gridOptions: GridOptions = {
columnDefs: [
{
headerName: "Aggregated (Sum)",
aggFunc: "sum",
field: "items",
},
{
headerName: "Provided",
field: "items",
},
],
defaultColDef: {
flex: 1,
},
autoGroupColumnDef: {
headerName: "Name",
field: "name",
cellRendererParams: {
suppressCount: true,
},
},
rowData: getData(),
getRowId: (params) => params.data.id, // This is required
treeData: true, // enable Tree Data mode
treeDataParentIdField: "parentId", // the field that points to the parent row id
groupDefaultExpanded: -1, // expand all groups by default
};
// wait for the document to be loaded, otherwise
// AG Grid will not find the div in the document.
// lookup the container we want the Grid to use
const eGridDiv = document.querySelector<HTMLElement>("#myGrid")!;
// create the grid passing in the div to use together with the columns & data we want to use
gridApi = createGrid(eGridDiv, gridOptions);
const data = [
{
id: '1',
name: 'Desktop',
type: 'folder',
size: 2097152,
items: 1,
},
{
id: '2',
parentId: '1',
name: 'ProjectAlpha',
type: 'folder',
size: 1572864,
},
{
id: '3',
parentId: '2',
name: 'Proposal.docx',
size: 512000,
created: '2023-07-10',
modified: '2023-08-01',
items: 1,
},
{
id: '4',
parentId: '2',
name: 'Timeline.xlsx',
size: 1048576,
created: '2023-07-12',
modified: '2023-08-03',
items: 1,
},
{
id: '5',
parentId: '1',
name: 'ToDoList.txt',
size: 51200,
created: '2023-08-05',
modified: '2023-08-10',
items: 1,
},
{
id: '6',
parentId: '1',
name: 'MeetingNotes_August.pdf',
size: 460800,
created: '2023-08-15',
modified: '2023-08-15',
items: 1,
},
];
export function getData() {
return data;
}
<div id="myGrid" style="height: 100%"></div>
The example above uses the configuration below to demonstrate the Desktop row is being aggregated to show the sum of its children (4), rather than the provided value (1), despite both columns showing the same field:
const gridOptions = {
treeData: true,
columnDefs: [
{
headerName: 'Aggregated (Sum)',
aggFunc: 'sum',
field: 'items',
},
{
headerName: 'Provided',
field: 'items',
},
],
};Refer to the Aggregation page for more details, and Editing Groups for editing aggregated values with cascading updates to children.