Skip to content

Tree

Rules

Basic Example

js
const rule = {
    type:"tree",
    title:"Permissions",
    field:"rule",
    value:[],
    props:{
        data:[],
        props: {
            label: "title"
        }
    }
}

Props Configuration Examples

Checkable Tree

js
const rule = {
    type:"tree",
    title:"Permission Selection",
    field:"permissions",
    value:[],
    props:{
        data: [
            {
                key: 1,
                label: 'User Management',
                children: [
                    { key: 11, label: 'View Users' },
                    { key: 12, label: 'Edit Users' },
                ]
            },
            {
                key: 2,
                label: 'System Settings',
                children: [
                    { key: 21, label: 'Basic Settings' },
                    { key: 22, label: 'Advanced Settings' },
                ]
            }
        ],
        checkable: true,
        cascade: true,
        checkStrategy: 'all',
    }
}

Selectable Tree

js
const rule = {
    type:"tree",
    title:"Menu Selection",
    field:"menu",
    value:[],
    props:{
        data: [
            {
                key: 1,
                label: 'Home',
            },
            {
                key: 2,
                label: 'System Management',
                children: [
                    { key: 21, label: 'User Management' },
                    { key: 22, label: 'Role Management' },
                ]
            }
        ],
        selectable: true,
        multiple: true,
    }
}

Expand All by Default

js
const rule = {
    type:"tree",
    title:"Permission Selection",
    field:"permissions",
    value:[],
    props:{
        data: [
            {
                key: 1,
                label: 'User Management',
                children: [
                    { key: 11, label: 'View Users' },
                ]
            }
        ],
        checkable: true,
        cascade: true,
        defaultExpandAll: true,
    }
}

Async Loading

js
const rule = {
    type:"tree",
    title:"Permission Selection",
    field:"permissions",
    value:[],
    props:{
        data: [
            {
                key: 1,
                label: 'User Management',
                isLeaf: false,
            }
        ],
        checkable: true,
        remote: true,
    },
    inject: true,
    on: {
        load: ($inject, node) => {
            // Async load child node data
            return new Promise((resolve) => {
                loadTreeData(node.key).then(res => {
                    node.children = res.data.map(item => ({
                        key: item.id,
                        label: item.name,
                        isLeaf: !item.hasChildren
                    }));
                    resolve();
                });
            });
        },
    },
}

Events Examples

Handle Check and Selection Changes

js
const rule = {
    type:"tree",
    title:"Permission Selection",
    field:"permissions",
    value:[],
    props:{
        data: [
            {
                key: 1,
                label: 'User Management',
                children: [
                    { key: 11, label: 'View Users' },
                ]
            }
        ],
        checkable: true,
        cascade: true,
    },
    on: {
        'update:checked-keys': (keys, options) => {
            console.log('Checked nodes changed:', keys, options);
        },
        'update:selected-keys': (keys, options) => {
            console.log('Selected nodes changed:', keys, options);
        },
        'update:expanded-keys': (keys, options) => {
            console.log('Expanded nodes changed:', keys, options);
        },
    },
}

Count Permissions After Checking

js
const rule = [
    {
        type:"tree",
        title:"Permission Selection",
        field:"permissions",
        value:[],
        props:{
            data: [
                {
                    key: 1,
                    label: 'User Management',
                    children: [
                        { key: 11, label: 'View Users' },
                        { key: 12, label: 'Edit Users' },
                    ]
                }
            ],
            checkable: true,
            cascade: true,
        },
        inject: true,
        on: {
            'update:checked-keys': ($inject, keys) => {
                // Count selected permissions
                $inject.api.setValue('permissionCount', keys.length);
            },
        },
    },
    {
        type:"input",
        field:"permissionCount",
        title:"Selected Permission Count",
        props: {
            disabled: true,
        },
    },
]

Complete configuration items: naive-ui_Tree

value :Array

Props

