使用vue动态在列表中添加或者删除某一行

**

使用vue动态在列表中添加或者删除某一行

**

先看一下展示的效果:

好了上代码:

样式界面:

csharp 复制代码
<template>
  <div class="container">
    <h4 style="margin-left: 20px;">线路停靠站站点</h4>
    <el-button
      type="primary"
      size="mini"
      icon="el-icon-circle-plus-outline"
      @click="addRow"
      style="margin-bottom: 20px;margin-left: 20px;"
      >新增一行</el-button
    >
    <el-table :data="tableData" border>
      <el-table-column
        label="序号"
        align="center"
        type="index"
        fixed
        sortable
        min-width="80"
      />

      <el-table-column label="站点名称" prop="zdmc" align="center" >
        <template slot-scope="scope">
          <div class="flex align-center">
            <el-input v-model="scope.row.zdmc" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="停靠时间" prop="zdsj" align="center" >
        <template slot-scope="scope">
          <div class="flex align-center">
            <!-- <el-input v-model="scope.row.colcumn2" /> -->
            <el-time-picker  v-model="scope.row.zdsj"  class="date-box" format="HH:mm"  value-format="HH:mm"></el-time-picker>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" prop="isEnable" align="center" >
        <template slot-scope="scope">
          <div class="flex align-center">
            <!-- <el-input v-model="scope.row.colcumn" /> -->
            <el-radio v-model="scope.row.isEnable" label="1">启用</el-radio>
            <el-radio v-model="scope.row.isEnable" label="-1">不启用</el-radio>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="success"
            icon="el-icon-plus"
            circle
            size="small"
            @click="addRowIndex(scope.$index)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-minus"
            circle
            size="small"
            @click="delRow(scope.$index)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="form_block" style="text-align: center;">
      <el-button @click="cancel" v-if="!disabled">取消</el-button>
      <el-button type="primary" @click="handleSubmit" v-if="!disabled">提交</el-button>
    </div>
  </div>
</template>

js代码

csharp 复制代码
 // 增加一行
        addRow() {
      const row = {
        colcumn1: "",
        colcumn2: "",
        isEnable:"1",
      };
      this.tableData.push(row);
    },
    // 删除指定行
    delRow(index) {
      this.tableData.splice(index, 1);
    },
    // 指定位置插入行
    addRowIndex(index) {
      const row = {
        colcumn1: "",
        colcumn2: "",
        isEnable:"1",
      };
      this.tableData.splice(index + 1, 0, row);
    },

记得在 return 的data中定义数据 tableData: \[\],

完成!!

相关推荐
@大迁世界1 小时前
Firefox 本地模式:localhost 再见了 ?
前端·架构
米米商聊3 小时前
同名附件为什么被 Map 覆盖?重复键与一对多索引
前端·javascript
echome8884 小时前
Python 异常处理:try、else、finally 的正确搭配
开发语言·前端·python·异常处理·编程技巧
时间的情敌5 小时前
OpenLayers 实战|Vue3 + OpenLayers 完整工程案例(加载天地图、要素、弹窗、绘制、WFS)
前端·openlayers
陈随易5 小时前
下载神器!Motrix 停更三年后重写回归
前端·后端·程序员
前端的日常5 小时前
一个人+AI做情侣食谱小程序,0 费用的服务器
前端·后端
计算机魔术师6 小时前
谷歌搜索广告踩刹车,AI广告成为救命稻草
前端
总要学点什么6 小时前
第81篇:数据可视化-PM2.5分布——让圆点的胖瘦替每座城市报出浓度
前端·信息可视化·echarts·cesium·空间分析
IT_陈寒6 小时前
Vue的v-for里用了个临时变量,页面直接挂了
前端·人工智能·后端
hunter_0766 小时前
一次 iOS 真机签名 409 的完整排查:证书、设备、描述文件为什么总是"对不上"
前端