vue element-ui 表格动态添加行编辑行删除行

这篇博客展示了如何在Vue项目中使用Element-UI组件库创建一个动态表格,包括添加、编辑和删除行的功能。通过在表格中使用`el-input`进行编辑,并通过`addList`、`deleteRow`方法实现增删操作,提交按钮用于查看当前表格数据。
摘要由CSDN通过智能技术生成

vue element-ui 表格动态添加行编辑行删除行

代码展示

<template>
  <div class="app-container home">
    <el-form ref="historyForm" :model="historyForm" size="small">
      <el-table
        :data="historyForm.tableData"
        style="width: 100%"
        class="list-table"
        size="mini"
        border>
        <el-table-column label="疾病名称" align="center" prop="diseaseName">
          <template slot-scope="scope">
            <el-input v-model="scope.row.diseaseName"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="用药情况">
          <el-table-column label="药物名称" align="center" prop="drugName">
            <template slot-scope="scope">
              <el-input v-model="scope.row.drugName"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="剂量" align="center" prop="dose">
            <template slot-scope="scope">
              <el-input v-model="scope.row.dose"></el-input>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="addParamsSetting">新增</el-button>
            <el-button type="primary" size="small" @click.native.prevent="deleteRow(scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button @click="submitFun">提交</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Index",
  data() {
    return {
      historyForm: {
        tableData: [{
          diseaseName: '',
          drugName: '',
          dose: '',
        }]
      },
    };
  },
  created() {

  },
  methods: {
    addList () {
      this.historyForm.tableData.push({
        diseaseName: '',
        drugName: '',
        dose: '',
      })
    },
    // 新增行
    addParamsSetting () {
      this.addList()
    },
    // 删除当前行
    deleteRow (index) {
      this.historyForm.tableData.splice(index, 1)
    },
    submitFun() {
      console.log(this.historyForm)
    },
  }
};
</script>

效果展示

在这里插入图片描述

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值