TreeGrid
import { Grid, TreeNode } from 'cx/widgets'; Copied A TreeGrid is a Grid with hierarchical data displayed using the TreeAdapter and TreeNode components. It supports lazy loading, expand/collapse, and all standard Grid features.
<div controller={PageController}>
<Grid
records={m.data}
mod="tree"
style="height: 400px"
scrollable
dataAdapter={{
type: TreeAdapter,
onLoad: (context, instance, node) =>
instance.getControllerByType(PageController).generateRecords(node),
}}
selection={{ type: KeySelection, bind: m.selection, keyField: "id" }}
columns={[
{
header: "Name",
field: "name",
sortable: true,
children: (
<TreeNode
expanded={m.$record.$expanded}
leaf={m.$record.$leaf}
level={m.$record.$level}
loading={m.$record.$loading}
text={m.$record.name}
/>
),
},
{ header: "Phone", field: "phone" },
{ header: "City", field: "city", sortable: true },
]}
/>
</div> | Name | Phone | City |
|---|---|---|
| 555-1002 | Los Angeles | |
| 555-1005 | Phoenix | |
| 555-1001 | New York | |
| 555-1003 | Chicago | |
| 555-1004 | Houston |
Click the arrows to expand/collapse nodes. The tree loads child nodes lazily when expanded.
TreeAdapter
To display hierarchical data, configure the dataAdapter property with TreeAdapter:
<Grid
records={m.data}
mod="tree"
dataAdapter={{ type: TreeAdapter }}
columns={columns}
/>
If your data is already a tree structure with children nested in each record, that’s all you need. The TreeAdapter will flatten the tree for display and manage expand/collapse state.
For lazy loading, implement the onLoad callback to fetch children when a node is expanded:
dataAdapter={{
type: TreeAdapter,
onLoad: (context, instance, node) =>
instance.getControllerByType(PageController).loadChildren(node),
}}
Return an array of child records or a Promise that resolves to an array.
TreeNode
Use TreeNode in the column’s children to render the expand/collapse arrow and indentation:
{
header: "Name",
field: "name",
children: (
<TreeNode
expanded={m.$record.$expanded}
leaf={m.$record.$leaf}
level={m.$record.$level}
loading={m.$record.$loading}
text={m.$record.name}
/>
)
}
The TreeAdapter manages special properties on each record ($expanded, $leaf, $level, $loading) that TreeNode uses for rendering.
See also: TreeNode, Stateful TreeGrid, Searching Tree Grids