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

本文介绍了如何使用Vue.js和ElementUI中的el-table组件实现动态在列表中添加和删除行的功能,包括使用模板插槽和JavaScript方法来管理表格数据。
摘要由CSDN通过智能技术生成

**

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

**
先看一下展示的效果:
在这里插入图片描述
好了上代码:
样式界面:

<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代码

 // 增加一行
        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: [],

完成!!

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Net蚂蚁代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值