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>
相关推荐
谢尔登几秒前
Vue3 响应式系统——computed 和 watch
前端·架构
愚公移码2 分钟前
蓝凌EKP产品:主文档权限机制浅析
java·前端·数据库·蓝凌
摘星编程13 分钟前
React Native for OpenHarmony 实战:RefreshControl 下拉刷新组件
javascript·react native·react.js
欣然~2 小时前
法律案例 PDF 批量转 TXT 工具代码
linux·前端·python
一个小废渣2 小时前
Flutter Web端网络请求跨域错误解决方法
前端·flutter
鸣弦artha2 小时前
Flutter框架跨平台鸿蒙开发——Extension扩展方法
android·javascript·flutter
符文师2 小时前
css3 新特性
前端·css3
ct9783 小时前
WebGL开发
前端·gis·webgl
C_心欲无痕3 小时前
前端页面渲染方式:CSR、SSR、SSG
前端
果粒蹬i3 小时前
生成式 AI 质量控制:幻觉抑制与 RLHF 对齐技术详解
前端·人工智能·easyui