CxJS

TreeAdapter

import { TreeAdapter } from 'cx/ui'; Copied

TreeAdapter manages hierarchical data with parent-child relationships. It handles expand/collapse state and supports lazy loading of child nodes.

<div controller={PageController}>
  <Grid
    records={m.data}
    mod="tree"
    style="height: 300px"
    scrollable
    dataAdapter={{ type: TreeAdapter, childrenField: "entries" }}
    selection={{ type: KeySelection, bind: m.selection, keyField: "id" }}
    columns={[
      {
        header: "Name",
        field: "name",
        children: (
          <TreeNode
            expanded={m.$record.$expanded}
            leaf={m.$record.$leaf}
            level={m.$record.$level}
            text={m.$record.name}
          />
        ),
      },
    ]}
  />
</div>
Name
Documents
report.pdf
notes.txt
Images
readme.md

Click the arrows to expand or collapse nodes.

Node Fields

TreeAdapter uses special fields on each record to manage tree state. Field names can be customized via configuration (defaults shown):

FieldTypeDescription
$childrenarrayChild nodes. Customize with childrenField.
$expandedbooleanWhether the node is expanded. Customize with expandedField.
$leafbooleanWhether the node is a leaf. Leaf nodes cannot be expanded and load is not called for them. Customize with leafField.
$levelnumberDepth level in the tree (0 = root).
$loadingbooleanWhether child nodes are currently loading. Customize with loadingField.
$loadedbooleanWhether child nodes have been loaded. Customize with loadedField.

The example above uses childrenField: "entries" to store children in the entries field instead of the default $children.

Lazy Loading

For large trees, load children on demand using the onLoad callback:

dataAdapter={{
  type: TreeAdapter,
  onLoad: (context, instance, node) =>
    instance.getControllerByType(PageController).loadChildren(node),
}}

The onLoad callback should return an array of child records or a Promise that resolves to an array.

Configuration

PropertyTypeDescription
recordNamestringAlias used to expose record data. Default is $record.
keyFieldstringField used as the unique record key.
childrenFieldstringField containing child nodes. Default is $children.
expandedFieldstringField indicating expansion state. Default is $expanded.
leafFieldstringField indicating if node is a leaf. Default is $leaf.
loadingFieldstringField indicating loading state. Default is $loading.
loadedFieldstringField indicating if children are loaded. Default is $loaded.
foldersFirstbooleanDisplay folders before leaf nodes. Default is true.
hideRootNodesbooleanHide root nodes and show their children as top level. Default is false.
restoreExpandedNodesOnLoadbooleanRestore expanded state when data is reloaded.
onLoadfunctionCallback to load child nodes. Receives (context, instance, node). Should return an array or Promise.
onLoadErrorfunctionCallback when loading children fails.
immutablebooleanPrevents aliased data from being written to the parent store.
sealedbooleanPrevents child components from writing aliased data.

See also: Data Adapters, Tree Grid, TreeNode