Skip to content

DynamicGroup 动态列表 ​

动态增减表单项组件,支持动态添加和删除表单项。

类型标识 ​

  • t: 'dynamic-group'

额外 Props ​

属性名类型默认值描述
labelstring-分组标题
config(option: DynamicConfigOption) => JSONFormOption[]-子级表单项配置
cellGroupPropsPartial<CellGroupProps>-VanCellGroup 组件的属性
cellGroupOnobject{}VanCellGroup 组件的事件
cellGroupSlotsobject-VanCellGroup 组件的插槽

DynamicConfigOption ​

参数类型描述
itemRecord<string, any>当前项的数据
indexnumber当前项的索引
queryRecord<string, any>表单的 model 对象
wrapperReturnType<typeof useWrapper>表单级的共用属性

Slots ​

插槽名绑定值描述
append{ query }底部插槽,用于添加新项
prepend{ query }顶部插槽

ItemSlots ​

插槽名绑定值描述
append{ checked, index }每项底部插槽,用于删除操作
prepend{ checked, index }每项顶部插槽

示例 ​

基础用法 ​

tsx
const formConfig = defineOption([
    {
        field: 'users',
        t: 'dynamic-group',
        label: '用户列表',
        config: ({ item, index }) => ([
            {
                field: 'name',
                t: 'input',
                label: '姓名',
                placeholder: '请输入姓名',
            },
            {
                field: 'age',
                t: 'stepper',
                label: '年龄',
                min: 0,
                max: 120,
            },
        ]),
        itemSlots: {
            append: ({ checked, index }) => (
                <VanButton
                    onClick={() => checked.splice(index, 1)}
                    type="danger"
                    size="small"
                >
                    删除
                </VanButton>
            ),
        },
        slots: {
            append: ({ query }) => (
                <VanButton
                    onClick={() => query.users.push({ name: '', age: 0 })}
                    type="primary"
                >
                    添加用户
                </VanButton>
            ),
        },
    },
]);

复杂表单 ​

tsx
const formConfig = defineOption([
    {
        field: 'educationList',
        t: 'dynamic-group',
        label: '教育经历',
        config: ({ item, index }) => ([
            {
                field: 'school',
                t: 'input',
                label: '学校',
                placeholder: '请输入学校名称',
            },
            {
                field: 'major',
                t: 'input',
                label: '专业',
                placeholder: '请输入专业',
            },
            {
                field: 'startDate',
                t: 'date-picker',
                label: '开始时间',
            },
            {
                field: 'endDate',
                t: 'date-picker',
                label: '结束时间',
            },
        ]),
        itemSlots: {
            append: ({ checked, index }) => (
                <VanIcon
                    name="delete"
                    color="#ee0a24"
                    onClick={() => checked.splice(index, 1)}
                />
            ),
        },
        slots: {
            append: ({ query }) => (
                <VanCell
                    title="添加教育经历"
                    icon="plus"
                    isLink
                    onClick={() => query.educationList.push({
                        school: '',
                        major: '',
                        startDate: '',
                        endDate: '',
                    })}
                />
            ),
        },
    },
]);