Vue + form_create 简单实现自定义表单且可存储为JSON格式进行读取

  1. demo背景:为用户发起活动时可自定义报名表单,使活动参与者报名时填写。由于表单需要在另一个页面显示并使用,所以要使表单可存储并渲染。

  2. 实现过程:百度并查阅了一系列关于自定义表单的内容后,在form-create中发现可以通过特定的JSON格式数据渲染表单,于是就写了一个简单的demo实现自定义的过程,将数据收集成特定的JSON格式来实现渲染。

先上效果图:
在这里插入图片描述
由于我的项目需要使移动端,所以简单做了一个移动端的demo来分享。
比较简陋,同时本人是新手,有写的不周到的地方欢迎指出

只是为了分享这个思路,随便画的界面!!!
只是为了分享这个思路,随便画的界面!!!
只是为了分享这个思路,随便画的界面!!!

实际效果图:
在这里插入图片描述

环境:

第一步

  1. vue-cli 2.X 关于安装就不赘述了
  2. form-create 安装可查看官方文档:http://www.form-create.com/v2/guide/start.html#%E5%BC%95%E5%85%A5-form-create-v2
    通过npm安装
 npm i @form-create/element-ui

第二步

然后在 main.js 中写入以下内容:

import formCreate from '@form-create/iview'
Vue.use(formCreate)

在相应页面写入:
方式1 <form-create v-model="fApi" :rule="rule" :option="option"></form-create>
方式2<div id="form-create"></div>
本demo采用的方式2,详细可查看文档用更详细的用法

第三步

data(){
	rule: [],//存储表单json数据
}
mounted() {
      //表单插入的节点
      const root = document.getElementById('form-create');
      //$f为表单api
      const $f = window.formCreate.create(
        //表单生成规则
        this.rule,//存储JSON表单数据的数组
        //组件参数配置
        {
          el:root,
          //显示表单重置按钮
          submitBtn: false, //控制是否显示提交按钮
          resetBtn: false, //控制是否显示重置按钮
        });
    },

rule中可先放入一些假数据

rule: [
	{"type":"input","field":"goods_name","title":"商品名称","value":"mi"},
    {"type":"inputNumber","field":"goods_price","title":"商品价格","value":12}
]

进行到这一步时 如果环境与代码无误 页面时会显示相应的两项表单元素。
同理当其他页面作为表单的使用页时,将以上代码写入 并将格式合理的json数据存入rule就可以了

页面完整代码:

