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>

四、看图片(效果)

相关推荐
我爱加班、、5 分钟前
Chrome安装最新vue-devtool插件
javascript·vue.js·chrome·vue-devtool
weixin_4738947717 分钟前
前端服务器部署分类总结
前端·网络·性能优化
LuckyLay35 分钟前
React百日学习计划-Grok3
前端·学习·react.js
澄江静如练_39 分钟前
小程序 存存上下滑动的页面
前端·javascript·vue.js
源码方舟1 小时前
基于SpringBoot+Vue的房屋租赁管理系统源码包(完整版)开发实战
vue.js·spring boot·后端
互联网搬砖老肖1 小时前
Web 架构之会话保持深度解析
前端·架构
m0_513962531 小时前
vue-ganttastic甘特图label标签横向滚动固定方法
javascript·vue.js·甘特图
菜鸟una1 小时前
【taro3 + vue3 + webpack4】在微信小程序中的请求封装及使用
前端·vue.js·微信小程序·小程序·typescript·taro
Java&Develop1 小时前
怎么查看当前vue项目,要求的node.js版本
vue.js
hao_04131 小时前
elpis-core: 基于 Koa 实现 web 服务引擎架构设计解析
前端