Skip to content

Tree Tree Component

Rules

Basic Example

js
const rule = {
    type:"tree",
    title:"Permissions",
    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 Users' },
                { id: 12, title: 'Edit Users' },
            ]
        }],
        props: {
            label: "title",
            children: "children"
        },
        showCheckbox: true,
        nodeKey: "id",
    }
}

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",
            children: "children"
        },
        defaultExpandAll: true,
        nodeKey: "id",
    }
}

Lazy Loading

js
const rule = {
    type:"tree",
    title:"File Tree",
    field:"files",
    value:[],
    props:{
        props: {
            label: "name",
            children: "children"
        },
        lazy: true,
        load: function(node, resolve) {
            // Dynamically load child nodes
            if (node.level === 0) {
                resolve([
                    { name: 'Folder 1', id: 1, isLeaf: false },
                    { name: 'Folder 2', id: 2, isLeaf: false },
                ]);
            } else {
                resolve([
                    { name: 'File 1.txt', id: 11, isLeaf: true },
                    { name: 'File 2.txt', id: 12, isLeaf: true },
                ]);
            }
        },
        nodeKey: "id",
    }
}

Draggable

js
const rule = {
    type:"tree",
    title:"Menu Sorting",
    field:"menus",
    value:[],
    props:{
        data:[{
            id: 1,
            title: 'Menu 1',
            children: [
                { id: 11, title: 'Submenu 1' },
            ]
        }],
        props: {
            label: "title",
            children: "children"
        },
        draggable: true,
        nodeKey: "id",
    }
}

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 Users' },
            ]
        }],
        props: {
            label: "title",
            children: "children"
        },
        showCheckbox: true,
        nodeKey: "id",
    },
    on: {
        'check': (data, checked) => {
            console.log('Node selection state changed:', data, checked);
        },
        'node-click': (data, node) => {
            console.log('Node clicked:', data, node);
        },
        'node-expand': (data, node) => {
            console.log('Node expanded:', data, node);
        },
        'node-collapse': (data, node) => {
            console.log('Node collapsed:', data, node);
        },
    },
}

Count Quantity 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 Users' },
                    { id: 12, title: 'Edit Users' },
                ]
            }],
            props: {
                label: "title",
                children: "children"
            },
            showCheckbox: true,
            nodeKey: "id",
        },
        inject: true,
        on: {
            'check': ($inject, data, checked) => {
                // Count selected permissions
                const checkedKeys = $inject.api.getValue('permissions') || [];
                $inject.api.setValue('permissionCount', checkedKeys.length);
            },
        },
    },
    {
        type:"input-number",
        field:"permissionCount",
        title:"Selected Permission Count",
        props: {
            disabled: true,
        },
    },
]

Full configuration: Element_Tree

value :Array

Props

ParameterDescriptionTypeOptional ValuesDefault Value
dataDisplay dataarray
emptyTextText displayed when content is emptyString
nodeKeyProperty used by each tree node as unique identifier. Should be unique across the entire treeString
propsConfiguration options, see table belowobject
renderAfterExpandWhether to render child nodes only after expanding a tree node for the first timebooleantrue
loadMethod to load subtree data. Only effective when lazy property is truefunction(node, resolve)
renderContentRendering Function for tree node content areaFunction(h, { node, data, store })
highlightCurrentWhether to highlight currently selected node. Default is false.booleanfalse
defaultExpandAllWhether to expand all nodes by defaultbooleanfalse
expandOnClickNodeWhether to expand or collapse node when clicking node. Default is true. If false, node will only expand or collapse when clicking arrow icon.booleantrue
checkOnClickNodeWhether to select node when clicking node. Default is false, meaning node will only be selected when clicking checkbox.booleanfalse
autoExpandParentWhether to automatically expand parent node when expanding child nodebooleantrue
defaultExpandedKeysArray of keys of nodes expanded by defaultarray
showCheckboxWhether nodes can be selectedbooleanfalse
checkStrictlyWhen checkbox is displayed, whether to strictly follow the practice that parent and child are not mutually associated. Default is falsebooleanfalse
defaultCheckedKeysArray of keys of nodes checked by defaultarray
currentNodeKeyCurrently selected nodestring, number
filterNodeMethodMethod executed when filtering tree nodes. Return true to display this node, return false to hide this nodeFunction(value, data, node)
accordionWhether to only open one sibling tree node at a timebooleanfalse
indentHorizontal indentation between adjacent level nodes, in pixelsnumber16
iconClassCustom icon for tree nodesstring--
lazyWhether to lazy load child nodes. Must be used together with load methodbooleanfalse
draggableWhether to enable drag and drop node functionalitybooleanfalse
allowDragDetermines whether a node can be draggedFunction(node)
allowDropDetermine whether target node can accept drop during drag and drop. type parameter has three cases: 'prev', 'inner' and 'next', representing placing before target node, inserting into target node and placing after target nodeFunction(draggingNode, dropNode, type)

Porps.props

ParameterDescriptionTypeOptional ValuesDefault Value
labelSpecify node label as a property value of node objectstring, function(data, node)
childrenSpecify subtree as a property value of node objectstring
disabledSpecify whether node checkbox is disabled as a property value of node objectboolean, function(data, node)
isLeafSpecify whether node is a leaf node. Only effective when lazy property is specifiedboolean, function(data, node)

data Data Structure

js
[{
    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
node-clickCallback when node is clickedThree parameters in order: object corresponding to the node in the array passed to data property, Node corresponding to the node, the node component itself.
node-contextmenuTriggered when a node is right-clickedFour parameters in order: event, object corresponding to the node in the array passed to data property, Node corresponding to the node, the node component itself.
check-changeCallback when node selection state changesThree parameters in order: object corresponding to the node in the array passed to data property, whether the node itself is selected, whether there are selected nodes in the node's subtree
checkTriggered when checkbox is clickedTwo parameters in order: object corresponding to the node in the array passed to data property, current selection state object of the tree, containing four properties: checkedNodes, checkedKeys, halfCheckedNodes, halfCheckedKeys
current-changeEvent triggered when currently selected node changesTwo parameters: data of current node, Node object of current node
node-expandEvent triggered when node is expandedThree parameters in order: object corresponding to the node in the array passed to data property, Node corresponding to the node, the node component itself
node-collapseEvent triggered when node is collapsedThree parameters in order: object corresponding to the node in the array passed to data property, Node corresponding to the node, the node component itself
node-drag-startEvent triggered when node starts draggingTwo parameters in order: Node corresponding to the dragged node, event
node-drag-enterEvent triggered when dragging enters another nodeThree parameters in order: Node corresponding to the dragged node, Node corresponding to the entered node, event
node-drag-leaveEvent triggered when dragging leaves a nodeThree parameters in order: Node corresponding to the dragged node, Node corresponding to the left node, event
node-drag-overEvent triggered when dragging node (similar to browser's mouseover event)Three parameters in order: Node corresponding to the dragged node, Node corresponding to the currently entered node, event
node-drag-endEvent triggered when drag ends (may not be successful)Four parameters in order: Node corresponding to the dragged node, the last entered node when drag ends (may be empty), placement position of the dragged node (before, after, inner), event
node-dropEvent triggered when drag is successfully completedFour parameters in order: Node corresponding to the dragged node, the last entered node when drag ends, placement position of the dragged node (before, after, inner), event

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