<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
相关推荐
muyun28002 小时前
History 模式 vs Hash 模式:Vue Router 技术决策因素详解什么什么什么?2 小时前
el-table高度自适应vue页面指令码上暴富6 小时前
axios请求的取消新中地GIS开发老师7 小时前
2025Mapbox零基础入门教程(14)定位功能tager7 小时前
Vue 3 组件开发中的"双脚本"困境han_8 小时前
前端遇到页面卡顿问题,如何排查和解决?changuncle10 小时前
Angular初学者入门第一课——搭建并改造项目(精品)海天胜景11 小时前
vue3 el-table 去除小数 并使用千分号漫天星梦11 小时前
Vue2项目搭建(Layout布局、全局样式、VueX、Vue Router、axios封装)星光不问赶路人12 小时前
TypeScript 模块扩展