Your Name
2022-06-27 0a3027eccd3b0bb6542e5fc831f7e02740dcfc95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<template>
    <div class="dynamic-form-container">
        <el-card shadow="hover" header="动态复杂表单">
            <el-form :model="form" ref="formRulesOneRef" size="default" label-width="100px" class="mt35">
                <el-row :gutter="35">
                    <el-col
                        :xs="val.xs"
                        :sm="val.sm"
                        :md="val.md"
                        :lg="val.md"
                        :xl="val.xl"
                        class="mb20"
                        v-show="val.isShow"
                        v-for="(val, key) in formData"
                        :key="key"
                    >
                        <template v-if="val.type !== ''">
                            <el-form-item
                                :label="val.label"
                                :prop="val.prop"
                                :rules="[{ required: val.required, message: `${val.label}不能为空`, trigger: val.type === 'input' ? 'blur' : 'change' }]"
                                v-if="val.type !== ''"
                            >
                                <el-input
                                    v-model="form[val.prop]"
                                    :placeholder="val.placeholder"
                                    clearable
                                    v-if="val.type === 'input'"
                                    style="width: 100%"
                                    :disabled="val.disabled"
                                ></el-input>
                                <el-date-picker
                                    v-model="form[val.prop]"
                                    type="date"
                                    :placeholder="val.placeholder"
                                    v-else-if="val.type === 'date'"
                                    style="width: 100%"
                                    :disabled="val.disabled"
                                >
                                </el-date-picker>
                                <el-select
                                    v-model="form[val.prop]"
                                    :placeholder="val.placeholder"
                                    v-else-if="val.type === 'select'"
                                    style="width: 100%"
                                    :disabled="val.disabled"
                                >
                                    <el-option v-for="item in val.options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                                </el-select>
                                <el-input
                                    type="textarea"
                                    v-model="form[val.prop]"
                                    :placeholder="val.placeholder"
                                    clearable
                                    v-if="val.type === 'textarea'"
                                    style="width: 100%"
                                    :disabled="val.disabled"
                                ></el-input>
                            </el-form-item>
                        </template>
                        <template v-else>
                            <el-row :gutter="35" v-for="(v, k) in form.list" :key="k">
                                <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="6" class="mb20">
                                    <el-form-item label="年度" :prop="`list[${k}].year`" :rules="[{ required: true, message: `年度不能为空`, trigger: 'blur' }]">
                                        <template #label>
                                            <el-button type="primary" circle size="small" @click="onAddRow" v-if="k === 0">
                                                <el-icon>
                                                    <ele-Plus />
                                                </el-icon>
                                            </el-button>
                                            <el-button type="danger" circle size="small" @click="onDelRow(k)" v-else>
                                                <el-icon>
                                                    <ele-Delete />
                                                </el-icon>
                                            </el-button>
                                            <span class="ml10">年度</span>
                                        </template>
                                        <el-input v-model="form.list[k].year" style="width: 100%" placeholder="请输入"> </el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="6" class="mb20">
                                    <el-form-item label="月度" :prop="`list[${k}].month`" :rules="[{ required: true, message: `月度不能为空`, trigger: 'blur' }]">
                                        <el-input v-model="form.list[k].month" style="width: 100%" placeholder="请输入"> </el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="6" class="mb20">
                                    <el-form-item label="日度" :prop="`list[${k}].day`" :rules="[{ required: true, message: `日度不能为空`, trigger: 'blur' }]">
                                        <el-input v-model="form.list[k].day" style="width: 100%" placeholder="请输入"> </el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </template>
                    </el-col>
                </el-row>
            </el-form>
        </el-card>
        <el-row class="flex mt15">
            <div class="flex-margin">
                <el-button size="default" @click="onResetForm">
                    <el-icon>
                        <ele-RefreshRight />
                    </el-icon>
                    重置表单
                </el-button>
                <el-button size="default" type="primary" @click="onSubmitForm">
                    <SvgIcon name="iconfont icon-shuxing" />
                    验证表单
                </el-button>
            </div>
        </el-row>
    </div>
</template>
 
<script lang="ts">
import { toRefs, reactive, onMounted, getCurrentInstance, defineComponent } from 'vue';
import { formData } from './mock';
 
// 定义接口来定义对象的类型
interface FormDataOptions {
    label: string;
    value: string;
}
interface FormDataState {
    label: string;
    prop: string;
    placeholder: string;
    clearable: boolean;
    disabled: boolean;
    required: boolean;
    type: string;
    i18n: boolean;
    i18nText: string;
    isShow: boolean;
    xs: number;
    sm: number;
    md: number;
    lg: number;
    xl: number;
    options?: FormDataOptions[];
}
interface DynamicFormState {
    formData: FormDataState[];
    form: any;
}
 
export default defineComponent({
    name: 'pagesDynamicForm',
    setup() {
        const { proxy } = <any>getCurrentInstance();
        const state = reactive<DynamicFormState>({
            formData,
            form: {
                name: '',
                email: '',
                autograph: '',
                occupation: '',
                list: [
                    {
                        year: '',
                        month: '',
                        day: '',
                    },
                ],
                remarks: '',
            },
        });
        // 新增行
        const onAddRow = () => {
            state.form.list.push({
                year: '',
                month: '',
                day: '',
            });
        };
        // 删除行
        const onDelRow = (k: number) => {
            state.form.list.splice(k, 1);
        };
        // 表单验证
        const onSubmitForm = () => {
            proxy.$refs.formRulesOneRef.validate((valid: boolean) => {
                if (valid) {
                    proxy.$message.success('验证成功');
                } else {
                    return false;
                }
            });
        };
        // 重置表单
        const onResetForm = () => {
            proxy.$refs.formRulesOneRef.resetFields();
        };
        // 页面加载时
        onMounted(() => {});
        return {
            onAddRow,
            onDelRow,
            onSubmitForm,
            onResetForm,
            ...toRefs(state),
        };
    },
});
</script>