使用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: \[\],

完成!!

相关推荐
bai27318 分钟前
path.join('C:\a', '\b') 给我 C:\a\b,path.resolve 给 C:\b:Windows 上 51 行路径实测
javascript·后端
kyriewen38 分钟前
我写的 12 个 AI Skill,三个月后 8 个在吃空饷
javascript·人工智能·程序员
北城笑笑2 小时前
Vue 112 ,下篇:前端 dist 还原成 Vue3 + Vite 项目( krpano 全景接入与页面适配实战,Vue2 → Vue3 )
前端·javascript·vue.js·3d·前端框架·vue·webgl
我叫黑大帅2 小时前
搜“张三”把备注也带出来了?不用加高级筛选,如何让一个搜索框实现「选项直达」!
前端·javascript·面试
KevinWang_2 小时前
iOS 开发计划(三) - 打印 JavaScript 调试日志
javascript
如夏倾城4 小时前
WAF (Web Application Firewall,Web应用防火墙)
前端·网络·安全·web安全·web app
图扑可视化5 小时前
基于图扑软件智慧制硅厂 Web SCADA 开发实践
前端·三维可视化·scada·hmi·组态软件
前端fighter5 小时前
2026年前端面试题总结(pdf版)
前端·pdf
谢小飞5 小时前
运维加了个参数,我们的网页挂掉了
前端·浏览器
CoderYanger6 小时前
Java EE 进阶:12.Spring AOP(第二弹)
java·前端·数据库·spring boot·spring·java-ee·mybatis