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>
相关推荐
漫游的渔夫8 分钟前
别再直接 `json.loads` 了!AI 返回的 JSON 坑位指南
前端·人工智能
软件工程师文艺19 分钟前
从0到1:Claude Code如何用React构建CLI应用
前端·react.js·前端框架
M ? A29 分钟前
Vue 迁移 React 实战:VuReact 一键自动化转换方案
前端·vue.js·经验分享·react.js·开源·自动化·vureact
yuki_uix30 分钟前
重排、重绘与合成——浏览器渲染性能的底层逻辑
前端·javascript·面试
Burt40 分钟前
我的 2026 全栈选型:Vue3 + Elysia + Bun + AlovaJS
vue.js·全栈·bun
止观止1 小时前
拥抱 ESNext:从 TC39 提案到生产环境中的现代 JS
开发语言·javascript·ecmascript·esnext
沃尔威武1 小时前
调试黑科技:Chrome DevTools时间旅行调试实战
前端·科技·chrome devtools
小锋java12341 小时前
SpringBoot 4 + Spring Security 7 + Vue3 前后端分离项目设计最佳实践
java·vue.js·spring boot
一 乐1 小时前
校园线上招聘|基于springboot + vue校园线上招聘系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·校园线上招聘系统
yuki_uix1 小时前
虚拟 DOM 与 Diff 算法——React 性能优化的底层逻辑
前端·react.js·面试