Skip to content

Cascader 多级联动

cascader

规则

基础示例

js
const rule = {
    type: 'cascader',
    title: '多级选择',
    field: 'cascader',
    value: '330100',
    props: {
        options: [
            {
                text: '浙江省',
                value: '330000',
                children: [{text: '杭州市', value: '330100'}],
            },
            {
                text: '江苏省',
                value: '320000',
                children: [{text: '南京市', value: '320100'}],
            },
        ],
        placeholder: '请选择',
    }
}

Props 配置示例

三级联动

js
const rule = {
    type: 'cascader',
    title: '所在区域',
    field: 'address',
    value: '',
    props: {
        title: '选择地址',
        options: [
            {
                text: '浙江省',
                value: '330000',
                children: [
                    {
                        text: '杭州市',
                        value: '330100',
                        children: [
                            {text: '西湖区', value: '330106'},
                            {text: '余杭区', value: '330110'},
                        ]
                    },
                ],
            },
        ],
        placeholder: '请选择区域',
    }
}

自定义字段名

js
const rule = {
    type: 'cascader',
    title: '商品分类',
    field: 'category',
    value: '',
    props: {
        title: '选择分类',
        options: [
            {
                label: '电子产品',
                id: '1',
                items: [
                    {label: '手机', id: '11'},
                    {label: '电脑', id: '12'},
                ]
            },
        ],
        fieldNames: {
            text: 'label',
            value: 'id',
            children: 'items',
        },
    }
}

Events 事件示例

监听选择变化

js
const rule = {
    type: 'cascader',
    title: '所在区域',
    field: 'address',
    value: '',
    props: {
        title: '选择地址',
        options: [
            {
                text: '浙江省',
                value: '330000',
                children: [{text: '杭州市', value: '330100'}],
            },
        ],
        placeholder: '请选择',
    },
    on: {
        change: (result) => {
            console.log('选择值改变:', result);
        },
        finish: (result) => {
            console.log('选择完成:', result);
        },
        close: () => {
            console.log('关闭选择器');
        },
    },
}

选择后联动更新

js
const rule = [
    {
        type: 'cascader',
        title: '所在区域',
        field: 'address',
        value: '',
        props: {
            title: '选择地址',
            options: [
                {
                    text: '浙江省',
                    value: '330000',
                    children: [{text: '杭州市', value: '330100'}],
                },
            ],
        },
        inject: true,
        on: {
            finish: ($inject, result) => {
                // 根据选择的区域,自动填充邮政编码
                const zipCodeMap = {
                    '330000': '310000',
                    '330100': '310000',
                };
                const value = result.value;
                if (zipCodeMap[value]) {
                    $inject.api.setValue('zipCode', zipCodeMap[value]);
                }
            },
        },
    },
    {
        type: 'input',
        title: '邮政编码',
        field: 'zipCode',
        props: {
            disabled: true,
        },
    },
]

完整配置项:Vant_Cascader

value :String|Number

Props

参数说明类型默认值
v-model选中项的值string | number-
title顶部标题string-
options可选项数据源CascaderOption[][]
placeholder未选中时的提示文案string请选择
disabled是否禁用输入booleanfalse
active-color选中状态的高亮颜色string#1989fa
swipeable是否开启手势左右滑动切换booleantrue
closeable是否显示关闭图标booleantrue
show-header是否展示标题栏booleantrue
close-icon关闭图标名称或图片链接,等同于 Icon 组件的 name 属性stringcross
field-names自定义 options 结构中的字段CascaderFieldNames{ text: 'text', value: 'value', children: 'children' }

Events

事件说明回调参数
change选中项变化时触发{ value: string | number, selectedOptions: CascaderOption[], tabIndex: number }
finish全部选项选择完成后触发{ value: string | number, selectedOptions: CascaderOption[], tabIndex: number }
close点击关闭图标时触发-
click-tab点击标签时触发tabIndex: number, title: string

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