Skip to content

Tree

A commonly used tree display component, supporting advanced features such as virtual scrolling for massive data, search filtering, and drag-and-drop sorting.

Basic Usage

Node 1
Node 2
Basic Usage

Selection and Disabling

Enable checkboxes via show-checkbox. Setting disabled: true in the data prohibits clicking and selecting that node.

Level one 1
Level two 1-1
Level two 1-2
Level three 1-2-1
Level one 2
Checkboxes and Disabled Nodes

Parents Independent of Children

Setting check-strictly=true ensures that parent and child selection statuses do not influence each other.

Level one 1
Level one 2
Independent Selection

Accordion Mode

Set accordion=true to automatically collapse peer nodes when one is expanded.

Level one 1
Level one 2
Accordion Mode

Connection Lines

Use the show-line property to clearly display the hierarchy.

Level one 1
Level two 1-1
Level two 1-2
Level three 1-2-1
Level one 2
Level two 2-1
Level two 2-2
Line Mode

Checkbox Position

Move checkboxes to the right side via checkbox-position.

Level one 1
Level one 2
Right Checkboxes

Async Lazy Loading

Lazy Root
Lazy Loading Demo

Custom Node Class Names

Important Node
Warning Node
Custom Class Names

Custom Expansion Icons

Level one 1
Level one 2
Custom Icons

Custom Node Content

Level one 1Root
Level one 2Root
Custom Content

Custom Prefix and Suffix

1Level one 1
2Level one 2
Prefix and Suffix

Virtual Scrolling (10k+ Data)

Virtual Node 0
Virtual Node 1
Virtual Node 2
Virtual Node 3
Virtual Node 4
Virtual Node 5
Virtual Node 6
Virtual Node 7
Virtual Node 8
Virtual Node 9
Virtual Node 10
Virtual Scrolling

Node Filtering

Level one 1
Level one 2
Tree Node Filtering

Draggable Nodes

Level one 1
Level one 2
Drag and Drop Demo

Multi-selection and Node Retrieval

Level one 1
Level one 2
Selection Retrieval

Custom Field Mapping

Root A
Root B
Field Mapping

Advanced Case: Massive Data Filtering and Selection

Virtual Node 0
Virtual Node 1
Virtual Node 2
Virtual Node 3
Virtual Node 4
Virtual Node 5
Virtual Node 6
Virtual Node 7
Virtual Node 8
Virtual Node 9
Virtual Node 10
Virtual Node 11
Virtual Node 12
Virtual Node 13
Comprehensive Example

Usage in Nuxt

The Tree component fully supports SSR in Nuxt 3/4. When used in a Nuxt project, it is auto-imported.

Nuxt SSR Node 1
Nuxt SSR Node 2
Usage in Nuxt

SSR Considerations:

  • ✅ Data structures are perfectly serialized.
  • ✅ Node expansion states are correctly generated in the SSR environment.
  • ✅ Virtual scrolling is automatically activated on the client.
  • ✅ Event bindings and reactive states work seamlessly.

SSR Safety

Internal virtual scrolling and DOM operations are handled to ensure no browser APIs are triggered on the server, ensuring a smooth transition upon client-side hydration.

API

Props

NameDescriptionTypeDefault
dataData sourceTreeNodeData[][]
propsConfiguration optionsobject{ label: 'label', children: 'children', disabled: 'disabled' }
node-keyAttribute field name used as unique identifier for each tree nodestring'id'
show-checkboxWhether nodes can be selectedbooleanfalse
checkbox-positionPosition of checkbox relative to label'left' | 'right''left'
default-expand-allWhether to expand all nodes by defaultbooleanfalse
default-expanded-keysArray of keys for nodes expanded by default(string | number)[][]
default-checked-keysArray of keys for nodes checked by default(string | number)[][]
current-node-keyKey of currently selected node (supports v-model)string | number-
highlight-currentWhether to highlight the currently selected nodebooleantrue
accordionWhether to allow only one peer node to expand at a timebooleanfalse
indentHorizontal indentation between adjacent levels in pixelsnumber18
check-strictlyWhether to decouple parent and child checkbox statusesbooleanfalse
expand-on-click-nodeWhether to expand/collapse on node clickbooleantrue
check-on-click-nodeWhether to check checkbox on node clickbooleanfalse
empty-textText shown when data is emptystring'No Data'
filter-methodMethod executed when filtering tree nodes(query, node) => boolean-
lazyWhether to lazy load child nodes (use with load)booleanfalse
loadMethod to load sub-tree data (only for lazy)(node) => Promise-
virtualWhether to enable virtual scrollingbooleanfalse
heightHeight of virtual scrolling window (px)number400
item-heightHeight of each node (px)number30
show-lineWhether to show connection linesbooleanfalse
draggableWhether to enable drag-and-dropbooleanfalse

