<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
相关推荐
alamhubb12 分钟前
反感pnpm的全链路污染?可以了解下这个对原项目零侵入,零修改完全兼容npm的monorepo工具2501_9481949835 分钟前
RN for OpenHarmony AnimeHub项目实战:正在热播页面开发2501_9445210036 分钟前
rn_for_openharmony商城项目app实战-语言设置实现AdleyTales41 分钟前
vscode识别不了@提示找不到路径解决前端九哥1 小时前
装个依赖把公司电脑干报废了?npm i 到底背着我干了啥?绝世唐门三哥2 小时前
工具函数-精准判断美东交易时间踢球的打工仔2 小时前
typescript-null和undefined前端小蜗2 小时前
对不起,我很贱:老板还没催,我自己就统计《GitLab年度代码报告》康一夏2 小时前
React面试题,useRef和普通变量的区别冴羽2 小时前
2025 年 HTML 年度调查报告公布!好多不知道!