Element-Ui el-table 动态添加行

一、在项目需要使用

这个需求主要是在项目中需要用到

1.点击新增按钮,可以实现新增行。

2.在每个列里面可以进行输入。

3.可以删除新增的行,包括数据。

二、HTML代码

1.主要是循环每一个列,而且这些列都是动态,根据父组件传过来的,这样可以动态的决定要多少的数据。

2.:data="tableData" 这个才是决定行的代码,可以通过push的方法新增行或者减少行。

复制代码
<template>
  <div class="add-table">
    <el-button class="mb10" size="mini" type="primary" @click="addTableRow">新 增</el-button>
    <el-table :data="tableData" stripe highlight-current-row border ref="addTableRowRef">
      <el-table-column type="index" label="序号" align="center" width="50"></el-table-column>
      <el-table-column align="center" v-for="(item,index) in dataList" :prop="item.code" :label="item.name" :key="index">
        <template slot-scope="scope">
          <el-input v-model="scope.row[item.code]"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" icon="el-icon-delete"  @click="handleDeleteRow(scope.$index,tableData)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

三、JavaScript代码

复制代码
<script>
export default {
  name: 'AddTable',
  props: {
    rowTitle: {
      type: Array,
      default() {
        return []
      }
    },
    rowObj: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  watch: {
    rowTitle: {
      handler: function(newValue) {
        if(newValue.length > 0) {
          this.dataList = newValue
        }
      },
      immediate: true
    }
  },
  data() {
    return {
      tableData: [],
      dataList: []
    }
  },
  methods: {
  addTableRow() {
      var addObj = {};
      this.rowTitle.forEach(el => {
        addObj[el.code] = ''
      })
      this.tableData.push(addObj);

      // this.tableData.push(this.rowObj);
      setTimeout(() => {
        this.$refs.addTableRowRef.setCurrentRow(addObj);
      }, 10);
    },

    handleDeleteRow(index, rows) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          rows.splice(index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
  },
}
</script>

四、看图片(效果)

相关推荐
NiceCloud喜云40 分钟前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
wordbaby1 小时前
React Native + RNOH:跨页面数据回传的最佳实践与避坑指南
前端·react native
丷丩1 小时前
MapLibre GL JS第22课:查看本地GeoJSON
前端·javascript·map·mapbox·maplibre gl js
Front思2 小时前
AI前端工程师需要具备能力+
前端·人工智能·ai
ZC跨境爬虫4 小时前
跟着 MDN 学CSS day_29:(掌握文本与字体样式的核心艺术)
前端·css·ui·html·tensorflow
李子琪。5 小时前
网络空间安全深度实战:CSRF 漏洞原理剖析与基于 Token 的纵深防御体系构建(全栈实验报告)
前端·安全·csrf
冰暮流星5 小时前
javascript之history对象介绍
前端·笔记
IT_陈寒5 小时前
Vite热更新失灵?你可能漏了这个配置
前端·人工智能·后端
丷丩5 小时前
MapLibre GL JS第19课:实时更新要素
前端·javascript·gis·map·mapbox·maplibre gl js
Mr.Daozhi5 小时前
RAG 进阶实战:跑通 Demo 后我连续翻了 6 次车,逐一修复才真正可用(含 Gradio Web 版)
前端·数据库·langchain·大模型·gradio·rag·科研工具