|
| 1 | +<docs> |
| 2 | +--- |
| 3 | +order: 4.2 |
| 4 | +title: |
| 5 | + zh-CN: 复杂的动态增减表单项 |
| 6 | + en-US: Complex Dynamic Form Item |
| 7 | +--- |
| 8 | + |
| 9 | +## zh-CN |
| 10 | + |
| 11 | +这个例子演示了一个表单中包含多个表单控件的情况。 |
| 12 | + |
| 13 | +## en-US |
| 14 | + |
| 15 | +This example demonstrates the case that a form contains multiple form controls. |
| 16 | + |
| 17 | +</docs> |
| 18 | +<template> |
| 19 | + <a-form |
| 20 | + ref="formRef" |
| 21 | + name="dynamic_form_nest_item" |
| 22 | + :model="dynamicValidateForm" |
| 23 | + @finish="onFinish" |
| 24 | + > |
| 25 | + <a-form-item name="area" label="Area" :rules="[{ required: true, message: 'Missing area' }]"> |
| 26 | + <a-select v-model:value="dynamicValidateForm.area" :options="areas" /> |
| 27 | + </a-form-item> |
| 28 | + <a-space |
| 29 | + v-for="(sight, index) in dynamicValidateForm.sights" |
| 30 | + :key="sight.id" |
| 31 | + style="display: flex; margin-bottom: 8px" |
| 32 | + align="baseline" |
| 33 | + > |
| 34 | + <a-form-item |
| 35 | + :name="['sights', index, 'value']" |
| 36 | + label="Sight" |
| 37 | + :rules="{ |
| 38 | + required: true, |
| 39 | + message: 'Missing sight', |
| 40 | + }" |
| 41 | + > |
| 42 | + <a-select |
| 43 | + v-model:value="sight.value" |
| 44 | + :disabled="!dynamicValidateForm.area" |
| 45 | + :options="(sights[dynamicValidateForm.area] || []).map(a => ({ value: a }))" |
| 46 | + style="width: 130px" |
| 47 | + ></a-select> |
| 48 | + </a-form-item> |
| 49 | + <a-form-item |
| 50 | + label="Price" |
| 51 | + :name="['sights', index, 'price']" |
| 52 | + :rules="{ |
| 53 | + required: true, |
| 54 | + message: 'Missing price', |
| 55 | + }" |
| 56 | + > |
| 57 | + <a-input v-model:value="sight.price" /> |
| 58 | + </a-form-item> |
| 59 | + <MinusCircleOutlined @click="removeSight(sight)" /> |
| 60 | + </a-space> |
| 61 | + <a-form-item> |
| 62 | + <a-button type="dashed" block @click="addSight"> |
| 63 | + <PlusOutlined /> |
| 64 | + Add sights |
| 65 | + </a-button> |
| 66 | + </a-form-item> |
| 67 | + <a-form-item> |
| 68 | + <a-button type="primary" html-type="submit">Submit</a-button> |
| 69 | + </a-form-item> |
| 70 | + </a-form> |
| 71 | +</template> |
| 72 | + |
| 73 | +<script lang="ts"> |
| 74 | +import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons-vue'; |
| 75 | +import { defineComponent, reactive, ref, watch } from 'vue'; |
| 76 | +import type { FormInstance } from 'ant-design-vue'; |
| 77 | +
|
| 78 | +interface Sights { |
| 79 | + value: string; |
| 80 | + price: string; |
| 81 | + id: number; |
| 82 | +} |
| 83 | +export default defineComponent({ |
| 84 | + components: { |
| 85 | + MinusCircleOutlined, |
| 86 | + PlusOutlined, |
| 87 | + }, |
| 88 | + setup() { |
| 89 | + const areas = [ |
| 90 | + { label: 'Beijing', value: 'Beijing' }, |
| 91 | + { label: 'Shanghai', value: 'Shanghai' }, |
| 92 | + ]; |
| 93 | +
|
| 94 | + const sights = { |
| 95 | + Beijing: ['Tiananmen', 'Great Wall'], |
| 96 | + Shanghai: ['Oriental Pearl', 'The Bund'], |
| 97 | + }; |
| 98 | +
|
| 99 | + const formRef = ref<FormInstance>(); |
| 100 | + const dynamicValidateForm = reactive<{ sights: Sights[]; area: string }>({ |
| 101 | + sights: [], |
| 102 | + area: undefined, |
| 103 | + }); |
| 104 | + watch( |
| 105 | + () => dynamicValidateForm.area, |
| 106 | + () => { |
| 107 | + dynamicValidateForm.sights = []; |
| 108 | + }, |
| 109 | + ); |
| 110 | + const removeSight = (item: Sights) => { |
| 111 | + let index = dynamicValidateForm.sights.indexOf(item); |
| 112 | + if (index !== -1) { |
| 113 | + dynamicValidateForm.sights.splice(index, 1); |
| 114 | + } |
| 115 | + }; |
| 116 | + const addSight = () => { |
| 117 | + dynamicValidateForm.sights.push({ |
| 118 | + value: undefined, |
| 119 | + price: undefined, |
| 120 | + id: Date.now(), |
| 121 | + }); |
| 122 | + }; |
| 123 | + const onFinish = values => { |
| 124 | + console.log('Received values of form:', values); |
| 125 | + console.log('dynamicValidateForm:', dynamicValidateForm); |
| 126 | + }; |
| 127 | + return { |
| 128 | + formRef, |
| 129 | + dynamicValidateForm, |
| 130 | + onFinish, |
| 131 | + removeSight, |
| 132 | + addSight, |
| 133 | + areas, |
| 134 | + sights, |
| 135 | + }; |
| 136 | + }, |
| 137 | +}); |
| 138 | +</script> |
0 commit comments