gaoshp
2024-06-23 fbee7228e2f6e43b417d4c3f03020704831261cd
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<template>
    <el-main>
        <el-row :gutter="15">
            <el-col :lg="6">
                <el-card shadow="never" header="处理逻辑说明">
                    <el-timeline>
                        <el-timeline-item timestamp="两者数据并无顺序要求,只是获取表单值时需要自行设置loading">1.远程获取表单配置参数和表单的值</el-timeline-item>
                        <el-timeline-item timestamp="构建form对象, 循环渲染对应组件, 获取远程选项数据. 完成前组件将由骨架代替">2.根据配置构建表单组件</el-timeline-item>
                        <el-timeline-item timestamp="合并form对象和表单值">3.给表单赋值</el-timeline-item>
                    </el-timeline>
                    <el-alert title="当前版本为预览版本,并未在正式项目中实施,请谨慎使用. 更多配置以及图形化编辑器正在路上." type="warning" show-icon></el-alert>
                </el-card>
                <el-card shadow="never" header="双向绑定">
                    <el-button type="primary" @click="setName">改变输入框的值</el-button>
                    <el-button type="primary" @click="setConfig">改变指定栅格配置</el-button>
                </el-card>
            </el-col>
            <el-col :lg="12">
                <el-card shadow="never" header="动态表单渲染器">
                    <sc-form ref="formref" :config="config" v-model="form" :loading="loading">
                        <el-button type="primary" @click="save">保 存</el-button>
                        <el-button @click="reset">重 置</el-button>
                    </sc-form>
                </el-card>
            </el-col>
            <el-col :lg="6">
                <el-card shadow="never" header="表单输出">
                    <pre>{{form}}</pre>
                </el-card>
            </el-col>
        </el-row>
    </el-main>
</template>
 
