form-create-designer很完美,但是后台需要表单生成后的JSON和配置的JSON数据,form-create-designer本身提供的API,结合vue-codemirror 和 jsonlint-mod就能实现预览、导入导出了,效果如下:
使用方法:
1、下载插件
2 | npm install vue-codemirror --save |
4 | npm install jsonlint-mod --save |
2、在main.js引入vue-codemirror
1 | import VueCodemirror from 'vue-codemirror' |
3、在使用form-create-designer的页面引入相关css和js
1 | import jsonlint from 'jsonlint-mod' ; |
2 | import 'codemirror/addon/lint/lint' |
3 | import 'codemirror/addon/lint/json-lint' |
4 | import 'codemirror/lib/codemirror.css' |
5 | import 'codemirror/addon/lint/lint.css' |
6 | import 'codemirror/addon/edit/matchbrackets.js' |
7 | import 'codemirror/mode/javascript/javascript.js' |
4、HTML代码
02 | < div class = "form-container" > |
04 | < el-button icon = "el-icon-download" type = "primary" size = "small" round @ click = "handleDownloadRule()" >生成表单JSON</ el-button > |
05 | < el-button icon = "el-icon-upload2" type = "success" size = "small" round @ click = "handleUploadRule()" >导入表单JSON</ el-button > |
06 | < el-button icon = "el-icon-download" type = "primary" size = "small" round @ click = "handleDownloadOption()" >生成表单配置</ el-button > |
07 | < el-button icon = "el-icon-upload2" type = "success" size = "small" round @ click = "handleUploadOption()" >导入表单配置</ el-button > |
11 | < fc-designer ref = "designer" /> |
14 | < el-dialog :title = "dialogTitle" :visible.sync = "dialogState" :close-on-click-modal = "false" append-to-body> |
16 | < codemirror v-model = "codemirrorContent" :options = "codemirrorOptions" style = "height: 90%;text-align: left;border: 1px solid #ccc;" /> |
17 | < el-row v-if = "dialogMenu" > |
18 | < el-button @ click = "dialogState = false" >取 消</ el-button > |
19 | < el-button type = "primary" @ click = "handleImport()" >导 入</ el-button > |
5、js关键代码:
04 | window.jsonlint = jsonlint |
14 | mode: "application/json" , |
16 | gutters: [ 'CodeMirror-lint-markers' ], |
23 | styleActiveLine: true , |
27 | codemirrorContent: null |
36 | this .dialogTitle = "表单规则" |
37 | this .dialogState = true |
38 | this .dialogMenu = false |
40 | this .codemirrorOptions.readOnly = true |
41 | this .codemirrorContent = JSON.stringify( this .$refs.designer.getRule(), |
45 | handleDownloadOption(){ |
46 | this .dialogTitle = "表单配置" |
47 | this .dialogState = true |
48 | this .dialogMenu = false |
50 | this .codemirrorOptions.readOnly = true |
51 | this .codemirrorContent = JSON.stringify( this .$refs.designer.getOption(), |
56 | this .dialogTitle = "导入表单规则" |
57 | this .dialogState = true |
58 | this .dialogMenu = true |
60 | this .codemirrorOptions.readOnly = false |
61 | this .codemirrorContent = JSON.stringify([], null , 2) |
65 | this .dialogTitle = "导入表单配置" |
66 | this .dialogState = true |
67 | this .dialogMenu = true |
69 | this .codemirrorOptions.readOnly = false |
70 | this .codemirrorContent = JSON.stringify({}, null , 2) |
75 | let content = JSON.parse( this .codemirrorContent) |
76 | if ( this .dialogTitle == "导入表单规则" ){ |
77 | this .$refs.designer.setRule(content) |
79 | if ( this .dialogTitle == "导入表单配置" ){ |
80 | this .$refs.designer.setOptions(content) |
82 | this .dialogState = false |