Skip to content

Select 下拉选择框

select

规则

基础示例

js
const rule = {
    type: 'select',
    title: '下拉选择',
    field: 'select',
    value: 'Wednesday',
    props: {
        title: '预约',
        options: [
            { text: '周一', value: 'Monday' },
            { text: '周二', value: 'Tuesday' },
            { text: '周三', value: 'Wednesday' },
            { text: '周四', value: 'Thursday' },
            { text: '周五', value: 'Friday' },
        ],
        placeholder: '请选择',
    }
}

Props 配置示例

单选下拉框

js
const rule = {
    type: 'select',
    title: '商品分类',
    field: 'category',
    value: '1',
    props: {
        title: '选择分类',
        options: [
            { text: '电子产品', value: '1' },
            { text: '服装配饰', value: '2' },
            { text: '家居用品', value: '3' },
        ],
        placeholder: '请选择分类',
    }
}

多列选择

js
const rule = {
    type: 'select',
    title: '省市区',
    field: 'address',
    value: [],
    props: {
        title: '选择地址',
        options: [
            [
                { text: '浙江', value: 'zhejiang' },
                { text: '福建', value: 'fujian' },
            ],
            [
                { text: '杭州', value: 'hangzhou' },
                { text: '温州', value: 'wenzhou' },
            ],
            [
                { text: '西湖区', value: 'xihu' },
                { text: '余杭区', value: 'yuhang' },
            ],
        ],
    }
}

自定义按钮文字

js
const rule = {
    type: 'select',
    title: '预约时间',
    field: 'time',
    value: '',
    props: {
        title: '选择时间',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        options: [
            { text: '09:00', value: '09:00' },
            { text: '10:00', value: '10:00' },
            { text: '11:00', value: '11:00' },
        ],
    }
}

Events 事件示例

监听选择变化

js
const rule = {
    type: 'select',
    title: '下拉选择',
    field: 'select',
    value: '',
    props: {
        title: '预约',
        options: [
            { text: '周一', value: 'Monday' },
            { text: '周二', value: 'Tuesday' },
        ],
        placeholder: '请选择',
    },
    on: {
        confirm: (result) => {
            console.log('确认选择:', result);
        },
        cancel: () => {
            console.log('取消选择');
        },
        change: (result) => {
            console.log('选择值改变:', result);
        },
    },
}

选择后联动更新

js
const rule = [
    {
        type: 'select',
        title: '商品分类',
        field: 'category',
        value: '',
        props: {
            title: '选择分类',
            options: [
                { text: '电子产品', value: '1' },
                { text: '服装配饰', value: '2' },
            ],
        },
        inject: true,
        on: {
            confirm: ($inject, result) => {
                // 根据分类加载对应的子分类
                const value = result.selectedValues[0];
                if (value === '1') {
                    $inject.api.updateRule('subcategory', {
                        props: {
                            options: [
                                { text: '手机', value: '11' },
                                { text: '电脑', value: '12' },
                            ]
                        }
                    });
                } else if (value === '2') {
                    $inject.api.updateRule('subcategory', {
                        props: {
                            options: [
                                { text: '男装', value: '21' },
                                { text: '女装', value: '22' },
                            ]
                        }
                    });
                }
            },
        },
    },
    {
        type: 'select',
        title: '子分类',
        field: 'subcategory',
        props: {
            title: '选择子分类',
            options: [],
            placeholder: '请先选择分类',
            disabled: true,
        },
    },
]

完整配置项:Vant_Picker

value :Number | String

Options

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

Props

参数说明类型默认值
disabled是否禁用输入booleanfalse
options对象数组,配置每一列显示的数据PickerOption[] | PickerOption[][][]
columns-field-names自定义 options 结构中的字段object{ text: 'text', value: 'value', children: 'children' }
title顶部栏标题string-
confirm-button-text确认按钮文字,设置为空字符串可以隐藏按钮string确认
cancel-button-text取消按钮文字,设置为空字符串可以隐藏按钮string取消
toolbar-position顶部栏位置,可选值为 bottomstringtop
loading是否显示加载状态booleanfalse
readonly是否为只读状态,只读状态下无法切换选项booleanfalse
show-toolbar是否显示顶部栏booleantrue
allow-html是否允许选项内容中渲染 HTMLbooleanfalse
option-height选项高度,支持 px vw vh rem 单位,默认 pxnumber | string44
visible-option-num可见的选项个数number | string6
swipe-duration快速滑动时惯性滚动的时长,单位 msnumber | string1000

Events

事件名说明回调参数
confirm点击完成按钮时触发{ selectedValues, selectedOptions, selectedIndexes }
cancel点击取消按钮时触发{ selectedValues, selectedOptions, selectedIndexes }
change选中的选项改变时触发{ selectedValues, selectedOptions, selectedIndexes, columnIndex }
click-option点击选项时触发{ currentOption, selectedValues, selectedOptions, selectedIndexes, columnIndex }
scrollInto当用户通过点击或拖拽让一个选项滚动到中间的选择区域时触发{ currentOption, columnIndex }

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