Managing the tree expand/collapse state
By default, the tree will be rendered with all nodes expanded. This is fine for basic use cases, but as soon as you go into more complex scenarios, you will want to control which nodes are expanded or collapsed.
This is easy to achieve via the
defaultTreeExpandState prop. This is an uncontrolled prop and allows you to initially specify the expand/collapse state of the tree - all subsequent user updates will result in the tree state being updated to match the UI actions.#
Specifying an initial tree expand state
loading demo…
Understanding the tree expand state#
You can specify the expand/collapse state of the tree in two ways:
- With node paths (recommended)
When using node paths, the object should have the following properties:
defaultExpanded:boolean- whether the tree nodes are expanded by default or not.collapsedPaths:string[]- whendefaultExpandedistrue, this is a mandatory prop.expandedPaths:string[]- whendefaultExpandedisfalse, this is a mandatory prop.
const treeExpandState = {
defaultExpanded: true,
collapsedPaths: [
['1', '10'],
['2', '20'],
['5']
],
expandedPaths: [
['1', '4'],
['5','nested node in 5'],
],
};- With node ids
When using node ids, the object should have the following properties:
defaultExpanded:boolean- whether the tree nodes are expanded by default or not.collapsedIds:string[]- whendefaultExpandedistrue, this is a mandatory prop.expandedIds:string[]- whendefaultExpandedisfalse, this is a mandatory prop.
const treeExpandState = {
defaultExpanded: true,
collapsedIds: ['1', '2', '5'],
expandedIds: ['10', '20', 'nested node in 5'],
};Reacting to user actions#
You can listen to the user interactions with the tree by using the
(treeExpandStateChange) output. It emits the new tree state whenever the user expands or collapses a node.In addition to this output, you can also use the following:
(nodeExpand)- emits the path of the expanded node(nodeCollapse)- emits the path of the collapsed node
When you also need the second argument of these callbacks (
{dataSourceApi, nodePath, nodeState} and {dataSourceApi}), bind a function to the input of the same name instead: [onTreeExpandStateChange]="onTreeExpandStateChange", [onNodeExpand], [onNodeCollapse].The
(nodeExpand) and (nodeCollapse) callbacks are called when a node is expanded or collapsed, respectively - either via user interaction or by an API call. However, they will not be called when the expandAll or collapseAll methods are called.Using controlled expand/collapse state#
If you want maximum control over the collapse/expand state, you should use the controlled
[(treeExpandState)]model prop.This will allow you to own the collapse/expand state entirely.
[(treeExpandState)]model is a model: bind it two-way to a signal, and the grid writes the new state back to it after user actions or API calls. To react to the change, use the (treeExpandStateChange) output.#
Using controlled expand/collapse state
loading demo…
When using controlled
[(treeExpandState)]model, you no longer need to use API calls.When you need to expand all nodes, simply set the
[(treeExpandState)]model to {defaultExpanded: true, collapsedPaths: []}.When you need to collapse all nodes, simply set the
[(treeExpandState)]model to {defaultExpanded: false, expandedPaths: []}.