vue点击按钮实现表单动态增加或删除数据的操作

第一版

1、简单样式展示

点击添加可追加行数
点击删除可删除行数,直至清空
在这里插入图片描述

  <el-form
          class="editTable"
          label-width="300px"
          :model="moveItem"
          :rules="rules"
          ref="moveItem"
        >
          <div class="rotate">
            <el-form-item
              v-for="(item, moveId) in moveLists"
              :key="moveId"
              label="轮转科室情况"
              prop="peopleIphone"
            >
              <el-input
                size="small"
                class="rotateYear"
                width="50"
                v-model="item.departmentStartName"
                auto-complete="off"
              ></el-input
              >科转至
              <el-input
                size="small"
                v-model="item.departmentEndName"
                auto-complete="off"
              ></el-input
              ></el-form-item>
          </div>
        </el-form>
        <div class="rotateButton">
          <div>
            <el-button
              size="mini"
              icon="el-icon-plus"
              type="success"
              @click="addModule()"
              >添加
            </el-button>
          </div>
          <div>
            <el-button
              size="mini"
              icon="el-icon-delete"
              type="danger"
              @click="deleteModule()"
              >删除
            </el-button>
          </div>
        </div>

2、获取数据

 data() {
    return {
    moveLists: [],
      moveItem: {
        moveId: "",
        moveStartOffice: "",
        moveEndOffice: "",
        moveJobNum: "",
        departmentStartName: "",
        departmentEndName: "",
      },
    }
   }
   //连接接口获取后台数据
    getMovedata() {
      this.loading = true;
      this.moveJobNum = JSON.parse(localStorage.getItem("baseJobNum"));
      MoveInfos(this.moveJobNum)
        .then((res) => {
          this.loading = false;
          if (res.data.status == 500) {
            this.$message({
              type: "info",
              message: res.data.message,
            });
          } else {
            this.moveLists = res.data;
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message.error("数据加载失败,请稍后再试!");
        });
    },

3、实现添加和删除的方法

 addModule() {
      //新增一行
      this.moveLists.push(this.moveItem);
    },
    deleteModule() {
      //删除最后一行
      this.moveLists.splice(this.moveLists.length - 1, 1);
    },

第二版可以单独删除其中一项

1、样式如下

在这里插入图片描述

2、获取数据

  <div class="addButton">
        <el-button
          type="primary"
          round
          size="mini"
          icon="el-icon-plus"
          @click="addModule()"
          >添加轮转科室信息</el-button
        >
      </div>
   <div class="rotateInfo">
        <el-form
          label-width="250px"
          :model="moveItem"
          :rules="rules"
          ref="moveItem"
        >
          <div class="rotate">
            <el-form-item
              v-for="(item, moveId) in moveLists"
              :key="moveId"
              prop="peopleIphone"
              ><el-date-picker
                value-format="yyyy-MM-dd"
                v-model="item.moveYear"
                type="year"
                placeholder="选择年"
              >
              </el-date-picker
              ><el-date-picker
                v-model="item.moveMonth"
                type="month"
                format="M月"
                value-format="M"
                placeholder="选择月"
              >
              </el-date-picker>
              月从
              <el-select
                v-model="item.moveStartOffice"
                filterable
                placeholder="请选择科室"
              >
                <el-option
                  v-for="item in departmentLists"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option> </el-select
              >科转至
              <el-select
                v-model="item.moveEndOffice"
                filterable
                placeholder="请选择科室"
              >
                <el-option
                  v-for="item in departmentLists"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option> </el-select
              ><i class="el-icon-delete" @click="deleteModule(moveId)"></i>
              <el-divider></el-divider>
            </el-form-item>
          </div>
        </el-form>
      </div>

3、实现添加和删除的方法

      moveItem: {
        moveYear: "",
        moveMonth: "",
        moveStartOffice: "",
        moveEndOffice: "",
        moveLists: [], //添加时将moveObj加到这里
      },
     moveObj: {
        moveYear: "",
        moveMonth: "",
        moveStartOffice: "",
        moveEndOffice: "",
      },
 addModule() {
       this.moveItem .moveLists.push(Object.assign({}, this.moveObj));
    },
     deleteModule(index) {
      this.moveItem.moveLists.splice(index, 1);
    },

最后保存就可以啦

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值