Skip to content

Cascader 多级联动

规则

基础示例

js
const rule = {
    type:"cascader",
    title:"所在区域",
    field:"address",
    value:['陕西省','西安市','新城区'],
    props:{}
}

Props 配置示例

可搜索级联选择

js
const rule = {
    type:"cascader",
    title:"所在区域",
    field:"address",
    value:[],
    props:{
        options: [
            {
                value: 'beijing',
                label: '北京',
                children: [
                    { value: 'chaoyang', label: '朝阳区' },
                    { value: 'haidian', label: '海淀区' },
                ]
            },
            {
                value: 'shanghai',
                label: '上海',
                children: [
                    { value: 'huangpu', label: '黄浦区' },
                    { value: 'pudong', label: '浦东新区' },
                ]
            }
        ],
        filterable: true,
        placeholder: "请选择区域",
        clearable: true,
    }
}

多选级联

js
const rule = {
    type:"cascader",
    title:"商品分类",
    field:"categories",
    value:[],
    props:{
        options: [
            {
                value: 'electronics',
                label: '电子产品',
                children: [
                    { value: 'phone', label: '手机' },
                    { value: 'computer', label: '电脑' },
                ]
            }
        ],
        multiple: true,
        checkStrategy: 'child',
        placeholder: "请选择分类",
    }
}

远程加载数据

js
const rule = {
    type:"cascader",
    title:"所在区域",
    field:"address",
    value:[],
    props:{
        remote: true,
        placeholder: "请选择区域",
    },
    inject: true,
    on: {
        load: ($inject, option) => {
            // 根据父级选项加载子级数据
            return new Promise((resolve) => {
                loadRegionData(option.value).then(res => {
                    option.children = res.data.map(item => ({
                        value: item.id,
                        label: item.name
                    }));
                    resolve();
                });
            });
        },
    },
}

Events 事件示例

监听选择变化

js
const rule = {
    type:"cascader",
    title:"所在区域",
    field:"address",
    value:[],
    props:{
        options: [
            {
                value: 'beijing',
                label: '北京',
                children: [
                    { value: 'chaoyang', label: '朝阳区' },
                ]
            }
        ],
        placeholder: "请选择区域",
        clearable: true,
    },
    on: {
        'update:value': (value, option, pathValues) => {
            console.log('选择值改变:', value, option, pathValues);
        },
        blur: () => {
            console.log('失去焦点');
        },
        focus: () => {
            console.log('获得焦点');
        },
    },
}

选择后联动更新

js
const rule = [
    {
        type:"cascader",
        title:"所在区域",
        field:"address",
        value:[],
        props:{
            options: [
                {
                    value: 'beijing',
                    label: '北京',
                    children: [
                        { value: 'chaoyang', label: '朝阳区' },
                    ]
                }
            ],
            placeholder: "请选择区域",
        },
        inject: true,
        on: {
            'update:value': ($inject, value, option) => {
                // 根据选择的区域自动填充邮政编码
                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:"邮政编码",
        props: {
            disabled: true,
        },
    },
]

完整配置项:naive-ui_Cascader

value :Array

props

名称类型默认值说明版本
cascadebooleantrue在多选时是否关联选项
check-strategystring'all'设置勾选策略来指定显示的勾选节点,all 表示显示全部选中节点;parent 表示只显示父节点(当父节点下所有子节点都选中时,对于单选无效);child 表示只显示子节点;
children-fieldstring'children'替代 CascaderOption 中的 children 字段名
clearablebooleanfalse值是否可清除
disabledbooleanfalse是否禁用
expand-trigger'click' | 'hover''click'remote 被设定时 'hover' 不生效
filterablebooleanfalseremote 被设定时不生效
filter(pattern: string, option: CascaderOption, path: CascaderOption[]) => boolean一个基于字符串的过滤算法过滤选项的函数
value-fieldstring'value'替代 CascaderOption 中的 value 字段名
label-fieldstring'label'替代 CascaderOption 中的 label 字段名
max-tag-countnumber | 'responsive'undefined多选标签的最大显示数量,responsive 会将所有标签保持在一行
multiplebooleanfalse是否支持多选
optionsCascaderOption[][]填充的 options 数据
placeholderstring'请选择'提示信息
placement'top-start' | 'top' | 'top-end' | 'right-start' | 'right' | 'right-end' | 'bottom-start' | 'bottom' | 'bottom-end' | 'left-start' | 'left' | 'left-end' | 'bottom-start'弹出位置2.25.0
remotebooleanfalse是否远程获取数据
render-label(option: CascaderOption, checked: boolean) => VNodeChildundefinedCascader 菜单选项标签渲染函数2.24.0
separatorstring' / '数据分隔符
showbooleanundefined是否打开菜单
show-pathbooleantrue是否在选择器中显示选项路径
size'small' | 'medium' | 'large''medium'尺寸
virtual-scrollbooleantrue是否支持虚拟滚动
on-blur() => voidundefined用户 blur 时执行的回调
on-focus() => voidundefined用户 focus 时执行的回调
on-load(option: CascaderOption) => Promise<void>undefined在点击未加载完成节点时的回调,在返回的 promise 中设定 option.children,在返回的 promise resolve 或 reject 之后完成加载
on-update:show(value: boolean) => voidundefined菜单打开关闭的回调
on-update:value(value: string | number | Array<string | number> | null, option: CascaderOption | Array<CascaderOption | null> | null, pathValues: Array<CascaderOption | null> | Array<CascaderOption[] | null> | null) => voidundefined值改变时执行的回调

FormCreate 是一个开源项目,基于 MIT 许可证发布,欢迎个人和企业用户免费使用