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>
相关推荐
用户11489669441056 分钟前
VUE3响应式原理——从零解析
vue.js
用户83040713057018 分钟前
SPA 首屏加载速度慢怎么解决?
vue.js·webpack
一枚前端小姐姐9 分钟前
低代码平台表单设计系统技术分析(实战三)
前端·vue.js·低代码
SuperEugene38 分钟前
从 Vue2 到 Vue3:语法差异与迁移时最容易懵的点
前端·vue.js·面试
Leon1 小时前
新手引导 intro.js 的使用
前端·javascript·vue.js
Forever7_2 小时前
仅用一个技巧,让 JavaScript 性能提速 500%!
前端·vue.js
牛奶2 小时前
JS随笔:浏览器 API(DOM 与 BOM)
前端·javascript·面试
牛奶2 小时前
JS随笔:异步编程与事件循环
前端·javascript·面试
牛奶2 小时前
JS随笔:数据结构与集合
前端·javascript·面试
小陆猿3 小时前
股票实时行情Echarts动态图表
前端·javascript