Skip to content

Checkbox 多选框

checkbox

规则

基础示例

js
const rule = {
    type: 'checkbox',
    title: '多选框',
    field: 'checkbox',
    value: ['1'],
    props: {
        options: [
            {
                text: '复选框1',
                value: '1',
            },
            {
                text: '复选框2',
                value: '2',
            },
        ]
    }
}

Props 配置示例

限制选择数量

js
const rule = {
    type: 'checkbox',
    title: '商品标签',
    field: 'tags',
    value: ['1'],
    props: {
        options: [
            { text: '热销', value: '1' },
            { text: '新品', value: '2' },
            { text: '推荐', value: '3' },
        ],
        max: 3,
    }
}

横向排列

js
const rule = {
    type: 'checkbox',
    title: '商品标签',
    field: 'tags',
    value: [],
    props: {
        options: [
            { text: '热销', value: '1' },
            { text: '新品', value: '2' },
            { text: '推荐', value: '3' },
        ],
        direction: 'horizontal',
    }
}

自定义样式

js
const rule = {
    type: 'checkbox',
    title: '商品标签',
    field: 'tags',
    value: [],
    props: {
        options: [
            { text: '热销', value: '1' },
            { text: '新品', value: '2' },
        ],
        shape: 'square',
        checkedColor: '#ee0a24',
        iconSize: '24px',
    }
}

Events 事件示例

监听选择变化

js
const rule = {
    type: 'checkbox',
    title: '商品标签',
    field: 'tags',
    value: [],
    props: {
        options: [
            { text: '热销', value: '1' },
            { text: '新品', value: '2' },
            { text: '推荐', value: '3' },
        ],
    },
    on: {
        change: (names) => {
            console.log('选择值改变:', names);
        },
    },
}

选择后联动更新

js
const rule = [
    {
        type: 'checkbox',
        title: '商品标签',
        field: 'tags',
        value: [],
        props: {
            options: [
                { text: '热销', value: '1' },
                { text: '新品', value: '2' },
                { text: '推荐', value: '3' },
            ],
        },
        inject: true,
        on: {
            change: ($inject, names) => {
                // 根据选择的标签数量,自动设置商品状态
                if (names.length >= 2) {
                    $inject.api.setValue('status', 'active');
                } else {
                    $inject.api.setValue('status', 'inactive');
                }
            },
        },
    },
    {
        type: 'input',
        title: '商品状态',
        field: 'status',
        props: {
            disabled: true,
        },
    },
]

完整配置项:Vant_Checkbox

value :Array

Options

字段名说明字段类型是否必填默认值
value参数值String,Numbertrue-
text字段别名Stringtrue-
disabled设置为禁用状态Booleanfalsefalse

Props

参数说明类型默认值
disabled是否禁用所有复选框booleanfalse
max最大可选数,0 为无限制number | string0
direction排列方向,可选值为 horizontalstringvertical
icon-size所有复选框的图标大小,默认单位为 pxnumber | string20px
checked-color所有复选框的选中状态颜色string#1989fa
shape形状,可选值为 squarestringround

Events

事件名说明回调参数
change当绑定值变化时触发的事件names: any[]

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