当前位置:   article > 正文

Vue 表单生成器form-create的基本使用

form-create

现在有个项目需求,表单通过请求后端接口返回的数据,动态生成对应的DOM(表单类型),以及表单的初始数据渲染,和修改表单后的数据提交。根据以上项目需求,所以这次就选择用form-create(表单生成器)来做。

form-create

form-create是一个具有动态渲染、数据收集、校验和提交功能的表单生成器,支持生成vue组件。

官方参考文档: http://www.form-create.com

CRMEB系统:https://v5.crmeb.net/admin/login

账号:demo 密码:crmeb.com

github源代码:https://gitee.com/ZhongBangKeJi/CRMEB#https://gitee.com/ZhongBangKeJi/crmeb_java

根据自己项目UI组件库来选下载方式。

我们项目用到的是ElementUi组件库,所以安装:

npm i @form-create/element-ui

如其他组件库,请参考下图安装:

在main.js文件中配置elementui和form-create

实例(代码在下方):

完整代码如下”

  1. <template>
  2.   <div>
  3.     <el-button type="primary" @click="openFormDialog">新增</el-button>
  4.     <el-button type="primary" @click="editFormDialog">修改</el-button>
  5.     <!-- 表单弹窗 -->
  6.     <el-dialog :visible.sync="dialogVisible" title="动态表单" width="800px">
  7.       <form-create :option="option" v-model="fApi" :rule="rules" ></form-create>
  8.       <!-- <el-button @click="onSubmit">提交</el-button> -->
  9.     </el-dialog>
  10.   </div>
  11. </template>
  12.   <script>
  13. import {addComplaint} from "@/api/guestAppeal/guest";
  14. export default {
  15.   data() {
  16.     return {
  17.       fApi:{},
  18.       rules: [
  19.         {
  20.           type: 'input',
  21.           field: 'userName',
  22.           title: '用户名称',
  23.           value: '',
  24.           props: {
  25.             placeholder: '请输入用户名称',
  26.             clearable: true
  27.           },
  28.           validate: [
  29.             {
  30.               trigger: 'blur',
  31.               required: true,//是否必填
  32.               message: '用户名称不能为空'
  33.             }
  34.           ]
  35.         },
  36.         {
  37.           type: 'input',
  38.           field: 'summary',
  39.           title: '个人简介',
  40.           value: '',
  41.           props: {
  42.             type:'textarea',
  43.             placeholder: '请输入个人简介',
  44.             rows:3
  45.           },
  46.           validate: [
  47.             {
  48.               trigger: 'blur',
  49.               required: true,//是否必填
  50.               message: '用户名称不能为空'
  51.             }
  52.           ]
  53.         },
  54.         {
  55.           type: 'radio',
  56.           field: 'sex',
  57.           title: '性别',
  58.           value: 0,
  59.           options: [
  60.             {
  61.               label: '先生', value: 1,
  62.             },
  63.             {
  64.               label: '女士', value: 2,
  65. },
  66. ]
  67. },
  68. {
  69. type: 'checkbox',
  70. field: 'hobby',
  71. title: '爱好',
  72. value: [1, 3],
  73.           options: [
  74. {
  75. label: '吃喝',
  76. value: 1,
  77. },
  78. {
  79. label: '旅游',
  80. value: 2,
  81. },
  82. {
  83. label: '运动', value: 3,
  84. },
  85. {
  86. label: '学习', value: 4,
  87.             },
  88.           ]
  89.         },
  90.         {
  91.           type: 'select',
  92.           field: 'address',
  93.           title: '去哪',
  94.           value: 6,
  95.           col:{
  96.             md:{
  97.               span:10
  98.             }
  99.           },
  100.           options: [
  101.             {
  102.               label: '哈尔滨', value: 1,
  103.             },
  104.             {
  105.               label: '云南', value: 2,
  106.             },
  107.             {
  108.               label: '三亚', value: 3,
  109. },
  110. {label: '泰国', value: 4, },
  111.           ]
  112.         },
  113.         {
  114.           type:'DatePicker',
  115.           field:'dateTime',
  116.           title:'起止日期',
  117.           value:['',''],
  118.           props:{
  119.             type:'daterange',
  120.             format:'yyyy-MM-dd',
  121.             placeholder:'请选择起止日期'
  122.           }
  123.         },
  124.         {
  125.           type: 'upload',
  126.           field: 'imgFile',
  127.           title: '图片上传',
  128.           value: [
  129. 'https://img0.baidu.com/it/u=3232582821,3516640051&fm=253&fmt=auto&app=138&f=JPEG?w=625&h=500',
  130.             'https://img2.baidu.com/it/u=1814561676,2470063876&fm=253&fmt=auto&app=138&f=JPEG?w=750&h=500'
  131.           ],
  132.           col:{
  133.             md:{
  134.               span:18
  135.             }
  136.           },
  137.           props:{
  138.             type:'select',
  139.             uploadType:'image',
  140.             name:'userPhoto',
  141.             multiple:true,
  142.             accept:'image/*',
  143.             format:['jpg','png','jpeg'],
  144.             action:'http"//www.baidu.com',
  145.             onSuccess:function(res){
  146.               return ''
  147.             }
  148.           }
  149.         }
  150.       ],
  151.       option: {
  152.         resetBtn: true,
  153.         submitBtn: true,
  154.         form: {
  155.         },
  156.         onSubmit: formData => {
  157.           // alert(JSON.stringify(formData))
  158.           addComplaint(JSON.stringify(formData)).then(res=>{
  159. })
  160.         }
  161.       },
  162.       title: '',
  163.       dialogVisible: false, // 弹窗是否可见
  164.       formFields: {},       // 后端返回的字段定义
  165.       formData: {}          // 表单数据
  166.     };
  167.   },
  168.   methods: {
  169.     editFormDialog(row){
  170.         this.dialogVisible = true
  171.     },
  172.     openFormDialog() {
  173.       // 打开弹窗
  174.       this.dialogVisible = true;
  175.     },
  176.     // onSubmit(formData) {
  177.     //   // console.log(formData);
  178.     //   this.fApi.submit((formData,fApi)=>{
  179.     //     console.log(JSON.stringify(formData));
  180.     //     addComplaint(JSON.stringify(formData)).then(res=>{
  181.     //     })
  182.     //   })
  183.     // },
  184.   }
  185. };
  186.   </script>

效果图:

完成啦。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号