1.动态添加表单及校验

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
<template>
<a-form-model
ref="refForm"
:model="form"
:label-col="labelCol"
:wrapper-col="wrapperCol"
>
<button @click="addType">添加类别</button>
<div
v-for="(item, index) in form.typeList"
style="border: 1px solid; width: 60%; margin: 10px auto"
:key="index"
>
<a-form-model-item
label="类别"
:prop="'typeList[' + index + ']' + '.type'"
:rules="[
{
required: true,
trigger: 'change',
message: '请选择类别',
},
{ validator: category },
]"
>
<a-select v-model="item.type" placeholder="请选择类别">
<a-select-option value="类别一">类别一</a-select-option>
<a-select-option value="类别二">类别二</a-select-option>
</a-select>
</a-form-model-item>
<div
v-for="(tab, indx) in item.tabs"
style="border: 1px solid; width: 60%; margin: 10px auto"
:key="indx"
>
<a-form-model-item
label="姓名"
:prop="'typeList[' + index + ']' + '.tabs[' + indx + ']'.name"
:rules="[
{
required: true,
trigger: 'blur',
message: '请选择姓名',
},
{ validator: category },
]"
>
<a-input v-model="tab.name" />
</a-form-model-item>
<a-form-model-item
label="年龄"
:prop="'typeList[' + index + ']' + '.tabs[' + indx + ']'.name"
:rules="[
{
required: true,
trigger: 'change',
message: '请选择年龄',
},
{ validator: category },
]"
>
<a-select v-model="tab.age" placeholder="请选择年龄">
<a-select-option value="年龄一">年龄一</a-select-option>
<a-select-option value="年龄二">年龄二</a-select-option>
</a-select>
</a-form-model-item>
<button @click="removeTab(tab, item.tabs)">删除个人信息</button>
</div>
<button @click="addTab(item.tabs)">添加个人信息</button>
<button @click="removeType(item)">删除类别</button>
</div>
<a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
<a-button type="primary" @click="onSubmit">提交表单</a-button>
<a-button type="primary" @click="edit">修改数据</a-button>
</a-form-model-item>
</a-form-model>
</template>

<script>
export default {
data() {
return {
labelCol: { span: 4 },
wrapperCol: { span: 14 },
form: {
typeList: [
{
type: "",
tabs: [
{
name: "",
age: "",
},
],
},
],
},
};
},
methods: {
onSubmit() {
this.$refs.refForm.validate((valid) => {
if (valid) {
alert("submit!");
} else {
console.log("error submit!!");
return false;
}
});
},
category(rule, value, callback) {
if (!value) {
callback(new Error("请选择"));
} else {
callback();
}
},

edit() {
this.form.typeList = [
{
type: "类别一",
tabs: [
{
name: "张三",
age: "年龄一",
},
{
name: "李四",
age: "年龄二",
},
],
},
{
type: "类别二",
tabs: [
{
name: "王五",
age: "年龄一",
},
{
name: "三小",
age: "年龄二",
},
],
},
];
},
addTab(tabs) {
tabs.push({
name: "",
age: "",
});
},
removeTab(tab, tabs) {
let index = tabs.indexOf(tab);
if (index !== -1) {
//当没有多余tab时就不再删除
tabs.splice(index, 1);
}
},
addType() {
this.form.typeList.push({
type: "",
tabs: [],
});
},
removeType(item) {
let index = this.form.typeList.indexOf(item);
if (index !== -1) {
//当没有多余tab时就不再删除
this.form.typeList.splice(index, 1);
}
},
},
};
</script>