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: '浦东新区' },
                ]
            }
        ],
        allowSearch: true,
        placeholder: "请选择区域",
        allowClear: true,
    }
}

多选级联

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

异步加载数据

js
const rule = {
    type:"cascader",
    title:"所在区域",
    field:"address",
    value:[],
    props:{
        options: [
            {
                value: 'beijing',
                label: '北京',
                isLeaf: false,
            }
        ],
        placeholder: "请选择区域",
    },
    inject: true,
    on: {
        'load-more': ($inject, option, done) => {
            // 根据父级选项加载子级数据
            loadRegionData(option.value).then(res => {
                option.children = res.data.map(item => ({
                    value: item.id,
                    label: item.name,
                    isLeaf: !item.hasChildren
                }));
                done();
            });
        },
    },
}

Events 事件示例

监听选择变化

js
const rule = {
    type:"cascader",
    title:"所在区域",
    field:"address",
    value:[],
    props:{
        options: [
            {
                value: 'beijing',
                label: '北京',
                children: [
                    { value: 'chaoyang', label: '朝阳区' },
                ]
            }
        ],
        placeholder: "请选择区域",
        allowClear: true,
    },
    on: {
        change: (value, option) => {
            console.log('选择值改变:', value, option);
        },
        clear: () => {
            console.log('清空选择');
        },
        search: (value) => {
            console.log('搜索值:', value);
        },
    },
}

选择后联动更新

js
const rule = [
    {
        type:"cascader",
        title:"所在区域",
        field:"address",
        value:[],
        props:{
            options: [
                {
                    value: 'beijing',
                    label: '北京',
                    children: [
                        { value: 'chaoyang', label: '朝阳区' },
                    ]
                }
            ],
            placeholder: "请选择区域",
        },
        inject: true,
        on: {
            change: ($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,
        },
    },
]

完整配置项:arco-design_Cascader

value :Array

Props

参数名描述类型默认值版本
path-mode绑定值是否为路径booleanfalse
multiple是否为多选状态(多选模式默认开启搜索)booleanfalse
options级联选择器的选项CascaderOption[][]
disabled是否禁用booleanfalse
error是否为错误状态booleanfalse
size选择框的大小'mini' | 'small' | 'medium' | 'large''medium'
allow-search是否允许搜索booleanfalse (single) | true (multiple)
allow-clear是否允许清除booleanfalse
popup-visible (v-model)是否显示下拉框boolean-
expand-trigger展开下一级的触发方式string'click'
default-popup-visible是否默认显示下拉框(非受控状态)booleanfalse
placeholder占位符string-
popup-container弹出框的挂载容器string | HTMLElement | null | undefined-
max-tag-count多选模式下,最多显示的标签数量。0 表示不限制number0
format-label格式化展示内容(options: CascaderOptionInfo[]) => string-
trigger-props下拉菜单的触发器属性TriggerProps-
check-strictly是否开启严格选择模式booleanfalse
load-more数据懒加载函数,传入时开启懒加载功能( option: CascaderOptionInfo, done: (children?: CascaderOption[]) => void) => void-2.13.0
loading是否为加载中状态booleanfalse2.15.0

Events

事件名描述参数
change选中值改变时触发value: string | string[] | undefined | (string | string[])[]
input-value-change输入值改变时触发value: string
clear点击清除按钮时触发-
search用户搜索时触发value: string
popup-visible-change下拉框的显示状态改变时触发visible: boolean
focus获得焦点时触发-
blur失去焦点时触发-

CascaderOption

参数名描述类型默认值版本
value选项值`stringnumber`-
label选项文本string-
render自定义渲染RenderFunction-
disabled是否禁用booleanfalse
tagProps展示的标签属性TagProps-2.8.0
children下一级选项CascaderOption[]-
isLeaf是否是叶子节点booleanfalse

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