Props Data Mapping

NameDescriptionTypeDefault
labelSpecifies the node label propertystring'label'
childrenSpecifies the child nodes propertystring'children'
disabledSpecifies the checkbox disabled propertystring'disabled'

TreeNodeData Definition

NameDescriptionType
keyUnique identifier (Required)string | number
labelDisplay text (Required)string
childrenArray of child nodesTreeNodeData[]
disabledWhether to disable selection/clickingboolean
isLeafWhether this is a leaf node (used in lazy load)boolean
iconCustom node icon classstring
classCustom node style classstring

Events

NameDescriptionParameters
node-clickTriggers when a node is clicked(node: TreeNode, e: MouseEvent)
node-expandTriggers when a node expands/collapses(node: TreeNode, expanded: boolean)
checkTriggers when checkbox status changes(node: TreeNode, checked: boolean, checkedKeys: (string | number)[])
current-changeTriggers when selected node changes(node: TreeNode | null)
node-drag-startTriggers when dragging starts(node: TreeNode, e: DragEvent)
node-drag-endTriggers when dragging ends(node: TreeNode, dropNode: TreeNode | null, pos: 'before' | 'after' | 'inner', e: DragEvent)
node-drag-overTriggers during dragging(node: TreeNode, e: DragEvent)
node-drag-enterTriggers when entering a node(node: TreeNode, e: DragEvent)
node-drag-leaveTriggers when leaving a node(node: TreeNode, e: DragEvent)
node-dropTriggers when dropping finishes(node: TreeNode, dropNode: TreeNode, type: 'before' | 'after' | 'inner', e: DragEvent)

Slots

NameDescriptionParameters
defaultCustom node content{ node, data }
iconCustom expansion icon{ node, data }
prefixCustom content prefix{ node, data }
suffixCustom content suffix{ node, data }
emptyContent when no data-

Note: The node object includes properties like key, label, level, expanded, checked, indeterminate, loading, isLeaf. data refers to the raw data item.

Expose

NameDescriptionParameters/Type
filterFilter tree nodes(query: string)
getNodeGet node data by key(key: string | number)
getCheckedNodesReturns array of currently checked nodes-
getCheckedKeysReturns array of currently checked keys-
getHalfCheckedNodesReturns array of currently half-checked nodes-
getHalfCheckedKeysReturns array of currently half-checked keys-
getCurrentKeyGets currently selected node's key-
getCurrentNodeGets currently selected node's data-
setCheckedSet checked status via key(key: string | number, checked: boolean, deep?: boolean)
setCheckedKeysSet checked status via keys(keys: (string | number)[])
setCheckedNodesSet checked status via nodes(nodes: TreeNodeData[])
setCurrentKeySet current selected node via key(key: string | number | undefined)
setExpandedKeysSet expanded keys(keys: (string | number)[])
setDataManually set tree data(data: TreeNodeData[])
expandNodeExpand specific node(key: string | number)
collapseNodeCollapse specific node(key: string | number)
scrollToScroll to position (virtual scroll only)(options: number | ScrollToOptions)
scrollToNodeScroll to specific node(key: string | number)
expandedKeysSet of currently expanded keysRef<Set<string | number>>
checkedKeysSet of currently checked keysRef<Set<string | number>>

Theme Variables

VariableDescriptionDefault
--yh-tree-node-heightNode height28px
--yh-tree-text-colorText colorvar(--yh-text-color-primary)
--yh-tree-hover-bgHover backgroundvar(--yh-color-primary-light-9)
--yh-tree-current-bgHighlighted node backgroundvar(--yh-color-primary-light-8)

Released under the MIT License.