Skip to content

Cascader

Rules

Basic Example

js
const rule = {
    type:"cascader",
    title:"Region",
    field:"address",
    value:['Shaanxi Province','Xi\'an City','Xincheng District'],
    props:{}
}

Props Configuration Examples

Searchable Cascader

js
const rule = {
    type:"cascader",
    title:"Region",
    field:"address",
    value:[],
    props:{
        options: [
            {
                value: 'beijing',
                label: 'Beijing',
                children: [
                    { value: 'chaoyang', label: 'Chaoyang District' },
                    { value: 'haidian', label: 'Haidian District' },
                ]
            },
            {
                value: 'shanghai',
                label: 'Shanghai',
                children: [
                    { value: 'huangpu', label: 'Huangpu District' },
                    { value: 'pudong', label: 'Pudong New Area' },
                ]
            }
        ],
        filterable: true,
        placeholder: "Select region",
        clearable: true,
    }
}

Multiple Selection Cascader

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

Remote Data Loading

js
const rule = {
    type:"cascader",
    title:"Region",
    field:"address",
    value:[],
    props:{
        remote: true,
        placeholder: "Select region",
    },
    inject: true,
    on: {
        load: ($inject, option) => {
            // Load child data for selected parent
            return new Promise((resolve) => {
                loadRegionData(option.value).then(res => {
                    option.children = res.data.map(item => ({
                        value: item.id,
                        label: item.name
                    }));
                    resolve();
                });
            });
        },
    },
}

Events Examples

Handle Selection Changes

js
const rule = {
    type:"cascader",
    title:"Region",
    field:"address",
    value:[],
    props:{
        options: [
            {
                value: 'beijing',
                label: 'Beijing',
                children: [
                    { value: 'chaoyang', label: 'Chaoyang District' },
                ]
            }
        ],
        placeholder: "Select region",
        clearable: true,
    },
    on: {
        'update:value': (value, option, pathValues) => {
            console.log('Selection value changed:', value, option, pathValues);
        },
        blur: () => {
            console.log('Lost focus');
        },
        focus: () => {
            console.log('Gained focus');
        },
    },
}

Linkage Update After Selection

js
const rule = [
    {
        type:"cascader",
        title:"Region",
        field:"address",
        value:[],
        props:{
            options: [
                {
                    value: 'beijing',
                    label: 'Beijing',
                    children: [
                        { value: 'chaoyang', label: 'Chaoyang District' },
                    ]
                }
            ],
            placeholder: "Select region",
        },
        inject: true,
        on: {
            'update:value': ($inject, value, option) => {
                // Fill postal code by region
                if (option && option.length > 0) {
                    const postcodeMap = {
                        'chaoyang': '100020',
                        'haidian': '100080',
                    };
                    const lastOption = option[option.length - 1];
                    $inject.api.setValue('postcode', postcodeMap[lastOption.value] || '');
                }
            },
        },
    },
    {
        type:"input",
        field:"postcode",
        title:"Postal Code",
        props: {
            disabled: true,
        },
    },
]

Complete configuration items: naive-ui_Cascader

value :Array

props

NameTypeDefault ValueDescriptionVersion
cascadebooleantrueWhether to associate options in multiple selection
check-strategystring'all'Set check strategy to specify displayed checked nodes, all means display all selected nodes; parent means only display parent nodes (when all child nodes under parent node are selected, invalid for single selection); child means only display child nodes;
children-fieldstring'children'Alternative field name for children in CascaderOption
clearablebooleanfalseWhether value can be cleared
disabledbooleanfalseWhether disabled
expand-trigger'click' | 'hover''click''hover' does not take effect when remote is set
filterablebooleanfalseDoes not take effect when remote is set
filter(pattern: string, option: CascaderOption, path: CascaderOption[]) => booleanA string-based filtering algorithmFunction to filter options
value-fieldstring'value'Alternative field name for value in CascaderOption
label-fieldstring'label'Alternative field name for label in CascaderOption
max-tag-countnumber | 'responsive'undefinedMaximum display count of multiple selection tags, responsive will keep all tags in one line
multiplebooleanfalseWhether to support multiple selection
optionsCascaderOption[][]Options data
placeholderstring'Select'Hint message
placement'top-start' | 'top' | 'top-end' | 'right-start' | 'right' | 'right-end' | 'bottom-start' | 'bottom' | 'bottom-end' | 'left-start' | 'left' | 'left-end' | 'bottom-start'Popup position2.25.0
remotebooleanfalseWhether to fetch data remotely
render-label(option: CascaderOption, checked: boolean) => VNodeChildundefinedCascader menu option label rendering function2.24.0
separatorstring' / 'Data separator
showbooleanundefinedWhether to open menu
show-pathbooleantrueWhether to display option path in selector
size'small' | 'medium' | 'large''medium'Size
virtual-scrollbooleantrueWhether to support virtual scrolling
on-blur() => voidundefinedCallback executed when user blurs
on-focus() => voidundefinedCallback executed when user focuses
on-load(option: CascaderOption) => Promise<void>undefinedCallback when clicking unloaded node, set option.children in returned promise, loading completes after promise resolve or reject
on-update:show(value: boolean) => voidundefinedCallback when menu opens/closes
on-update:value(value: string | number | Array<string | number> | null, option: CascaderOption | Array<CascaderOption | null> | null, pathValues: Array<CascaderOption | null> | Array<CascaderOption[] | null> | null) => voidundefinedCallback executed when value changes

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