<template>
  <div class="Form_box">
  //此处只实现了输入框的demo,其它依葫芦画瓢即可实现
    <div class="Form_topbar">
      <div class="Form_item" @click="details(1)">输入框</div>
      <div class="Form_item" @click="details(2)">单选</div>
      <div class="Form_item" @click="details(3)">多选</div>
      <div class="Form_item" @click="details(4)">文本框</div>
      <div class="Form_item" @click="details(5)">图片</div>
      <div class="Form_item" @click="details(6)">日期</div>
    </div>
    //这里是表单插入的地方,id要和js代码中的一致
    <div id="app3">
      <div class="form-create" id="form-create"></div>
    </div>
    //这里是表单项的配置弹窗,可根据需要改动
    <div class="Form_details" v-if="showDetails">
      <div class="Form_details_item">标题:
        <input placeholder="例:姓名" maxlength="10" v-model="title">
      </div>
      <div class="Form_details_item">类型:
        <el-select v-model="type" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </div>
      <div class="Form_details_item">占位内容:
        <input placeholder="例:请输入姓名" maxlength="10" v-model="placeholder">
      </div>
      <div class="Form_details_item">是否必填:
        <el-switch
          v-model="Required"
          active-color="#13ce66"
          inactive-color="#C0CCDA">
        </el-switch>
      </div>
      <div class="Form_details_item">最大长度:
        <el-input-number v-model="maxlength" :min="1" :max="50" label="描述文字"></el-input-number>
      </div>
      <div class="tapBar">
        <div class="cancle" @click="cancle()">取消</div>
        <div class="add" @click="add()">插入</div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "HelloWorld",
    data(){
      return{
        maxlength: 1,//设置输入框最大长度
        title:"",//存储表单元素标题
        type:'',//存储表单元素类型
        placeholder:'',//存储表单输入框占位符
        Required:false,//存储表单元素类型
        input:0,//用来改变表单元素id
        showDetails:false,//表单细节弹窗
        options: [{
          value: 'text',
          label: '字符'
        }, {
          value: 'number',
          label: '数字'
        }],//类型下拉框value
        rule: [],//存储表单json数据
      }
    },
    mounted() {
      //表单插入的节点
      const root = document.getElementById('form-create');
      //$f为表单api
      const $f = window.formCreate.create(
        //表单生成规则
        this.rule,
        //组件参数配置
        {
          el:root,
          //显示表单重置按钮
          submitBtn: false,
          resetBtn: false,
        });
    },
    methods:{
      //表单设置弹窗
      details(index){
        var that = this
        that.showDetails = true;
      },
      //将填写好的数据整合成json并添加至rule
      pushform(){
        var that = this
        let temp = {
          type:'input',
          title:that.title,
          field:"input"+that.input,
          value:"",
          props: {
            type:that.type,
            maxlength:that.maxlength,
            placeholder:that.placeholder,
          },
          validate:[
            {
              required: that.Required,
              message: '不得为空',
            },
          ],
        }
        that.rule.push(temp)
        that.input++
        return Promise.resolve()//为了解决异步
      },
      //添加表单元素并为其加入删除button
      add(){
        var that = this
        let  index = 0
        //这样写为了解决异步
        that.pushform().then(() => {
          let newli = document.createElement('button');
          newli.innerHTML="x";
          newli.style.width='20px'
          newli.style.height='20px'
          newli.style.borderRadius='50%'
          newli.style.position='absolute'
          newli.style.right='20px'
          newli.style.backgroundColor='#2cb3d0'
          newli.style.color='#fff'
          newli.onclick=function(e){
            that.$forceUpdate()
            that.del(e).then(()=>{
              that.sort()
            })
          }
          let tags = document.getElementsByTagName("div");
          that.$forceUpdate()
          for( let k in tags)
          {
            let tag = tags[k];
            if(tag.className == 'el-col el-col-24') {
              newli.value=index
              tag.appendChild(newli);
              index++;
            }
          }
        });
      },
      //删除所选项
      del(e){
        var that =this
        let id = e.target.value
        that.rule.splice(id,1)
        return Promise.resolve()
      },
      //对剩余元素重新排序(目的实现删除功能)
      sort(){
        let ind = 0
        let tags = tags || document.getElementsByTagName("div");
        for( let k in tags)
        {
          let tag = tags[k];
          if(tag.className == 'el-col el-col-24') {
            tag.childNodes[1].value=ind
            ind++;
          }
        }
      },
      cancle(){
          //省略
      }
    }
  }
</script>
<style>
  .el-col-24 {
    height: 100px;
    display: flex;
  }
</style>
<style lang="less" scoped>
  button{
    border: unset;
  }
  .Form_box{
    width:100%;
    height: 100%;
    position: relative;
    .form-create{
      position: relative;
      .delbtn{
        position: absolute;
        right: 10px;
        width: 50px;
      }
    }
    .Form_topbar{
      display: flex;
      width: 100%;
      white-space: nowrap;
      overflow-x:scroll;
      margin-bottom: 20px;
      .Form_item{
        background-color: #2cb3d0;
        color: #fff;
        border: 1px solid #000;
        flex-shrink: 0;
        width: 150px;
        margin-left: 10px;
        margin-top: 10px;
      }
    }
    .Form_details{
      text-align: left;
      align-items: center;
      width: 90%;
      height: 50%;
      background-color: #fff;
      bottom: 10px;
      position: absolute;
      border: 1px solid #000;
      left: 50%;
      transform: translate(-50%);
      .Form_details_item{
        margin-top: 20px;
        width: 500px;
        margin: 20px auto;
      }
      .tapBar{
        display: flex;
        position: absolute;
        bottom: 20px;
        color: #fff;
        width: 100%;
        text-align: center;
        justify-content: center;
        .cancle{
          background-color: gray;
          width: 200px;
          border-radius: 20px;
          margin-right: 50px;
        }
        .add{
          background-color: #13ce66;
          width: 200px;
          border-radius: 20px;
        }
      }
    }
  }
