LyteNyte Grid logo for light mode. Links back to the documentation home page.
Tree Data

Tree Data

Pass a nested JavaScript object to the tree data source to create a hierarchical grid layout.

Data Object

Use the useTreeDataSource hook to render a nested object as a row set. The data property must be a JavaScript object. Pass these functions to control how the hook creates rows from the source object:

  • rowValueFn: Returns the data associated with a specific row.
  • rowChildrenFn: Returns an array of objects used to generate child rows.

The demo below uses objects with a children key to define the hierarchy.

Custom Keys

Fork code on stack blitzFork code on code sandbox

Root Rows

Define rowRootFn to specify the initial root rows. This function is required when using a wrapper object where the top-level property should not appear as a row.

The following demo uses rowRootFn to bypass a root property and return the nested contents as the starting rows.

Root Rows

Fork code on stack blitzFork code on code sandbox

In this example, the grid ignores the top-level root property in the data. The rowRootFn function returns the children property of the root object as an array of entries. Each entry becomes a row, and the grid branches from there.

const ds = useTreeDataSource({
data: data,
rowGroupDefaultExpansion: true,
rowRootFn: (x) => {
return x.children.map((row) => [row.name, row]);
},
rowChildrenFn: (x) => {
if (!x.children) return [];
return x.children.map((r: any) => [r.name, r]);
},
rowValueFn: (x) => ({
name: x.name,
kind: x.kind,
size: x.size || null,
modified: x.modified,
lastEditedBy: x.lastEditedBy,
permissions: x.permissions,
}),
});

Next Steps

  • Tree Filtering: Remove tree data rows by providing a custom filter function.
  • Tree Sorting: Sort tree rows in ascending or descending order.
  • Tree Row Pinning: Freeze rows to the top or bottom of the viewport with the tree source.