表单设计器form-create使用

# form-create vue3版本

官方网址:http://www.form-create.com/v3/

设计器演示地址:http://www.form-create.com/designer/?fr=home

表单源码gitee地址:https://gitee.com/xaboy/form-create/tree/next/

设计器源码gitee地址:https://gitee.com/xaboy/form-create-designer/tree/next/

# form-create v3使用

npm install @form-create/element-ui@next

import formCreate from '@form-create/element-ui'

app.use(formCreate)

<form-create :rule="rule" v-model:api="fApi" :option="options" v-model="value"/>

export default {
    data(){
        return {
            fApi:{},
            value:{field1:'111',field2:'222',time:'11:11:11'},
            options:{
                onSubmit:(formData)=>{
                    alert(JSON.stringify(formData))
                }
            },
            rule:[
                {type:'input', field:'field1',title:'field1',value:'aaa'},
                {type:'input', field:'field2',title:'field2',value:'sss'},
                {type:'timePicker', field:'time',title:'time',value:'12:12:12'},
                {
                    type:'ElButton',
                    title:'修改 field1',
                    native: false,
                    on:{
                        click: ()=>{
                            this.rule[0].value+='a'
                        }
                    },
                    children: ['点击'],
                }
            ]
        }
    }
}
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

# form-create-designer v3使用

npm install @form-create/designer@next

import formCreate from '@form-create/element-ui'

import FcDesigner from '@form-create/designer'

app.use(formCreate)

app.use(FcDesigner)

<fc-designer ref="designer"/>
1

# 源码分析

  • vue-codemirror 和 jsonlint-mod实现 form-create-designer的导入导出JSON
使用方法:
  1. 下载插件
  //vue-codemirror
  npm install vue-codemirror --save
  //JSON校验器插件
  npm install jsonlint-mod --save
1
2
3
4
  1. 在main.js引入vue-codemirror
import VueCodemirror from 'vue-codemirror'
Vue.use(VueCodemirror)
1
2
  1. 在使用form-create-designer的页面引入相关css和js
import jsonlint from 'jsonlint-mod';
import 'codemirror/addon/lint/lint'
import 'codemirror/addon/lint/json-lint'
import 'codemirror/lib/codemirror.css'
import 'codemirror/addon/lint/lint.css'
import 'codemirror/addon/edit/matchbrackets.js'
import 'codemirror/mode/javascript/javascript.js'
1
2
3
4
5
6
7
  1. HTML代码
<template>
  <div class="form-container">
    <el-row>
      <el-button icon="el-icon-download" type="primary" size="small" round @click="handleDownloadRule()">生成表单JSON</el-button>
      <el-button icon="el-icon-upload2" type="success" size="small" round @click="handleUploadRule()">导入表单JSON</el-button>
      <el-button icon="el-icon-download" type="primary" size="small" round @click="handleDownloadOption()">生成表单配置</el-button>
      <el-button icon="el-icon-upload2" type="success" size="small" round @click="handleUploadOption()">导入表单配置</el-button>
    </el-row>
    <!--form-create-designer-->
    <fc-designer ref="designer" />
    <!--代码预览-->
    <el-dialog :title="dialogTitle" :visible.sync="dialogState" :close-on-click-modal="false" append-to-body>
      <!--vue-codemirror-->
      <codemirror v-model="codemirrorContent" :options="codemirrorOptions" style="height: 90%;text-align: left;border: 1px solid #ccc;" />
      <el-row v-if="dialogMenu">
        <el-button @click="dialogState = false">取 消</el-button>
        <el-button type="primary" @click="handleImport()">导 入</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  1. JS关键代码
export default {
    beforeCreate() {
        // 开启JSON校验
        window.jsonlint = jsonlint
    },
    data() {
        return {
            dialogTitle: '', // 对话框标题
            dialogState: false, // 对话框状态
            dialogMenu: false, // 对话框菜单状态
            // codemirror配置
            codemirrorOptions: {
                mode: "application/json",
                theme: "default",
                gutters: ['CodeMirror-lint-markers'],
                tabSize: 2,
                lint: true,
                line: true,
                lineNumbers: true,
                matchBrackets: true,
                lineWrapping: true,
                styleActiveLine: true,
                readOnly: false
            },
            // codemirror内容
            codemirrorContent: null
        }
    },
    components: {
        codemirror
    },
    methods: {
        // 导出表单JSON
        handleDownloadRule(){
            this.dialogTitle = "表单规则"
            this.dialogState = true
            this.dialogMenu = false
 
            this.codemirrorOptions.readOnly = true
            this.codemirrorContent = JSON.stringify(this.$refs.designer.getRule(),
                                                    null, 2)
        },
        // 导出表单配置
        handleDownloadOption(){
            this.dialogTitle = "表单配置"
            this.dialogState = true
            this.dialogMenu = false
 
            this.codemirrorOptions.readOnly = true
            this.codemirrorContent = JSON.stringify(this.$refs.designer.getOption(),
                                                    null, 2)
        },
        // 导入表单JSON
        handleUploadRule(){
            this.dialogTitle = "导入表单规则"
            this.dialogState = true
            this.dialogMenu = true
 
            this.codemirrorOptions.readOnly = false
            this.codemirrorContent = JSON.stringify([], null, 2)
        },
        // 导入表单配置
        handleUploadOption(){
            this.dialogTitle = "导入表单配置"
            this.dialogState = true
            this.dialogMenu = true
 
            this.codemirrorOptions.readOnly = false
            this.codemirrorContent = JSON.stringify({}, null, 2)
        },
        // 配置导入
        handleImport(){
            try {
                let content = JSON.parse(this.codemirrorContent)
                if(this.dialogTitle == "导入表单规则"){
                    this.$refs.designer.setRule(content)
                }
                if(this.dialogTitle == "导入表单配置"){
                    this.$refs.designer.setOptions(content)
                }
                this.dialogState = false
            } catch(e) {
                alert('输入内容格式有误!')
            }
        }
    }
}
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
上次更新: 2025/02/10, 20:20:37
最近更新
01
Git问题集合
01-29
02
安装 Nginx 服务器
01-25
03
安装 Docker 容器
01-25
更多文章>
×
×