</style>

以上对form-create的简单使用。有很多不足的地方,欢迎大佬们指点和交流!!!!!

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
实现可视化可拖放的自定义表单可以使用Vue.js和一些第三方库来帮助我们完成。下面是一个简单实现步骤: 1. 安装并引入vue-draggable插件 可以使用npm安装vue-draggable插件:`npm install vuedraggable --save`,然后在Vue组件中引入该插件。 ```javascript import draggable from 'vuedraggable' export default { components: { draggable } } ``` 2. 创建表单设计器组件并添加表单元素 在Vue组件中,可以使用`draggable`组件来实现拖放功能。我们可以使用`v-for`指令渲染表单元素列表,并将元素添加到`draggable`组件中。 ```html <draggable v-model="formElements"> <div v-for="(element, index) in formElements" :key="index"> <input v-if="element.type === 'input'" :type="element.inputType" :placeholder="element.placeholder"> <select v-else-if="element.type === 'select'"> <option v-for="(option, optionIndex) in element.options" :key="optionIndex">{{ option }}</option> </select> </div> </draggable> ``` 3. 添加表单元素到表单设计器 我们可以添加按钮来添加新的表单元素。在Vue组件中,我们可以使用`@click`事件添加新的元素到表单元素列表。 ```html <button @click="addInput">Add Input</button> <button @click="addSelect">Add Select</button> ``` ```javascript export default { data() { return { formElements: [] } }, methods: { addInput() { this.formElements.push({ type: 'input', inputType: 'text', placeholder: 'Enter text' }) }, addSelect() { this.formElements.push({ type: 'select', options: ['Option 1', 'Option 2', 'Option 3'] }) } } } ``` 4. 渲染表单 最后,我们可以使用`v-for`指令再次渲染表单元素列表,并将表单元素添加到表单中。 ```html <form> <div v-for="(element, index) in formElements" :key="index"> <input v-if="element.type === 'input'" :type="element.inputType" :placeholder="element.placeholder"> <select v-else-if="element.type === 'select'"> <option v-for="(option, optionIndex) in element.options" :key="optionIndex">{{ option }}</option> </select> </div> <button type="submit">Submit</button> </form> ``` 完整代码如下: ```html <template> <div> <draggable v-model="formElements"> <div v-for="(element, index) in formElements" :key="index"> <input v-if="element.type === 'input'" :type="element.inputType" :placeholder="element.placeholder"> <select v-else-if="element.type === 'select'"> <option v-for="(option, optionIndex) in element.options" :key="optionIndex">{{ option }}</option> </select> </div> </draggable> <button @click="addInput">Add Input</button> <button @click="addSelect">Add Select</button> <form> <div v-for="(element, index) in formElements" :key="index"> <input v-if="element.type === 'input'" :type="element.inputType" :placeholder="element.placeholder"> <select v-else-if="element.type === 'select'"> <option v-for="(option, optionIndex) in element.options" :key="optionIndex">{{ option }}</option> </select> </div> <button type="submit">Submit</button> </form> </div> </template> <script> import draggable from 'vuedraggable' export default { components: { draggable }, data() { return { formElements: [] } }, methods: { addInput() { this.formElements.push({ type: 'input', inputType: 'text', placeholder: 'Enter text' }) }, addSelect() { this.formElements.push({ type: 'select', options: ['Option 1', 'Option 2', 'Option 3'] }) } } } </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值