TreeView

Tree view component with better performance and customizability.
Import

When To Use#

More basic Tree component, allowing each of its parts to be defined in the template, and state to be managed manually.

⚠️ nzTreeControl has been removed in v21.0.0. Please use either nzLevelAccessor or nzChildrenAccessor instead, one of them must exist.

Examples#

parent 1 parent 1-0 leaf leaf parent 1-1 leaf

Use nzLevelAccessor for tree view basic usage, including select, disable and expand features.

      Loading...
    

Directory tree.

      Loading...
    

To load data asynchronously when click to expand a treeNode.

      Loading...
    

Use virtual scroll.

      Loading...
    
parent 1 parent 1-0 leaf leaf parent 1-1 leaf

Use nzChildrenAccessor for tree view basic usage, including select, disable and expand features.

      Loading...
    

Tree with checkboxes.

      Loading...
    
parent 1 parent 1-0 leaf leaf parent 1-1 leaf parent 2 leaf

Tree with add and delete actions.

      Loading...
    
Show Leaf Icon:
parent 1 parent 1-0 leaf leaf parent 1-1 leaf parent 1-1-0 leaf leaf leaf parent 2 leaf leaf

Tree with connected line between nodes.

      Loading...
    

API#

nz-tree-view#

PropertyDescriptionTypeDefault
[nzDataSource]The data array to renderDataSource<T> | Observable<T[]> | T[], DataSource-
[nzLevelAccessor]The level accessor of tree node (levelAccessor), used with flat data.(dataNode: T) => number-
[nzChildrenAccessor]The children accessor of tree node (childrenAccessor), used with nested data.(dataNode: T) => T[]-
[nzTrackBy]Tracking function that will be used to check the differences in data changes. Used similarly to ngFor trackBy function.TrackByFunction(_index, dataNode: T) => dataNode
[nzDirectoryTree]Whether nodes are displayed as directory stylebooleanfalse
[nzBlockNode]Whether tree nodes fill remaining horizontal spacebooleanfalse

nz-tree-virtual-scroll-view#

The virtual scroll tree view, which can be accessed from the CdkVirtualScrollViewport instance through the virtualScrollViewport member of the component instance.

PropertyDescriptionTypeDefault
[nzDataSource]The data array to renderDataSource<T> | Observable<T[]> | T[], DataSource-
[nzLevelAccessor]The level accessor of tree node (levelAccessor), used with flat data.(dataNode: T) => number-
[nzChildrenAccessor]The children accessor of tree node (childrenAccessor), used with nested data.(dataNode: T) => T[]-
[nzTrackBy]Tracking function that will be used to check the differences in data changes. Used similarly to ngFor trackBy function.TrackByFunction(_index, dataNode: T) => dataNode
[nzDirectoryTree]Whether nodes are displayed as directory stylebooleanfalse
[nzBlockNode]Whether tree nodes fill remaining horizontal spacebooleanfalse
[nzItemSize]The size of nodes in the tree (in pixels)number28
[nzMinBufferPx]The minimum amount of buffer rendered allowed outside the viewport (in pixels)number28 * 5
[nzMaxBufferPx]The amount of buffer required for rendering new nodes (in pixels)number28 * 10

[nzTreeNodeDef]#

Directive to define nz-tree-node.

PropertyDescriptionTypeDefault
[nzTreeNodeDefWhen]A matching function which indicates whether inputted node should be used. It matches the very first node that makes this function return true. If no nodes that makes this function return true, the node which does not define this function would be matched instead.(index: number, nodeData: T) => boolean-

nz-tree-node#

The tree node container component, which needs to be defined by the nzTreeNodeDef directive.

PropertyDescriptionTypeDefault
[nzExpandable]Whether or not this tree node is expandable. Please set this parameter value when using nzLevelAccessor or nzChildrenAccessor.booleanfalse

[nzTreeNodePadding]#

Show node indentation by adding paddingBest Performance.

<nz-tree-node nzTreeNodePadding></nz-tree-node>

nzTreeNodeIndentLine#

Show node indentation by adding indent lines.

<nz-tree-node nzTreeNodeIndentLine></nz-tree-node>

nz-tree-node-toggle#

A toggle which is used to expand / collapse the node.

PropertyDescriptionTypeDefault
[nzTreeNodeToggleRecursive]Is it recursively expand / collapsebooleanfalse

