Skip to content

TreeSelect

Rules

Basic Example

js
const rule = {
    type:"treeSelect",
    title:"Tree Selection",
    field:"treeSelect",
    value:[],
    props:{
        treeData:[
            {
                label: 'Node1',
                value: '0-0',
                children: [
                    {
                        label: 'Child Node1',
                        value: '0-0-0',
                    },
                ],
            },
            {
                label: 'Node2',
                value: '0-1',
                children: [
                    {
                        label: 'Child Node3',
                        value: '0-1-0',
                        disabled: true,
                    },
                    {
                        label: 'Child Node4',
                        value: '0-1-1',
                    },
                    {
                        label: 'Child Node5',
                        value: '0-1-2',
                    },
                ],
            },
        ]
    }
}

Props Configuration Examples

Multiple Selection

js
const rule = {
    type:"treeSelect",
    title:"Product Category",
    field:"categories",
    value:[],
    props:{
        treeData:[{
            label: 'Electronics',
            value: 'electronics',
            children: [
                { label: 'Phone', value: 'phone' },
                { label: 'Computer', value: 'computer' },
            ]
        }],
        multiple: true,
        placeholder: "Select category",
    }
}

Searchable

js
const rule = {
    type:"treeSelect",
    title:"Location",
    field:"address",
    value:[],
    props:{
        treeData:[{
            label: 'Beijing',
            value: 'beijing',
            children: [
                { label: 'Forbidden City', value: 'gugong' },
            ]
        }],
        showSearch: true,
        placeholder: "Select or search for an area",
    }
}

Show Checkbox

js
const rule = {
    type:"treeSelect",
    title:"Permission Selection",
    field:"permissions",
    value:[],
    props:{
        treeData:[{
            label: 'User Management',
            value: 'user',
            children: [
                { label: 'View', value: 'view' },
                { label: 'Edit', value: 'edit' },
            ]
        }],
        treeCheckable: true,
        multiple: true,
        placeholder: "Select permissions",
    }
}

Events Examples

Listen to Selection Changes

js
const rule = {
    type:"treeSelect",
    title:"Location",
    field:"address",
    value:[],
    props:{
        treeData:[{
            label: 'Beijing',
            value: 'beijing',
            children: [
                { label: 'Forbidden City', value: 'gugong' },
            ]
        }],
        placeholder: "Select area",
        allowClear: true,
    },
    on: {
        change: (value, label, extra) => {
            console.log('Selection value changed:', value, label, extra);
        },
        search: (value) => {
            console.log('Search:', value);
        },
        select: (value, node, extra) => {
            console.log('Select node:', value, node, extra);
        },
    },
}

Complete configuration items: Ant-design-vue_TreeSelect

Props

ParameterDescriptionTypeDefault
allowClearShow clear buttonbooleanfalse
defaultValueSpecify default selected itemstring/string[]-
disabledWhether disabledbooleanfalse
popupClassNameclassName attribute of dropdown menustring-
dropdownMatchSelectWidthDropdown menu and selector have the same width. By default, min-width will be set, which will be ignored when the value is less than the selector width. false will disable virtual scrollingboolean | numbertrue
dropdownStyleStyle of dropdown menuobject-
fieldNamesReplace label,value,children fields in treeNode with corresponding fields in treeDataobject{children:'children', label:'title', value: 'value' }
filterTreeNodeWhether to filter based on input items, default uses value of treeNodeFilterProp as property value of TreeNode to filterboolean|Function(inputValue: string, treeNode: TreeNode) (function needs to return bool value)Function
getPopupContainerParent node for menu rendering. Default renders to body. If you encounter menu scroll positioning issues, try changing to scroll area and position relative to it.Function(triggerNode)() => document.body
labelInValueWhether to wrap each option's label into value, will change value type from string to {value: string, label: VNode, halfChecked(treeCheckStrictly effective): string[] } formatbooleanfalse
listHeightSet popup scroll heightnumber256
loadDataAsynchronously load datafunction(node)-
maxTagCountMaximum number of tags to displaynumber-
maxTagPlaceholderContent displayed when tags are hiddenslot/function(omittedValues)-
multipleSupport multiple selection (automatically becomes true when treeCheckable is set)booleanfalse
notFoundContentContent displayed when dropdown list is emptyslotNot Found
placeholderDefault text of selectorstring|slot-
placementPosition where selector pops upbottomLeft bottomRight topLeft topRightbottomLeft
replaceFieldsReplace label,value,key,children fields in treeNode with corresponding fields in treeDataobject{children:'children', label:'title', key:'key', value: 'value' }
searchPlaceholderDefault text of search boxstring|slot-
searchValueValue of search box, can get user input through search eventstring-
showCheckedStrategyDefine how selected items are backfilled. TreeSelect.SHOW_ALL: Show all selected nodes (including parent nodes). TreeSelect.SHOW_PARENT: Only show parent node (when all child nodes under parent are selected). Default only shows child nodes.enum{TreeSelect.SHOW_ALL, TreeSelect.SHOW_PARENT, TreeSelect.SHOW_CHILD }TreeSelect.SHOW_CHILD
showSearchDisplay search box in dropdown (only effective in single selection mode)booleanfalse
sizeSelector size, options: large smallstring'default'
statusSet validation status'error' | 'warning'-
suffixIconCustom suffix icon for selectorVNode | slot-
tagRenderCustom tag content, effective in multiple selectionslot-
titleCustom titleslot
treeCheckableShow checkboxbooleanfalse
treeCheckStrictlyIn checkable state, node selection is fully controlled (parent and child node selection states are no longer related), will force labelInValue to be truebooleanfalse
treeDatatreeNodes data, if set then no need to manually construct TreeNode nodes (value is unique within entire tree)array<{value, label, children, [disabled, disableCheckbox, selectable]}>[]
treeDataSimpleModeUse simple format treeData, specific settings refer to settable types (at this time treeData should become data structure like: [{id:1, pId:0, value:'1', label:"test1",...},...], pId is parent node's id)false|Array<{ id: string, pId: string, rootPId: null }>false
treeDefaultExpandAllExpand all tree nodes by defaultbooleanfalse
treeDefaultExpandedKeysDefault expanded tree nodesstring[] | number[]-
treeExpandedKeysSet expanded tree nodesstring[] | number[]-
treeIconWhether to display icon before TreeNode title, no default style, if set to true, need to define icon related styles yourselfbooleanfalse
treeLineWhether to display line style, please refer to Tree - showLineboolean | objectfalse
treeLoadedKeys(Controlled) Already loaded nodes, need to use with loadDatastring[][]
treeNodeFilterProptreeNode property corresponding to input item filterstring'value'
treeNodeLabelPropSet as displayed propstring'title'
virtualSet false to disable virtual scrollingbooleantrue

Events

Event NameDescriptionCallback Parameters
changeCalled when tree node is selected or input value changesfunction(value, label, extra)
dropdownVisibleChangeCallback when dropdown menu expandsfunction(open)
searchCallback when text box value changesfunction(value: string)
selectCalled when tree node is selectedfunction(value, node, extra)
treeExpandCalled when tree node expandsfunction(expandedKeys)

TreeNode props

ParameterDescriptionTypeDefault
checkableWhen tree is checkable, set whether independent node displays Checkboxboolean-
disableCheckboxDisable checkboxbooleanfalse
disabledWhether disabledbooleanfalse
isLeafWhether is leaf nodebooleanfalse
keyThis must be set (value is unique within entire tree)string | number-
selectableWhether selectablebooleantrue
titleContent displayed by tree nodestring|slot'---'
valueDefault filter based on this property value (value is unique within entire tree)string-

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