<script>
    export default {
        name: 'formRender',
        data() {
            return {
                loading: false,
                config: {},
                form: {}
            }
        },
        mounted() {
            //模拟请求延迟, 配置参数和form值并无顺序要求,可同时请求
            const config = {
                labelWidth: '130px',
                labelPosition: 'right',
                size: 'medium',
                formItems: [
                    {
                        label: "ElementPlus",
                        component: "title",
                    },
                    {
                        label: "输入框",
                        name: "name",
                        value: "",
                        component: "input",
                        options: {
                            maxlength: "20",
                            placeholder: "Activity name",
                        },
                        rules: [
                            {required: true, message: "Please input Activity name", trigger: "blur"}
                        ],
                        requiredHandle: "$.required==true",
                    },
                    {
                        label: "栅格(12/24)",
                        name: "name2",
                        value: "",
                        component: "input",
                        span: 12,
                        options: {
                            placeholder: "span: 12",
                        }
                    },
                    {
                        label: "栅格(12/24)",
                        name: "name3",
                        value: "",
                        component: "input",
                        span: 12,
                        options: {
                            placeholder: "span: 12",
                        }
                    },
                    {
                        label: "选择器(多选)",
                        name: "select",
                        value: "",
                        component: "select",
                        span: 24,
                        options: {
                            remote: {
                                api: `${this.$CONFIG.API_URL}/system/dic/get`,
                                data: {name: 'a'}
                            },
                            multiple: true,
                            items:[
                                {
                                    label: "选项1",
                                    value: "1"
                                },
                                {
                                    label: "选项2",
                                    value: "2"
                                }
                            ]
                        },
                        rules: [
                            {required: true, message: "Please input Activity name", trigger: "change"}
                        ],
                        requiredHandle: "$.required==true",
                    },
                    {
                        label: "选择器(单选)",
                        name: "select2",
                        value: "",
                        component: "select",
                        span: 24,
                        options: {
                            remote: {
                                api: `${this.$CONFIG.API_URL}/system/dic/get`,
                                data: {name: 'b'}
                            },
                            items:[
                                {
                                    label: "选项1",
                                    value: "1"
                                },
                                {
                                    label: "选项2",
                                    value: "2"
                                }
                            ]
                        }
                    },
                    {
                        label: "级联选择器",
                        name: "cascader",
                        value: "",
                        component: "cascader",
                        span: 24,
                        options: {
                            items:[
                                {
                                    label: "Guide",
                                    value: "guide",
                                    children: [
                                        {
                                            label: "Disciplines",
                                            value: "disciplines"
                                        },
                                        {
                                            label: "Consistency",
                                            value: "consistency"
                                        },
                                    ]
                                },
                                {
                                    label: "Resource",
                                    value: "resource",
                                    children: [
                                        {
                                            label: "Axure Components",
                                            value: "axure"
                                        },
                                        {
                                            label: "Sketch Templates",
                                            value: "sketch"
                                        },
                                        {
                                            label: "Design Documentation",
                                            value: "docs"
                                        }
                                    ]
                                },
                                {
                                    label: "Component",
                                    value: "component"
                                },
                            ]
                        }
                    },
                    {
                        label: "多选框",
                        name: "checkbox",
                        component: "checkbox",
                        span: 24,
                        tips: "多选框配置加上 name 表示拥有嵌套关系。否则将值“平铺”在form对象",
                        options: {
                            items:[
                                {
                                    label: "选项1",
                                    name: "option1",
                                    value: false
                                },
                                {
                                    label: "选项2",
                                    name: "option2",
                                    value: false
                                }
                            ]
                        },
                        hideHandle: "$.required==true"
                    },
                    {
                        label: "多选框组",
                        name: "checkboxGroup",
                        value: [],
                        component: "checkboxGroup",
                        span: 24,
                        options: {
                            items:[
                                {
                                    label: "选项1",
                                    value: "option1"
                                },
                                {
                                    label: "选项2",
                                    value: "option2"
                                }
                            ]
                        },
                        hideHandle: "$.required==true"
                    },
                    {
                        label: "单选",
                        name: "radio",
                        value: "1",
                        component: "radio",
                        options: {
                            items:[
                                {
                                    label: "选项1",
                                    value: "1"
                                },
                                {
                                    label: "选项2",
                                    value: "2"
                                }
                            ]
                        },
                        hideHandle: "$.required==true"
                    },
                    {
                        label: "开关",
                        name: "required",
                        message: "演示如何使用表达式动态显隐和必填,试试打开和关闭开关",
                        value: false,
                        component: "switch",
                    },
                    {
                        label: "日期/时间",
                        name: "date",
                        value: "",
                        component: "date",
                        options: {
                            type: "datetime",
                            valueFormat: "YYYY-MM-DD HH:mm:ss",
                        },
                        rules: [
                            {required: true, message: "Please input Data", trigger: "change"}
                        ],
                    },
                    {
                        label: "滑块",
                        name: "slider",
                        value: 0,
                        component: "slider",
                        options: {
                            marks: {
                                0: '0°C',
                                8: '8°C',
                                37: '37°C'
                            }
                        }
                    },
                    {
                        label: "数值",
                        name: "number",
                        value: 0,
                        component: "number",
                    },
                    {
                        label: "颜色",
                        name: "color",
                        value: "",
                        component: "color",
                    },
                    {
                        label: "评分",
                        name: "rate",
                        value: 0,
                        component: "rate",
                    },
                    {
                        label: "SCUI扩展",
                        component: "title",
                    },
                    {
                        label: "表格选择器",
                        name: "tableselect",
                        value: {},
                        component: "tableselect",
                        span: 24,
                        options: {
                            apiObj: '$API.demo.page',
                            column: [
                                {
                                    label: "ID",
                                    prop: "id",
                                    width:150
                                },
                                {
                                    label: "姓名",
                                    prop: "user"
                                }
                            ],
                            props: {
                                label: 'user',
                                value: 'id',
                                keyword: "keyword"
                            }
                        }
                    },
                    {
                        label: "上传",
                        component: "upload",
                        options: {
                            items:[
                                {
                                    label: "图像1",
                                    name: "img1",
                                    value: ""
                                },
                                {
                                    label: "图像2",
                                    name: "img2",
                                    value: ""
                                }
                            ]
                        }
                    },
                    {
                        label: "富文本",
                        name: "text",
                        value: "",
                        component: "editor",
                    },
                ]
            }
 
            const form = {
                name: "Activity name",
                checkbox: {
                    option1: true
                },
                checkboxGroup: ["option1"],
                select: ["1"],
                select2: "1",
                cascader: ['resource','docs'],
                tableselect: {
                    user: "何敏",
                    id: "360000197302144442"
                },
                text: "<p><span style=\"font-size: 28px;\">Editor</span></p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 47.4544%; background-color: #ecf0f1;\">Name</td>\n<td style=\"width: 47.4544%;\">SCUI</td>\n</tr>\n<tr>\n<td style=\"width: 47.4544%; background-color: #ecf0f1;\">Describe</td>\n<td style=\"width: 47.4544%;\">Super color UI Admin</td>\n</tr>\n</tbody>\n</table>"
            }
 
            setTimeout(()=>{
                this.config = config
            },500)
 
            this.loading = true
            setTimeout(()=>{
                this.form = form
                this.loading = false
            },100)
        },
        methods: {
            setName(){
                this.form.name = "New Title"
            },
            setConfig(){
                this.config.formItems[2].span = this.config.formItems[2].span==24?12:24
                this.config.formItems[3].span = this.config.formItems[3].span==24?12:24
            },
            save(){
                this.$refs.formref.validate((valid, obj) => {
                    if (valid) {
                        console.log(this.form)
                        alert('submit!')
                    }else{
                        this.$refs.formref.scrollToField(Object.keys(obj)[0])
                        return false
                    }
                })
            },
            reset(){
                this.$refs.formref.resetFields()
            }
        }
    }
</script>
 
<style>
</style>