index.vue
6.6 KB
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
<!--
* @Date: 2022-07-18 10:22:22
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2022-09-08 17:50:25
* @FilePath: /data-table/src/views/index.vue
* @Description: 首页
-->
<template>
<van-image v-if="table_cover" width="100%" height="200" :src="table_cover" />
<div v-if="table_title" class="table-title">{{ table_title }}</div>
<div class="table-box">
<van-form @submit="onSubmit">
<van-cell-group>
<component :ref="item.component_props.name" v-for="(item, index) in mockData" :key="index" :is="item.component" :item="item" @active="onActive" />
</van-cell-group>
<div style="margin: 16px;">
<van-button round block type="primary" native-type="submit">
提交
</van-button>
</div>
</van-form>
</div>
</template>
<script setup>
import { createComponentType } from '@/hooks/useComponentType'
import _ from 'lodash'
const table_cover = ref('');
const table_title = ref('');
const mockData = ref([]);
const postData = ref({})
onMounted(() => {
table_cover.value = 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg'
table_title.value = '这是一个表单的描述'
mockData.value = [
// {
// key: 'phone',
// value: '',
// label: '手机号',
// placeholder: '请输入手机号',
// component: '',
// component_props: {
// name: 'phone'
// },
// required: true,
// },
// {
// key: 'username',
// value: 'test',
// label: '用户名',
// placeholder: '请输入用户名',
// component: '',
// component_props: {
// name: 'text'
// },
// required: true,
// },
// {
// key: 'email',
// value: '',
// label: '邮箱',
// placeholder: '请输入邮箱',
// component: '',
// component_props: {
// name: 'email'
// },
// required: true,
// },
// {
// key: 'age',
// value: '',
// label: '年龄',
// placeholder: '请输入年龄',
// component: '',
// component_props: {
// name: 'number'
// },
// required: true,
// },
// {
// key: 'gender',
// value: '',
// label: '性别',
// placeholder: '',
// component: '',
// component_props: {
// name: 'radio',
// direction: 'horizontal'
// },
// options: [{
// key: '男',
// value: '男'
// }, {
// key: '女',
// value: '女'
// }]
// },
// {
// key: 'hobby',
// value: [],
// label: '兴趣爱好',
// component: '',
// component_props: {
// name: 'checkbox',
// direction: 'horizontal',
// max: '3'
// },
// options: [{
// key: '足球',
// value: '足球'
// }, {
// key: '篮球',
// value: '篮球'
// }, {
// key: '羽毛球',
// value: '羽毛球'
// }, {
// key: '乒乓球',
// value: '乒乓球'
// }]
// },
// {
// key: 'message',
// value: '一种可以用来记录,展示文字信息的载体,有比较强的时效性。一般以黑板、木板为载体。用来留言。各种各样的留言使用。留言板还有引申的“网络留言板”。这个和网络留言本不一样的地方是留言板一般比较集中的反应信息的。',
// label: '留言',
// placeholder: '请输入留言',
// component: '',
// component_props: {
// name: 'textarea',
// rows: 3,
// maxlength: null,
// },
// },
// {
// key: 'vehicle',
// value: '自行车',
// label: '交通工具',
// placeholder: '请选择交通工具',
// component: '',
// component_props: {
// name: 'picker'
// },
// options: [
// { text: '自行车', value: '自行车' },
// { text: '汽车', value: '汽车' },
// { text: '地铁', value: '地铁' },
// ],
// required: true,
// },
// {
// key: 'sign',
// value: '',
// label: '电子签名',
// placeholder: '',
// component: '',
// component_props: {
// name: 'sign',
// },
// required: true,
// },
// {
// key: 'city',
// value: '天津市/天津市/和平区',
// city_code: '120101',
// label: '地址',
// address: '',
// placeholder: '请选择省市区',
// component_props: {
// name: 'area_picker'
// },
// },
// {
// key: 'date',
// value: '2022-10',
// label: '日期选择',
// placeholder: '请选择日期',
// component_props: {
// name: 'date_picker',
// title: '请选择',
// min_date: new Date(),
// columns_type: ['year', 'month']
// },
// },
// {
// key: 'time',
// value: '',
// label: '时间选择',
// placeholder: '请选择时间',
// component_props: {
// name: 'time_picker',
// title: '请选择',
// columns_type: ['hour', 'minute']
// },
// required: true,
// },
// {
// key: 'image_src',
// value: '',
// label: '图片上传',
// component_props: {
// name: 'image_uploader',
// image_type: ['jpg', 'png'],
// multiple: false
// }
// }
// {
// key: 'datetime',
// value: '2022-06-01 12:00',
// label: '日期时间',
// placeholder: '请选择日期时间',
// component_props: {
// name: 'datetime_picker',
// title: '请选择',
// minDate: new Date(),
// },
// required: true,
// },
{
key: 'rate',
value: '',
label: '评分',
placeholder: '请选择评分',
component_props: {
name: 'rate_picker',
count: 10
},
required: true,
}
];
// 生成自定义组件
createComponentType(mockData.value)
})
const sign = ref(null);
const rate_picker = ref(null);
const onSubmit = (values) => {
// 合并自定义字段到提交表单字段
postData.value = _.assign(postData.value, values);
// console.warn(mockData.value);
// 检查非表单输入项
if (validOther()) { // 通过验证
console.warn(postData.value);
console.warn('通过验证');
} else {
console.warn('不通过验证');
}
};
const onActive = (item) => {
// 返回自定义字段
if (item.key === 'sign') {
postData.value['sign'] = item.value
}
if (item.key === 'rate') {
postData.value['rate'] = item.value
}
}
const validOther = () => {
// 检验没有绑定name的输入项
let flag = true;
if (sign.value) { // 检验电子签名
flag = sign.value[0].validSign();
}
if (rate_picker.value) { // 检验评分
flag = rate_picker.value[0].validRate();
}
return flag;
}
</script>
<style lang="less" scoped>
.table-title {
padding: 1rem;
}
.table-box {
padding: 1rem;
}
</style>