vue+element 动态添加表单以及提交表单

vue+element 动态添加表单以及提交表单

实现效果

在这里插入图片描述
在这里插入图片描述

html代码:

<el-form-item label="问题选项:" :label-width="formLabelWidth" prop="options" :inline="true">
    <el-button @click="addItem" size="mini">添加选项</el-button>
    <div v-for="(item, index) in addform.options" :key="index">
         <el-row :gutter="20">
             <el-col :span="6">
                 <div class="grid-content bg-purple">
                      <el-form-item
                         :prop="'options.' + index + '.item'"
                         :rules="{
                            required: true, message: '选项序号不能为空', trigger: 'blur'
                          }"
                      >
                       		<el-input placeholder="请输入选项序号,如A,B" autocomplete="off" size="small" v-model="item.item">			</el-input>
                      </el-form-item>
                  </div>
               </el-col>
               <el-col :span="6">
                   <div class="grid-content bg-purple" >
                        <el-form-item
                            :prop="'options.' + index + '.text'"
                            :rules="[
                              {required: true, message: '选项内容不能为空', trigger: 'blur'},
                            ]"
                         >
                         <el-input placeholder="请输入选项内容" autocomplete="off" size="small" v-model="item.text">		</el-input>
                        </el-form-item>
                     </div>
                 </el-col>
                 <el-col :span="6">
                     <div class="grid-content bg-purple">
                          <el-form-item>
                              <i class="el-icon-delete" @click="deleteItem(item, index)"></i>
                          </el-form-item>
                       </div>
                  </el-col>
              </el-row>
          </div>
      </el-form-item>

JS代码:

//新增选项
        addItem () {
          this.addform.options.push({
            item: this.item,
            text: this.text
          })
        },
        deleteItem (item, index) {
          this.addform.options.splice(index, 1)
        },

数据的提交(JSON字符串):

addform: {
            options: [{
              item: '',
              text: '',
            }]
          },
add: function(){
          this.title='';
          this.dialogformVisible = true;
          this.addform={
            options: [{
              item: '',
              text: '',
            }]
          };
          this.isadd=true;
          this.getProjectList();
        },
//新增
        cancelBtn1: function() {
          this.dialogformVisible = false;
          this.getProjectList();
        },
        sureBtn1: function(addform) {
          this.$refs[addform].validate((valid) => {
          console.log(valid);
          if (valid) {
          } else {
              return false;
            }
        });
        var addform = this.addform;
        let data = new FormData();
        data.append('options', JSON.stringify(addform.options));
        if (this.isadd == true){
          axios.post("",     //后端提供接口
          data,
            {
              headers: {
                'token': sessionStorage.getItem('token'),
                'Content-type': 'Content-Type: application/json'
              },
            }).then((response) => {
                if (response.data.code == 200) {
                  this.$message({ message: response.data.message, duration: 3000, type: 'success' });
                  this.dialogformkVisible = false;
                  this.getProjectList();
                } else {
                    this.$message({ message: response.data.message, duration: 2000, type: 'warning' });
                  }
              }).catch(function (error) {
                  console.log(error)
              });
          }
        },
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Element动态是一种可以动态设置项并渲染出来的组件。通常情况下,我们需要使用Vue Element的el-form和el-form-item构建,但是使用动态可以快速简便地创建。它不仅减少了代码的重复,还可以根据不同的需求灵活地增加和删除项。 首先,我们需要定义一个数组来存储项。数组中的每一个项都是一个对象,对象包含了项的各种属性,如类型,名称,placeholder等。在组件中,我们需要对这个数组进行遍历,并根据每一个对象的属性来动态地渲染出项。 其次,我们需要使用Vue Element的组件绑定来动态地设置项的属性,比如v-model可以用来绑定项的值,v-if可以用来控制项的显隐性,v-for可以用来循环遍历项。 在项的定义过程中,我们可以使用计算属性来对项的属性进行动态计算,从而实现项的复杂计算和逻辑处理。 最后,我们需要添加一个动态增加项的方法或组件。通常情况下,我们可以添加一个按钮或者下拉选择框,在用户进行相应的操作后,根据选择或者用户输入的数据来动态地增加项。 总的来说,Vue Element动态可以根据不同的需求快速地创建,并且可以灵活地增加和删除项。使用动态可以大大减少代码的重复,提高开发效率。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值