Tree
Stable keyed nodes, linked checking, per-node lazy loading and path lookup without mutating application data.
Import
With uni-app easycom, use hui-tree。 Standalone Vue imports @nscode/handy-uni/web HuiTree or installs the preview plugin.
Examples
Interaction and event results
Interact with the control. The latest event and its full payload are shown below. Popup selectors commit only after confirmation; cancellation keeps the previous value.
<script setup lang="ts">
import { ref } from 'vue'
const result = ref('')
const data = [
{
id: 1,
label: '根 / Root',
children: [
{ id: 2, label: '子项 / Child' },
{ id: 3, label: '禁用 / Disabled', disabled: true }
]
}
]
const keys = ref([])
function event0(arg0: unknown) {
result.value = 'node-click' + ': ' + JSON.stringify([arg0])
}
function event1(arg0: unknown) {
result.value = 'node-expand' + ': ' + JSON.stringify([arg0])
}
function event2(arg0: unknown) {
result.value = 'node-collapse' + ': ' + JSON.stringify([arg0])
}
function event3(arg0: unknown) {
result.value = 'update:current-key' + ': ' + JSON.stringify([arg0])
}
function event4(arg0: unknown) {
result.value = 'update:checked-keys' + ': ' + JSON.stringify([arg0])
}
function event5(arg0: unknown) {
result.value = 'update:expanded-keys' + ': ' + JSON.stringify([arg0])
}
function event6(arg0: unknown) {
result.value = 'check' + ': ' + JSON.stringify([arg0])
}
function event7(arg0: unknown) {
result.value = 'load-error' + ': ' + JSON.stringify([arg0])
}
</script>
<template>
<view>
<hui-tree
v-model:checked-keys="keys"
:data="data"
show-checkbox
default-expand-all
@node-click="event0"
@node-expand="event1"
@node-collapse="event2"
@update:current-key="event3"
@update:checked-keys="event4"
@update:expanded-keys="event5"
@check="event6"
@load-error="event7"
></hui-tree>
<text aria-live="polite">{{ result }}</text>
</view>
</template>
Behavior and platform support
Disabled descendants are not changed by linked selection, but still participate in the parent all-checked decision. A parent may therefore remain indeterminate. Lazy children stay in an internal cache.
Props
| Property | Type | Default | Description |
|---|---|---|---|
data | TreeNode[] | undefined | [] | Full tree records supplied by the application. |
props | TreeFieldNames | undefined | — | Field mapping; omitted fields use standard label/value/children/disabled names. |
node-key | string | undefined | 'id' | Unique key field; numeric and string keys remain distinct. |
expanded-keys | TreeNodeKey[] | undefined | undefined | Controlled expanded keys. |
default-expanded-keys | TreeNodeKey[] | undefined | [] | Initial expanded keys. |
default-expand-all | boolean | undefined | false | Initially expand all nodes, combined with explicit initial keys. |
default-expand-level | number | undefined | 0 | Initially expanded level count; zero adds no expansion. |
current-key | TreeNodeKey | null | undefined | undefined | Current selected node key; null clears selection. |
show-checkbox | boolean | undefined | false | Show checkboxes and enable multiple selection. |
checked-keys | TreeNodeKey[] | undefined | undefined | Controlled checked keys. |
default-checked-keys | TreeNodeKey[] | undefined | [] | Initial checked keys. |
check-strictly | boolean | undefined | false | Disable linked checking; allow non-leaf selection in single tree or cascader selection. |
accordion | boolean | undefined | false | Allow one expanded sibling at a time. |
expand-on-click-node | boolean | undefined | true | Toggle non-leaf expansion on row click. |
lazy | boolean | undefined | false | Load descendants asynchronously. |
load | TreeLoad | undefined | — | Load children from the current parent; failed requests can be retried and stale results are ignored. |
indent | number | undefined | 16 | Indent per level in pixels. |
empty-text | string | undefined | — | Empty-state text, defaulting to localized common.noData. |
custom-class | string | undefined | '' | Additional root classes. |
custom-style | string | CSSProperties | undefined | — | Additional root styles. |
Events
| Event | Parameters | Description |
|---|---|---|
node-click | (data: { node: TreeNode; key: TreeNodeKey; }) | Reports node-click with the arguments shown in the example. |
node-expand | (data: { node: TreeNode; key: TreeNodeKey; }) | Reports node-expand with the arguments shown in the example. |
node-collapse | (data: { node: TreeNode; key: TreeNodeKey; }) | Reports node-collapse with the arguments shown in the example. |
update:current-key | (key: TreeNodeKey | null) | Requests a controlled value update. |
update:checked-keys | (keys: TreeNodeKey[]) | Requests a controlled value update. |
update:expanded-keys | (keys: TreeNodeKey[]) | Requests a controlled value update. |
check | (data: { checkedKeys: TreeNodeKey[]; node: TreeNode; key: TreeNodeKey; checked: boolean; }) | Reports check with the arguments shown in the example. |
load-error | (data: { node: TreeNode; key: TreeNodeKey; error: unknown; }) | Reports load-error with the arguments shown in the example. |
Slots
| Slot | Description |
|---|---|
empty | Empty-state content. |
default | Default content. |
Methods
| Method | Signature |
|---|---|
expandPath | (key: TreeNodeKey) => Promise<void> |
getNode | (key: TreeNodeKey) => TreeNode | undefined |
getPath | (key: TreeNodeKey) => TreeNode[] |
getCheckedNodes | (strategy?: TreeCheckedStrategy | undefined) => TreeNode[] |
filter | (keyword: string, method?: ((keyword: string, node: TreeNode) => boolean) | undefined) => void |
Types
TreeProps、TreeEmits、TreeExpose。 Public types are exported from the package entry.
Platform notes
| Scope | Behavior and validation limits |
|---|---|
| Shared implementation | Renders explicit keys and data without relying on VNode child order. Check logic uses the shared tree utilities. |
| Device checks pending | Check long-tree scrolling, expansion and lazy loading on WeChat and App, including disabled branches, half checks and scoped slots. |
See examples/uni-demo/smoke/Tree.md for the checklist. Unit tests do not replace device acceptance.