vue模块动态增加和删除

本文介绍如何利用ElementUI的表单组件实现Vue应用中模块的动态添加和删除,通过调整组件以满足自定义需求,简化前端开发过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
一个常见的模块增加和删除功能
如果用js写相对复杂一点
elementUi 表单组件中有一个类似的功能 可以改成自己的需求
在这里插入图片描述

<el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
      <el-form-item prop="name" label="问卷名称">
        <el-input v-model="dynamicValidateForm.name"></el-input>
      </el-form-item>
      <el-form-item v-for="(domain, index) in dynamicValidateForm.domains" :label="index + 1 + ''" :key="domain.key"
        :prop="'domains.' + index + '.value'">
        <div v-if="domain.textValue == undefined">
          <el-input v-model="domain.value" placeholder="请输入想问的问题"></el-input>
          <el-checkbox v-model="domain.checked" style="margin-right:30px">是否必填</el-checkbox>
          <i class="el-icon-delete" style="font-size:18px;cursor: pointer;" @click.prevent="removeDomain(domain)"></i>
          <div style="margin-top:10px">
            <el-rate v-model="domain.rateValue"></el-rate>
          </div>
        </div>
        <div v-else>
          <el-input v-model="domain.value"></el-input>
          <el-checkbox v-model="domain.checked" style="margin-right:30px">是否必填</el-checkbox>
          <i class="el-icon-delete" style="font-size:18px;cursor: pointer;" @click.prevent="removeDomain(domain)"></i>
          <div style="margin-top:10px">
            <el-input type="textarea" :rows="5" placeholder="请输入内容" v-model="domain.textValue">
            </el-input>
          </div>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('dynamicValidateForm')">确定</el-button>
        <el-button @click="resetForm('dynamicValidateForm')">返回</el-button>
        <el-button @click="addDomain(1)">添加NPS题</el-button>
        <el-button @click="addDomain(2)">添加反馈填空题</el-button>
      </el-form-item>
    </el-form>
 data() {
    return {
      dynamicValidateForm: {
        name: '',
        domains: [{
          value: '',
          rateValue: 4,
          checked: false,
        }, {
          value: '',
          rateValue: 4,
          checked: false,
        }, {
          value: '非常感谢您的反馈!您还有什么意见和建议想对我们说吗?',
          textValue: '',
          checked: false,
        }],
      },
    }
  },
    removeDomain(item) {
      var index = this.dynamicValidateForm.domains.indexOf(item)
      if (index !== -1) {
        this.dynamicValidateForm.domains.splice(index, 1)
      }
    },
    addDomain(res) {
      if (res == 1) {
        this.dynamicValidateForm.domains.push({
          value: '',
          rateValue: 4,
          checked: false,
          key: Date.now()
        });
      } else {
        this.dynamicValidateForm.domains.push({
          value: '非常感谢您的反馈!您还有什么意见和建议想对我们说吗?',
          textValue: '',
          checked: false,
          key: Date.now()
        });
      }
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值