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>
相关推荐
子兮曰11 小时前
OpenClaw入门:从零开始搭建你的私有化AI助手
前端·架构·github
吴仰晖11 小时前
使用github copliot chat的源码学习之Chromium Compositor
前端
1024小神11 小时前
github发布pages的几种状态记录
前端
不像程序员的程序媛14 小时前
Nginx日志切分
服务器·前端·nginx
Daniel李华14 小时前
echarts使用案例
android·javascript·echarts
北原_春希14 小时前
如何在Vue3项目中引入并使用Echarts图表
前端·javascript·echarts
JY-HPS14 小时前
echarts天气折线图
javascript·vue.js·echarts
尽意啊14 小时前
echarts树图动态添加子节点
前端·javascript·echarts
吃面必吃蒜14 小时前
echarts 极坐标柱状图 如何定义柱子颜色
前端·javascript·echarts
O_oStayPositive14 小时前
Vue3使用ECharts
前端·javascript·echarts