|
| 1 | +<template> |
| 2 | + <PageWrapper title="表单增删示例"> |
| 3 | + <CollapseContainer title="表单增删"> |
| 4 | + <BasicForm @register="register" @submit="handleSubmit"> |
| 5 | + <template #add="{ field }"> |
| 6 | + <Button v-if="field === 1" @click="add">+</Button> |
| 7 | + <Button v-if="field > 1" @click="del(field)">-</Button> |
| 8 | + </template> |
| 9 | + </BasicForm> |
| 10 | + </CollapseContainer> |
| 11 | + </PageWrapper> |
| 12 | +</template> |
| 13 | +<script lang="ts"> |
| 14 | + import { defineComponent, ref } from 'vue'; |
| 15 | + import { BasicForm, useForm } from '/@/components/Form/index'; |
| 16 | + import { CollapseContainer } from '/@/components/Container/index'; |
| 17 | + import { Input } from 'ant-design-vue'; |
| 18 | + import { PageWrapper } from '/@/components/Page'; |
| 19 | + import { Button } from '/@/components/Button'; |
| 20 | +
|
| 21 | + export default defineComponent({ |
| 22 | + components: { BasicForm, CollapseContainer, PageWrapper, [Input.name]: Input, Button }, |
| 23 | + setup() { |
| 24 | + const [register, { appendSchemaByField, removeSchemaByFiled, validate }] = useForm({ |
| 25 | + schemas: [ |
| 26 | + { |
| 27 | + field: 'field1a', |
| 28 | + component: 'Input', |
| 29 | + label: '字段1', |
| 30 | + colProps: { |
| 31 | + span: 8, |
| 32 | + }, |
| 33 | + required: true, |
| 34 | + }, |
| 35 | + { |
| 36 | + field: 'field1b', |
| 37 | + component: 'Input', |
| 38 | + label: '字段1', |
| 39 | + colProps: { |
| 40 | + span: 8, |
| 41 | + }, |
| 42 | + required: true, |
| 43 | + }, |
| 44 | + { |
| 45 | + field: '1', |
| 46 | + component: 'Input', |
| 47 | + label: ' ', |
| 48 | + colProps: { |
| 49 | + span: 8, |
| 50 | + }, |
| 51 | + slot: 'add', |
| 52 | + }, |
| 53 | + ], |
| 54 | + labelWidth: 100, |
| 55 | + actionColOptions: { span: 24 }, |
| 56 | + }); |
| 57 | +
|
| 58 | + async function handleSubmit() { |
| 59 | + try { |
| 60 | + const data = await validate(); |
| 61 | + console.log(data); |
| 62 | + } catch (e) { |
| 63 | + console.log(e); |
| 64 | + } |
| 65 | + } |
| 66 | +
|
| 67 | + const n = ref(2); |
| 68 | +
|
| 69 | + function add() { |
| 70 | + appendSchemaByField( |
| 71 | + { |
| 72 | + field: 'field' + n.value + 'a', |
| 73 | + component: 'Input', |
| 74 | + label: '字段2', |
| 75 | + colProps: { |
| 76 | + span: 8, |
| 77 | + }, |
| 78 | + required: true, |
| 79 | + }, |
| 80 | + '' |
| 81 | + ); |
| 82 | + appendSchemaByField( |
| 83 | + { |
| 84 | + field: 'field' + n.value + 'b', |
| 85 | + component: 'Input', |
| 86 | + label: '字段2', |
| 87 | + colProps: { |
| 88 | + span: 8, |
| 89 | + }, |
| 90 | + required: true, |
| 91 | + }, |
| 92 | + '' |
| 93 | + ); |
| 94 | + appendSchemaByField( |
| 95 | + { |
| 96 | + field: `${n.value}`, |
| 97 | + component: 'Input', |
| 98 | + label: ' ', |
| 99 | + colProps: { |
| 100 | + span: 8, |
| 101 | + }, |
| 102 | + slot: 'add', |
| 103 | + }, |
| 104 | + '' |
| 105 | + ); |
| 106 | + n.value++; |
| 107 | + } |
| 108 | +
|
| 109 | + function del(field) { |
| 110 | + console.log(field); |
| 111 | + removeSchemaByFiled([`field${field}a`, `field${field}b`, `${field}`]); |
| 112 | + } |
| 113 | +
|
| 114 | + return { register, handleSubmit, add, del }; |
| 115 | + }, |
| 116 | + }); |
| 117 | +</script> |
0 commit comments