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>
相关推荐
一个游离的指针14 小时前
Vue2:vue-router
前端·javascript·vue.js
用户45141942932216 小时前
2026 Vue 生态分析:底层重写了一遍,升级却几乎零成本
vue.js
凌晨三点说晚安17 小时前
理解 JavaScript 对象、原型与包装类
javascript
凌晨三点说晚安17 小时前
理解 JavaScript 作用域链、return 与闭包
javascript
tryCbest17 小时前
Vue系列——第 20 篇:Vue Router 4——路由管理完全指南(上)
前端·javascript·vue.js
feixing_fx17 小时前
原始类型与引用类型:赋值时到底复制了什么
开发语言·前端·javascript·前端框架
Funlang18 小时前
在 Windows 95 上写前端
前端·javascript·windows
用户8417948145618 小时前
vue 表格组件 vxe-table 实现 Excel 双击填充柄向下复制填充功能
vue.js
AI前端工程狮18 小时前
我给 MCP 长任务加进度条,五步只显示了一步
javascript·人工智能·程序员
彩虹暴龙兽21 小时前
JavaScript 作用域、闭包与对象
javascript