nz-tree-node-toggle[nzTreeNodeNoopToggle]#

A toggle that does no actions. This can be used for placeholders or displays icons.

[nzTreeNodeToggleRotateIcon]#

Define an icon in the toggle, which it will automatically rotate depending on the collapse/expand state.

[nzTreeNodeToggleActiveIcon]#

Define an icon in the toggle for an active style, which it can be used for the loading state.

nz-tree-node-option#

Define the selectable feature of a node.

PropertyDescriptionTypeDefault
[nzSelected]Whether the option is selected.booleanfalse
[nzDisabled]Whether the option is disabled.booleanfalse
(nzClick)Event on click.EventEmitter<MouseEvent>-

nz-tree-node-checkbox#

Define the checkbox feature of a node.

PropertyDescriptionTypeDefault
[nzChecked]Whether the checkbox is checked.booleanfalse
[nzDisabled]Whether the checkbox is disabled.booleanfalse
[nzIndeterminate]Whether the checkbox is indeterminate.booleanfalse
(nzClick)Event on click.EventEmitter<MouseEvent>-

Classes#

NzTreeViewFlatDataSource extends DataSource#

The dataSource for flat data that automatically responds to view and data changes, used with nzLevelAccessor.

Construction Parameters

NameDescription
tree: NzTreeView<T, F>Tree View component instance.
treeFlattener: NzTreeFlattener<T, F>Flattener for convert nested nodes <T> into flattened nodes <F>.
initialData: T[] = []Initialized data <T>.

Methods

NameDescription
connect(collectionViewer: CollectionViewer): Observable<F[]>Called in the TreeView component, emit flattenData to the TreeView
disconnect(): voidCall when TreeView component is destroyed.
setData(value: T[]): voidSet the origin data <T>.
getData(): T[]Get the origin data <T>.
setFlattenedData(nodes: F[]): voidSet the flattened data <F>.
getFlattenData(): F[]Get the flattened data <F>.

NzTreeViewNestedDataSource extends DataSource#

The dataSource for nested data that automatically responds to view and data changes, used with nzChildrenAccessor.

Construction Parameters

NameDescription
tree: NzTreeView<T>Tree View component instance.
initialData: T[] = []Initialized data <T>.

Methods

NameDescription
connect(collectionViewer: CollectionViewer): Observable<T[]>Called in the TreeView component, emit data to the TreeView
disconnect(): voidCall when TreeView component is destroyed.
setData(value: T[]): voidSet the origin data <T>
getData(): T[]Get the origin data <T>

NzTreeFlattener#

Convert nested data with child nodes into node data with level information.

Construction Parameters

NameDescription
transformFunction: (node: T, level: number) => FReceive a nested node <T> and return a flattened node <F>.
getLevel: (node: F) => numberDefine the method to get the level property.
isExpandable: (node: F) => booleanMethods for defining whether a node is expandable.
getChildren: (node: T) => Observable<T[]> | T[] | undefined | nullDefine methods to get children nodes from nested node <T>.

Methods

NameDescription
flattenNodes(structuredData: T[]): F[]Receive nested data <T> and return flattened data <F>.

utils#

Common utility classes in the Tree View component. Provides methods for getting the father, brother, and descendant nodes of the current tree node, including two sets of methods for flat and nested data structures respectively.

Flat Data

NameDescription
getParent: (nodes: F[], node: F, getLevel: (dataNode: F) => number): F | nullReturn parent of the node.
getDescendants: (nodes: F[], node: F, getLevel: (dataNode: F) => number): F[]Return descendants of the node.
getNextSibling: (nodes: F[], node: F, getLevel: (dataNode: F) => number, _index?: number): F | nullReturn the next sibling of the node, or the first sibling of the node after _index position.

Nested Data

NameDescription
getParentForNestedData: (nodes: T[], node: T, getChildren: (dataNode: T) => T[]): T | nullReturn parent of the node.
getDescendantsForNestedData: (node: T, getChildren: (dataNode: T) => T[]): T[]Return descendants of the node.
getNextSiblingForNestedData: (nodes: T[], node: T, getChildren: (dataNode: T) => T[]): T | nullReturn the next sibling of the node.
flattenNestedNodes: (nodes: T[], getChildren: (dataNode: T) => T[]): T[]Flatten nested data and returns flat arrays, but does not change the nested node properties.