el-table的行向上移动向下移动,删除选定行

复制代码
<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>
相关推荐
程序猿大波42 分钟前
基于java,SpringBoot和Vue餐饮公司食堂管理系统设计
java·vue.js·spring boot
swipe1 小时前
JavaScript 对象操作进阶:从属性描述符到对象创建模式
前端·javascript·面试
前端小趴菜052 小时前
vue3项目优化方案
前端·javascript·vue.js
Irene19913 小时前
对比总结:Vue3中的 watch 和 Pinia中的 $subscribe
vue.js·pinia·watch·subscribe
Luna-player3 小时前
gitee上的vue项目,刚刚创建了一个分支,怎么在本地上拉取分支项目
前端·vue.js·gitee
徐小夕3 小时前
借助AI,1周,0后端成本,我开源了一款Office预览SDK
前端·vue.js·github
还是大剑师兰特3 小时前
Vue3 按钮切换示例(启动 / 关闭互斥显示)
开发语言·javascript·vue.js
小金鱼Y3 小时前
从进程线程到 async/await,一文吃透前端异步核心原理
前端·javascript
SuperEugene3 小时前
前端代码注释规范:Vue 实战避坑,让 3 年后的自己还能看懂代码|项目规范篇
前端·javascript·vue.js