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

完成!!

相关推荐
喝拿铁写前端5 小时前
别再让 AI 直接写页面了:一种更稳的中后台开发方式
前端·人工智能
A向前奔跑6 小时前
前端实现实现视频播放的方案和面试问题
前端·音视频
十一.3666 小时前
131-133 定时器的应用
前端·javascript·html
xhxxx7 小时前
你的 AI 为什么总答非所问?缺的不是智商,是“记忆系统”
前端·langchain·llm
3824278278 小时前
python:输出JSON
前端·python·json
2503_928411568 小时前
12.22 wxml语法
开发语言·前端·javascript
光影少年8 小时前
Vue2 Diff和Vue 3 Diff实现及底层原理
前端·javascript·vue.js
2501_946224318 小时前
旅行记录应用统计分析 - Cordova & OpenHarmony 混合开发实战
javascript·harmonyos·harvester
傻啦嘿哟8 小时前
隧道代理“请求监控”实战:动态调整采集策略的完整指南
前端·javascript·vue.js