Skip to content

Tree

Rules

Basic Example

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

Props Configuration Examples

Show Checkbox

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

Expand All Nodes by Default

js
const rule = {
    type:"tree",
    title:"Product Category",
    field:"categories",
    value:[],
    props:{
        data:[{
            id: 1,
            title: 'Electronics',
            children: [
                { id: 11, title: 'Phone' },
                { id: 12, title: 'Computer' },
            ]
        }],
        props: {
            label: "title"
        },
        defaultExpandAll: true,
    }
}

Lazy Load

js
const rule = {
    type:"tree",
    title:"File Tree",
    field:"files",
    value:[],
    props:{
        props: {
            label: "title"
        },
        loadData: function(node) {
            // Load child nodes dynamically
            return new Promise((resolve) => {
                if (node.id === '0-0') {
                    setTimeout(() => {
                        resolve([
                            { id: '0-0-0', title: 'File1.txt', isLeaf: true },
                            { id: '0-0-1', title: 'File2.txt', isLeaf: true },
                        ]);
                    }, 1000);
                } else {
                    resolve([]);
                }
            });
        },
    }
}

Events Examples

Listen to Node Selection

js
const rule = {
    type:"tree",
    title:"Permission Selection",
    field:"permissions",
    value:[],
    props:{
        data:[{
            id: 1,
            title: 'User Management',
            children: [
                { id: 11, title: 'View User' },
            ]
        }],
        props: {
            label: "title"
        },
        checkable: true,
    },
    on: {
        check: (checkedKeys, e) => {
            console.log('Node selection state changed:', checkedKeys, e);
        },
        select: (selectedKeys, e) => {
            console.log('Node selection:', selectedKeys, e);
        },
        expand: (expandedKeys, e) => {
            console.log('Node expand:', expandedKeys, e);
        },
    },
}

Count After Selection

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

Reference: Ant-design-vue_Tree

value :Array

Props

ParameterDescriptionTypeDefault
blockNodeWhether node occupies one linebooleanfalse
treeDatatreeNodes data, if set then no need to manually construct TreeNode nodes (key is unique within entire tree)array<{key, title, children, [disabled, selectable]}>--
replaceFieldsReplace title,key,children fields in treeNode with corresponding fields in treeDataobject{children:'children', title:'title', key:'key' }
autoExpandParentWhether to automatically expand parent nodesbooleantrue
checkableAdd Checkbox checkbox before nodebooleanfalse
checkedKeys(v-model)(Controlled) Tree nodes with checked checkboxes (Note: parent and child nodes are related, if parent node key is passed, child nodes are automatically selected; correspondingly if all child node keys are passed, parent node is also automatically selected. When setting checkable and checkStrictly, it is an object with checked and halfChecked properties, and parent and child node selection is no longer relatedstring[] | number[] | {checked: string[] | number[], halfChecked: string[] | number[]}[]
checkStrictlyIn checkable state, node selection is fully controlled (parent and child node selection states are no longer related)booleanfalse
defaultCheckedKeysDefault tree nodes with checked checkboxesstring[] | number[][]
defaultExpandAllExpand all tree nodes by defaultbooleanfalse
defaultExpandedKeysDefault expanded specified tree nodesstring[] | number[][]
defaultExpandParentExpand parent nodes by defaultbooltrue
defaultSelectedKeysDefault selected tree nodesstring[] | number[][]
disabledDisable treeboolfalse
draggableSet nodes draggablebooleanfalse
expandedKeys(.sync)(Controlled) Expand specified tree nodesstring[] | number[][]
filterTreeNodeFilter tree nodes on demand (highlight), return truefunction(node)-
loadDataAsynchronously load datafunction(node)-
loadedKeys(Controlled) Already loaded nodes, need to use with loadDatastring[] | number[][]
multipleSupport selecting multiple nodes (node itself)booleanfalse
selectableWhether selectablebooleantrue
selectedKeys(.sync)(Controlled) Set selected tree nodesstring[] | number[]-
showIconWhether to display icon before TreeNode title, no default style, if set to true, need to define icon related styles yourselfbooleanfalse
switcherIconCustom expand/collapse icon for tree nodesslot-
showLineWhether to display connection linebooleanfalse

Props.props

ParameterDescriptionTypeDefault
classNode's classstring-
styleNode's stylestring|object-
checkableWhen tree is checkable, set whether independent node displays Checkboxboolean-
disableCheckboxDisable checkboxbooleanfalse
disabledDisable responsebooleanfalse
iconCustom icon. Can receive component, props is current node propsslot|slot-scope-
isLeafSet as leaf node (effective when loadData is set)booleanfalse
keyUsed by tree's (default)ExpandedKeys / (default)CheckedKeys / (default)SelectedKeys properties. Note: All node key values in entire tree cannot be duplicated!string | numberInternally calculated node position
selectableSet whether node can be selectedbooleantrue
titleTitlestring|slot|slot-scope'---'
slotsWhen using treeNodes, can configure properties that support slot through this property, such as slots: { title: 'XXX'}object-
scopedSlotsWhen using columns, can configure properties that support slot-scope through this property, such as scopedSlots: { title: 'XXX'}object-
onEvent object, only effective when using treeNodes, such as {click: () => {}}object'---'

data Data Structure

js
const data = [{
    title: 'parent 1',
    expand: true,
    selected: false,
    id: 1,
    children: [
        {
            title: 'parent 1-1',
            expand: true,
            id:2,
            children: [
                {
                    title: 'leaf 1-1-1',
                    disabled: true,
                    id:11
                },
                {
                    title: 'leaf 1-1-2',
                    selected:true,
                    id:12
                }
            ]
        },
        {
            title: 'parent 1-2',
            expand: true,
            id:3,
            children: [
                {
                    title: 'leaf 1-2-1',
                    checked: true,
                    id:13,
                },
                {
                    title: 'leaf 1-2-1',
                    id:14,
                }
            ]
        }
    ]
}]

Events

Event NameDescriptionCallback Parameters
checkTriggered when clicking checkboxfunction(checkedKeys, e:{checked: bool, checkedNodes, node, event})
dragendCalled when dragend is triggeredfunction({event, node})
dragenterCalled when dragenter is triggeredfunction({event, node, expandedKeys})
dragleaveCalled when dragleave is triggeredfunction({event, node})
dragoverCalled when dragover is triggeredfunction({event, node})
dragstartCalled when starting dragfunction({event, node})
dropCalled when drop is triggeredfunction({event, node, dragNode, dragNodesKeys})
expandTriggered when expanding/collapsing nodefunction(expandedKeys, {expanded: bool, node})
loadTriggered when node loading completesfunction(loadedKeys, {event, node})
rightClickResponse to right clickfunction({event, node})
selectTriggered when clicking tree nodefunction(selectedKeys, e:{selected: bool, selectedNodes, node, event})

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