el-table动态添加行,删除行

el-table动态添加行,删除行

html 复制代码
<template>
  <div class="table-demo">
    <el-button type="primary" @click="addRow" style="margin-bottom: 10px;">
      添加行
    </el-button>
    
    <el-table
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column prop="name" label="姓名" width="180">
        <template #default="scope">
          <el-input v-model="scope.row.name" placeholder="请输入姓名"></el-input>
        </template>
      </el-table-column>
      
      <el-table-column prop="age" label="年龄" width="180">
        <template #default="scope">
          <el-input v-model.number="scope.row.age" type="number" placeholder="请输入年龄"></el-input>
        </template>
      </el-table-column>
      
      <el-table-column prop="address" label="地址">
        <template #default="scope">
          <el-input v-model="scope.row.address" placeholder="请输入地址"></el-input>
        </template>
      </el-table-column>
      
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button 
            type="danger" 
            size="small" 
            @click="deleteRow(scope.$index)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElTable, ElTableColumn, ElButton, ElInput } from 'element-plus'

// 表格数据源
const tableData = ref([
  { name: '张三', age: 20, address: '北京市' },
  { name: '李四', age: 25, address: '上海市' }
])

// 添加行
const addRow = () => {
  tableData.value.push({})
}

// 删除行
const deleteRow = (index) => {
  tableData.value.splice(index, 1)
}
</script>

<style scoped>
.table-demo {
  width: 800px;
  margin: 20px auto;
  padding: 20px;
  border: 1px solid #eee;
  border-radius: 4px;
}
</style>
相关推荐
我是永恒17 小时前
上架一个跨境工具导航网站
前端
电子羊17 小时前
Spec 编程工作流文档
前端
GISer_Jing17 小时前
从CLI到GUI桌面应用——前端工程化进阶之路
前端·人工智能·aigc·交互
还是大剑师兰特17 小时前
Vue3 报错:computed value is readonly 解决方案
前端·vue.js
leaves falling17 小时前
有效的字母异位词
java·服务器·前端
We་ct18 小时前
LeetCode 35. 搜索插入位置:二分查找的经典应用
前端·算法·leetcode·typescript·个人开发
左耳咚18 小时前
Claude Code 中的 SubAgent
前端·ai编程·claude
FPGA小迷弟18 小时前
高频时钟设计:FPGA 多时钟域同步与时序收敛实战方案
前端·学习·fpga开发·verilog·fpga
IT古董18 小时前
【前端】企业级前端调试体系设计(含日志埋点 + Eruda 动态注入 + Sentry)
前端·sentry
gis开发18 小时前
cesium 中添加鹰眼效果
前端·javascript