Skip to content

Cascader

Rules

Basic Example

js
const rule = {
    type:"cascader",
    title:"Location",
    field:"address",
    value:['Shaanxi Province','Xi\'an City','Xincheng District'],
    props:{
        options:[{
            value: 'beijing',
            label: 'Beijing',
            children: [
                {
                    value: 'gugong',
                    label: 'Forbidden City'
                },
                {
                    value: 'tiantan',
                    label: 'Temple of Heaven'
                },
                {
                    value: 'wangfujing',
                    label: 'Wangfujing'
                }
            ]
        }, {
            value: 'jiangsu',
            label: 'Jiangsu',
            children: [
                {
                    value: 'nanjing',
                    label: 'Nanjing',
                    children: [
                        {
                            value: 'fuzimiao',
                            label: 'Confucius Temple',
                        }
                    ]
                },
                {
                    value: 'suzhou',
                    label: 'Suzhou',
                    children: [
                        {
                            value: 'zhuozhengyuan',
                            label: 'Humble Administrator\'s Garden',
                        },
                        {
                            value: 'shizilin',
                            label: 'Lion Grove Garden',
                        }
                    ]
                }
            ]
        }]
    }
}

Props Configuration Examples

Searchable

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

Select Any Level

js
const rule = {
    type:"cascader",
    title:"Product Category",
    field:"category",
    props:{
        options:[{
            value: 'electronics',
            label: 'Electronics',
            children: [
                { value: 'phone', label: 'Phone' },
                { value: 'computer', label: 'Computer' },
            ]
        }],
        changeOnSelect: true,
        placeholder: "Select any level",
    }
}

Only Display Last Level

js
const rule = {
    type:"cascader",
    title:"Location",
    field:"address",
    props:{
        options:[{
            value: 'beijing',
            label: 'Beijing',
            children: [
                { value: 'gugong', label: 'Forbidden City' },
            ]
        }],
        displayRender: (labels) => labels[labels.length - 1],
        placeholder: "Select area",
    }
}

Events Examples

Listen to Selection Changes

js
const rule = {
    type:"cascader",
    title:"Location",
    field:"address",
    props:{
        options:[{
            value: 'beijing',
            label: 'Beijing',
            children: [
                { value: 'gugong', label: 'Forbidden City' },
            ]
        }],
        placeholder: "Select area",
        allowClear: true,
    },
    on: {
        change: (value, selectedOptions) => {
            console.log('Selection value changed:', value, selectedOptions);
        },
        visibleChange: (visible) => {
            console.log('Dropdown display status:', visible);
        },
    },
}

Linkage Update After Selection

js
const rule = [
    {
        type:"cascader",
        title:"Location",
        field:"address",
        props:{
            options:[{
                value: 'beijing',
                label: 'Beijing',
                children: [
                    { value: 'gugong', label: 'Forbidden City' },
                ]
            }],
            placeholder: "Select area",
        },
        inject: true,
        on: {
            change: ($inject, value, selectedOptions) => {
                // Auto-fill zip code based on selected area
                const zipCodeMap = {
                    'beijing': '100000',
                    'gugong': '100006',
                };
                const lastValue = value[value.length - 1];
                if (zipCodeMap[lastValue]) {
                    $inject.api.setValue('zipCode', zipCodeMap[lastValue]);
                }
            },
        },
    },
    {
        type:"input",
        title:"Zip Code",
        field:"zipCode",
        props: {
            disabled: true,
        },
    },
]

Reference: Ant-design-vue_Cascader

value :Array

Props

ParameterDescriptionTypeDefault
allowClearWhether to support clearbooleantrue
autofocusAuto focusbooleanfalse
borderedWhether to have borderbooleantrue
clearIconCustom clear icon for selectorslot-
changeOnSelect(Effective in single selection) When this is true, clicking each level menu option will change the value, see demo abovebooleanfalse
defaultValueDefault selected itemstring[] | number[][]
disabledDisabledbooleanfalse
displayRenderRender function displayed after selection, can use #displayRender="{labels, selectedOptions}"({labels, selectedOptions}) => VNodelabels => labels.join(' / ')
popupClassNameCustom popup class namestring-
dropdownStyleCustom popup styleCSSProperties{}
expandIconCustom expand icon for submenuslot-
expandTriggerExpand method for submenuclick | hover
fieldNamesCustom fields for label value children in optionsobject{ label: 'label', value: 'value', children: 'children' }
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
loadDataUsed for dynamically loading options, cannot be used with showSearch(selectedOptions) => void-
maxTagCountMaximum number of tags to display, responsive mode will have performance impactnumber | responsive-
maxTagPlaceholderContent displayed when tags are hiddenv-slot | function(omittedValues)-
multipleSupport multiple selection nodesboolean-
notFoundContentContent displayed when dropdown list is emptystring | slot'Not Found'
openControl popup visibilityboolean-
optionsData source for optionsOption[]-
placeholderInput box placeholder textstring'Select'
placementPreset position for popupbottomLeft | bottomRight | topRightbottomLeft
showCheckedStrategyDefine how selected items are backfilled. Cascader.SHOW_CHILD: Only show selected child nodes. Cascader.SHOW_PARENT: Only show parent node (when all child nodes under parent are selected).Cascader.SHOW_PARENT | Cascader.SHOW_CHILD3.3.0
removeIconCustom clear icon for multiple selectionslot-
searchValueSet search value, needs to be used with showSearchstring-
showSearchDisplay search box in selectorboolean | objectfalse
statusSet validation status'error' | 'warning'-
sizeInput box sizelarge | default | default
suffixIconCustom suffix icon for selectorstring | VNode | slot-
tagRenderCustom tag content, effective in multiple selectionslot-

When showSearch is an object, its fields:

ParameterDescriptionTypeDefault
filterReceives inputValue and path as parameters. When path meets filter condition, should return true, otherwise return false.function(inputValue, path): boolean
limitNumber of search results to displaynumber | false50
matchInputWidthWhether search result list has same width as input boxboolean
renderUsed to render filtered options, can use #showSearchRender="{inputValue, path}"function({inputValue, path}): VNode
sortUsed to sort filtered optionsfunction(a, b, inputValue)

Events

Event NameDescriptionCallback Parameters
changeCallback after selection completes(value, selectedOptions) => void
dropdownVisibleChangeCallback for showing/hiding popup(value) => void
searchListen to search, returns input value(value) => void

Option

ts
interface Option {
  value: string | number;
  label?: any;
  disabled?: boolean;
  children?: Option[];
  // Mark whether it is a leaf node, effective when `loadData` is set
  // When set to `false`, it will force mark as parent node, even if current node has no children, it will show expand icon
  isLeaf?: boolean;
}

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