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>
相关推荐
JinSo31 分钟前
我的2025年度总结:EasyEditor
前端·程序员
喝拿铁写前端5 小时前
前端开发者使用 AI 的能力层级——从表面使用到工程化能力的真正分水岭
前端·人工智能·程序员
wuhen_n5 小时前
LeetCode -- 15. 三数之和(中等)
前端·javascript·算法·leetcode
七月shi人6 小时前
AI浪潮下,前端路在何方
前端·人工智能·ai编程
非凡ghost6 小时前
MusicPlayer2(本地音乐播放器)
前端·windows·学习·软件需求
脾气有点小暴6 小时前
scroll-view分页加载
前端·javascript·uni-app
beckyye6 小时前
ant design vue Table根据数据合并单元格
前端·antd
布列瑟农的星空6 小时前
还在手动翻译国际化词条?AST解析+AI翻译实现一键替换
前端·后端·ai编程
土豆12506 小时前
Rust 错误处理完全指南:从入门到精通
前端·rust·编程语言