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>
相关推荐
绿草在线2 分钟前
pnpm创建vite
vue.js
KjPrime1 小时前
纯vue手写流程组件
前端·javascript·vue.js
化作繁星2 小时前
tailwindcss如何修改elementplus的内部样式
vue.js·elementplus·tailwind css
码农不惑3 小时前
前端开发:Vue以及Vue的路由
前端·javascript·vue.js
lina_mua3 小时前
JavaScript 中的性能优化:从基础到高级技巧
开发语言·javascript·性能优化
烛阴5 小时前
JavaScript instanceof:你真的懂它吗?
前端·javascript
shadouqi5 小时前
1.angular介绍
前端·javascript·angular.js
痴心阿文6 小时前
React如何导入md5,把密码password进行md5加密
前端·javascript·react.js
qq_13948428826 小时前
springboot444-基于Vue的网络小说交流平台(源码+数据库+纯前后端分离+部署讲解等)
java·vue.js·spring boot·mysql·spring·maven·intellij-idea