NameTypeDefault ValueDescriptionVersion
allow-drop(info: { dropPosition: DropPosition, node: TreeOption, phase: 'drag' | 'drop' }) => booleanA function that does not allow drop inside leaf nodesWhether drop is allowed
block-linebooleanfalseNode entire line stretched
block-nodebooleanfalseNode name entire line stretched
cancelablebooleantrueWhether cancellation is allowed after selection
cascadebooleanfalseWhether to associate options
check-strategystring'all'Set check strategy to specify values returned by check callback, all means callback function value is all selected nodes; parent means callback function value is parent nodes (when all child nodes under parent node are selected); child means callback function value is child nodes
checkablebooleanfalseWhether to show checkbox, need to set cascade to true
children-fieldstring'children'Alternative field name for children in TreeOption
checked-keysArray<string | number>undefinedIf set, checked state is controlled
dataArray<TreeOption>[]Tree node data. Resetting data will clear some uncontrolled states. If you need to modify data during use, it's better to control the tree in a controlled way
default-checked-keysArray<string | number>[]Default selected multiple options
default-expand-allbooleanfalseExpand all options
default-expanded-keysArray<string | number>[]Default expanded items
default-selected-keysArray<string | number>[]Default selected nodes
draggablebooleanfalseWhether draggable
expand-on-dragenterbooleantrueWhether to expand node after dragging in
expanded-keysArray<string | number>undefinedIf set, expansion is controlled
filter(pattern: string, node: TreeOption) => booleanA simple string filtering algorithmFunction to filter nodes based on pattern
indeterminate-keysArray<string | number>undefinedKeys of partially selected options
key-fieldstring'key'Alternative field name for key in TreeOption
label-fieldstring'label'Alternative field name for label in TreeOption
leaf-onlybooleanfalseWhether to enable only leaf tree nodes selectable
node-props(info: { option: TreeOption }) => HTMLAttributesundefinedHTML attributes of nodes2.25.0
multiplebooleanfalseWhether to allow multiple node selection
on-load(node: TreeOption) => Promise<void>undefinedCallback function for async data loading
patternstring''Default search content
remotebooleanfalseWhether to fetch options asynchronously, used with onLoad
render-label(info: {option: TreeOption, checked: boolean, selected: boolean}) => VNodeChildundefinedRendering function for node content
render-prefix(info: {option: TreeOption, checked: boolean, selected: boolean}) => VNodeChildundefinedRendering function for node prefix
render-suffix(info: {option: TreeOption, checked: boolean, selected: boolean}) => VNodeChildundefinedRendering function for node suffix
render-switcher-icon() => VNodeChildundefinedRendering function for node expand switch2.24.0
selectablebooleantrueWhether nodes can be selected
selected-keysArray<string | number>undefinedIf set, selected state is controlled
virtual-scrollbooleanfalseWhether to enable virtual scrolling, you need to set tree height style before enabling
watch-propsArray<'defaultCheckedKeys' | 'defaultSelectedKeys' |'defaultExpandedKeys'>undefinedDefault properties that need to detect changes, component state will update after detection. Note: watch-props itself is not reactive
on-dragend(data: { node: TreeOption, event: DragEvent }) => voidundefinedCallback function after node drag action completes
on-dragenter(data: { node: TreeOption, event: DragEvent }) => voidundefinedCallback function during node dragging
on-dragleave(data: { node: TreeOption, event: DragEvent }) => voidCallback function after dragging a node, when the node leaves other nodes
on-dragstart(data: { node: TreeOption, event: DragEvent }) => voidundefinedCallback function when starting to drag a node
on-drop(data: { node: TreeOption, dragNode: TreeOption, dropPosition: 'before' | 'inside' | 'after', event: DragEvent }) => voidundefinedCallback function after node drag action completes
on-update:checked-keys(keys: Array<string | number>, option: Array<TreeOption | null>)) => voidundefinedCallback function when node checked items change
on-update:indeterminate-keys(keys: Array<string | number>, option: Array<TreeOption | null>)) => voidundefinedCallback function when node partially checked items change
on-update:expanded-keys(keys: Array<string | number>, option: Array<TreeOption | null>)) => voidundefinedCallback function when node expanded items change
on-update:selected-keys(keys: Array<string | number>, option: Array<TreeOption | null>)) => voidundefinedCallback function when node selected items change

TreeOption Properties

NameTypeDescription
keystring | numberNode's key, needs to be unique, can use key-field to modify field name
labelstringNode's content, can use label-field to modify field name
checkboxDisabled?booleanWhether to disable node's checkbox
children?TreeOption[]Node's child nodes
disabled?booleanWhether to disable node
isLeaf?booleanWhether node is a leaf node, required in remote mode
prefix?string | (() => VNodeChild)Node's prefix
suffix?string | (() => VNodeChild)Node's suffix

FormCreate is an open-source project released under the MIT License. Free for personal and commercial use.