Skip to content

Tree

Rules

Basic Example

js
const rule = {
    type:"tree",
    title:"Permissions",
    field:"rule",
    value:[],
    props:{
        data:[],
        fieldNames: {
            key: 'id',
            title: 'title',
            children: 'children'
        }
    }
}

Props Configuration Examples

Checkable Tree

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

Selectable Tree

js
const rule = {
    type:"tree",
    title:"Menu Selection",
    field:"menu",
    value:[],
    props:{
        data: [
            {
                key: 1,
                title: 'Home',
            },
            {
                key: 2,
                title: 'System Management',
                children: [
                    { key: 21, title: 'User Management' },
                    { key: 22, title: '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,
                title: 'User Management',
                children: [
                    { key: 11, title: 'View Users' },
                ]
            }
        ],
        checkable: true,
        defaultExpandAll: true,
    }
}

Async Load

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

Events Examples

Handle Check and Select Changes

js
const rule = {
    type:"tree",
    title:"Permission Selection",
    field:"permissions",
    value:[],
    props:{
        data: [
            {
                key: 1,
                title: 'User Management',
                children: [
                    { key: 11, title: 'View Users' },
                ]
            }
        ],
        checkable: true,
    },
    on: {
        check: (checkedKeys, event) => {
            console.log('Checked nodes changed:', checkedKeys, event);
        },
        select: (selectedKeys, event) => {
            console.log('Selected nodes changed:', selectedKeys, event);
        },
        expand: (expandKeys, event) => {
            console.log('Expanded nodes changed:', expandKeys, event);
        },
    },
}

Count Permission Number After Checking

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

Complete configuration items: arco-design_Tree

value: Array

Props

ParameterDescriptionTypeDefaultVersion
sizeSize'mini' | 'small' | 'medium' | 'large''medium'
block-nodeWhether node occupies one linebooleanfalse
default-expand-allWhether to expand parent nodes by defaultbooleantrue
multipleWhether to support multiple selectionbooleanfalse
checkableWhether to add checkbox before nodebooleanfalse
selectableWhether to support selectionbooleantrue
check-strictlyWhether to cancel parent-child node associationbooleanfalse
checked-strategyCustom backfill method: all: return all selected nodes, parent: return only parent when both parent and child are selected, child: return only child'all' | 'parent' | 'child''all'
default-selected-keysDefault selected tree nodesArray<string | number>-
selected-keysSelected tree nodesArray<string | number>-
default-checked-keysDefault checked tree nodesArray<string | number>-
checked-keysChecked tree nodesArray<string | number>-
default-expanded-keysDefault expanded nodesArray<string | number>-
expanded-keysExpanded nodesArray<string | number>-
dataPass data to generate corresponding tree structureTreeNodeData[][]
field-namesSpecify field names in node dataFieldNames-
show-lineWhether to show connection linesbooleanfalse
load-moreCallback for async loading data, returns a Promise(node: TreeNodeData) => Promise<void>-
draggableWhether draggablebooleanfalse
allow-dropWhether to allow drop on a node when dragging(options: { dropNode: TreeNodeData; dropPosition: -1 | 0 | 1;}) => boolean-
virtual-list-propsPass virtual list properties, pass this parameter to enable virtual scrolling, VirtualListPropsVirtualListProps-
default-expand-selectedWhether to expand parent nodes of selected nodes by defaultbooleanfalse2.9.0
default-expand-checkedWhether to expand parent nodes of checked nodes by defaultbooleanfalse2.9.0
auto-expand-parentWhether to automatically expand parent nodes of expanded nodesbooleantrue2.9.0

Events

Event NameDescriptionParameters
selectTriggered when tree node is clickedselectedKeys: Array<string | number> event: { selected: boolean; selectedNodes: TreeNodeData[]; node: TreeNodeData; e: Event; }
checkTriggered when tree node checkbox is clickedcheckedKeys: Array<string | number> event: { checked: boolean; checkedNodes: TreeNodeData[]; node: TreeNodeData; e: Event; }
expandExpand/CollapseexpandKeys: Array<string | number> event: { expanded: boolean; expandNodes: TreeNodeData[]; node: TreeNodeData; e: Event; }
drag-startNode starts dragging-
drag-endNode ends draggingevent: DragEvent node: TreeNodeData
drag-overNode is dragged to droppable targetevent: DragEvent node: TreeNodeData
drag-leaveNode leaves droppable targetevent: DragEvent node: TreeNodeData
dropNode is dropped on droppable targetinfo: { e: DragEvent; dragNode: TreeNodeData; dropNode: TreeNodeData; dropPosition: -1 | 0 | 1; }

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