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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<template>
<el-form
ref="form"
:model="form"
:disabled="options.disabled"
:label-position="options.labelPosition || 'left'"
:label-width="(options.labelWidth || 100) + 'px'"
:size="size"
@submit.native.prevent="handleConfirm()"
:style="formStyle"
class="avue-form-self"
>
<template v-if="options.column && options.column.length > 0">
<form-content
ref="form-content"
:form="form"
:columns="{ column: options.column }"
:options="options"
:is-show-important="isShowImportant"
></form-content>
</template>
<template v-if="options.group && options.group.length > 0">
<el-tabs v-if="options.tabs" v-model="activeName" type="card">
<el-tab-pane
:label="g.label || `标签${gIndex + 1}`"
:name="`${gIndex}`"
v-for="(g, gIndex) in options.group"
:key="gIndex"
>
<form-content
ref="form-content"
:form="form"
:columns="g"
:options="options"
:is-show-important="isShowImportant"
></form-content>
</el-tab-pane>
</el-tabs>
<el-collapse v-model="collapseNames" v-else>
<template v-for="(g, gIndex) in options.group">
<el-collapse-item
:title="g.label"
:name="g.prop"
:key="gIndex"
v-show="g.display"
:disabled="!g.arrow"
>
<form-content
v-if="collapseNames.includes(g.prop)"
ref="form-content"
:form="form"
:columns="g"
:options="options"
:is-show-important="isShowImportant"
></form-content>
</el-collapse-item>
</template>
</el-collapse>
</template>
<div class="form-footer">
<template v-if="options.menuBtn">
<template v-if="options.submitBtn">
<el-button
type="primary"
icon="el-icon-check"
@click="handleConfirm"
:loading="loading"
size="large"
>
{{ options.submitText || "提交" }}
</el-button>
</template>
<template v-if="options.emptyBtn">
<el-button
plain
icon="el-icon-delete "
size="large"
@click="resetForm(true)"
>
{{ options.emptyText || "清空" }}
</el-button>
</template>
</template>
<template v-if="nextTabBtnShow">
<el-form style="display: inline-block; margin-left: 10px">
<el-button
plain
icon="el-icon-caret-right"
size="large"
@click="nextTab"
>
{{ options.nextTabText || "下一页" }}
</el-button>
</el-form>
</template>
</div>
</el-form>
</template>
<script>
/**
* @description 自定义表单
*/
import handleFormData from "./handleFormData"
import FormContent from "./FormContent"
import { isObject } from "@/utils/validate"
export default {
name: "CustomForm",
components: { FormContent },
props: {
options: { //配置 数据
type: Object,
default: () => {
return {}
},
},
size: { type: String, default: "small" },
formStyle: { type: Object },
formEdit: {
type: Object,
default: () => {
return {}
},
},
},
provide() {
const vwForm = {}
for (const k in this.options) {
if (k !== "column" && k !== "group") {
vwForm[k] = this.options[k]
}
}
return {
vwForm,
}
},
mixins: [handleFormData],
data() {
return {
collapseNames: [],
activeName: "0",
loading: false,
isShowImportant: false,
form: {},
}
},
methods: {
nextTab() {
const tabsLen = this.options.group.length
let active = Number(this.activeName)
let next = ++active
if (next >= tabsLen) next = 0
this.activeName = String(next)
this.$emit("scrollTop")
},
imFieldChange(val = false) {
this.isShowImportant = val
},
// 查询
submit() {
this.$emit("submit", this.form)
},
// 重置
resetForm(flag) {
if (flag) {
this.$confirm("是否清空数据?", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
})
.then(() => {
this.$message({
type: "success",
message: "清空成功!",
})
this.$refs.form.resetFields()
})
.catch(() => {})
} else {
if (this.$refs.form) this.$refs.form.resetFields()
}
},
validate(func) {
this.$refs.form.validate((valid) => {
func(valid, () => {})
})
},
handleConfirm() {
this.loading = true
this.$refs.form.validate((valid) => {
if (valid) {
const data = {}
const form = this.deepClone(this.form)
Object.keys(form).forEach((k) => {
if (form[k] === undefined) {
data[k] = ""
return false
}
if (k === "YZZKJC") {
// 牙周表格
data[k] = JSON.stringify(form[k])
} else if (
Array.isArray(form[k]) &&
form[k][0] &&
isObject(form[k][0])
) {
// 子表单 去除前端添加的显隐辅助数据($_)和 删除按钮辅助数据(showDelBtn)
data[k] = form[k].map((item) => {
for (let key in item) {
if (["$_keyField", "$_hidden", "showDelBtn"].includes(key))
delete item[key]
}
return item
})
} else {
data[k] = form[k]
}
})
this.$emit("handleConfirm", data, () => {
this.loading = false
})
} else {
this.loading = false
}
})
},
// 表单赋值
initforms() {
const form = {}
const collapseNames = []
const initFunc = (column = []) => {
if (column.length > 0) {
column.forEach((item) => {
if (!item.prop) return false
if (
item.type === "dynamic" ||
item.type === "checkbox" ||
item.type === "cascader" ||
item.type === "upload" ||
(item.type === "select" && item.multiple)
) {
form[item.prop] = []
} else if (
item.type === "dental-tab" ||
item.type === "dental-tab-tj"
) {
form[item.prop] = {}
} else if (item.type === "group") {
initFunc(item.children.column)
} else if (item.type !== "title") {
form[item.prop] = item.value ? item.value : ""
}
})
}
}
const { group, column } = this.options
initFunc(column)
group &&
group.forEach((g) => {
if (g.collapse) {
collapseNames.push(g.prop)
}
initFunc(g.column)
})
this.collapseNames = collapseNames
this.form = form
this.loading = false
this.$nextTick(() => {
// 子组件数据初始化完成后
setTimeout(() => {
this.initfields(this.formEdit)
}, 0)
if (this.$refs.form && this.$refs.form.clearValidate) {
this.$refs.form.clearValidate()
}
})
},
// 绑定部分值(此时表单已渲染)
initfields(obj) {
for (let k in obj) {
if (this.form.hasOwnProperty(k)) {
this.form[k] = obj[k]
}
}
},
},
computed: {
nextTabBtnShow() {
const { nextTabBtn, tabs, group } = this.options
return tabs && group && group.length > 1 && nextTabBtn
},
},
watch: {
formEdit: {
handler() {
this.initfields(this.formEdit)
},
},
},
created() {
this.initforms()
this.$nextTick(() => {
// this.setformWatch(this.options, "form")
})
},
}
</script>
<style lang="scss" scoped>
.avue-form-self {
padding: 0 11px;
::v-deep .el-collapse {
border: none;
.el-collapse-item__header.is-active {
border-bottom-color: #ebeef5;
}
.el-collapse-item__wrap {
border: none;
padding: 10px;
&:hover {
background-color: #ecf8ff;
outline: 1px dashed #ccc;
}
}
.el-collapse-item__header {
font-size: 16px;
font-weight: 600;
color: rgba(0, 0, 0, 0.85);
height: 50px;
line-height: 50px;
}
}
.form-footer {
margin-top: 20px;
text-align: center;
}
}
::v-deep .el-col {
}
::v-deep .el-radio__input.is-disabled .el-radio__inner {
// background-color: $base-color-default;
// border-color: $base-color-default;
}
</style>