<template> <el-table :data="tableData" border style="width: 100%"> <!-- 其他列 --> <el-table-column label="ID"> <template slot-scope="scope">{{ scope.$index }}</template> </el-table-column> <el-table-column label="名称" prop="name"></el-table-column> <!-- 操作列:上下移动 --> <el-table-column label="操作" width="120"> <template slot-scope="scope"> <el-button size="mini" :disabled="scope.$index === 0" @click="moveRow(scope.$index, 'up')" >上移</el-button> <el-button size="mini" :disabled="scope.$index === tableData.length - 1" @click="moveRow(scope.$index, 'down')" >下移</el-button> <el-button @click="delRow(scope.$index)">删除一行</el-button> </template> </el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: "第 1 行",ID:"" }, { name: "第 2 行",ID:"" }, { name: "第 3 行",ID:""}, { name: "第 4 行",ID:""} ] }; }, methods: { // 行移动逻辑 moveRow(index, direction) { const newIndex = direction === 'up' ? index - 1 : index + 1; // 边界检查 if (newIndex < 0 || newIndex >= this.tableData.length) return; // 交换数组元素 const temp = this.tableData[index]; this.$set(this.tableData, index, this.tableData[newIndex]); this.$set(this.tableData, newIndex, temp); console.log(this.tableData) },
delRow(index){ if (this.tableData.length > 0) { //this.tableData.pop(); // 删除数组最后一个元素 this.tableData.splice(index,1) } },
} }; </script>
el-table的行向上移动向下移动,删除选定行
大霞上仙2025-03-22 15:54
相关推荐
gaolei_eit2 小时前
Vue3项目ES6转ES5,兼容低版本的硬件设备,React也一位搞嵌入式的 genius2 小时前
从 ES6 到 ESNext:JavaScript 现代语法全解析(含编译工具与实战)子兮曰7 小时前
OpenClaw架构揭秘:178k stars的个人AI助手如何用Gateway模式统一控制12+通讯频道Howrun7778 小时前
VSCode烦人的远程交互UI讲解百锦再8 小时前
Reactive编程入门:Project Reactor 深度指南百锦再8 小时前
React编程高级主题:测试代码颜酱10 小时前
图结构完全解析:从基础概念到遍历实现小迷糊的学习记录10 小时前
Vuex 与 pinia发现一只大呆瓜10 小时前
前端性能优化:图片懒加载的三种手写方案不爱吃糖的程序媛11 小时前
Flutter 与 OpenHarmony 通信:Flutter Channel 使用指南