当前位置: 首页 > news >正文

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>

相关文章:

  • 网络编程(客户端间通信)
  • numpy学习笔记5:arr.T 是数组的转置属性详细解释
  • AI学习——卷积神经网络(CNN)入门
  • 计算机网络-网络接入技术
  • 【图像处理基石】什么是HDR图片?
  • Git 分支使用规范全解(多人协作开发适用)
  • 火星探测发展概述2025.3.20
  • JAVA序列化与反序列化URLDNS链CC1链
  • 【初学者】谈谈ChatGPT使用的算法?
  • IMX8MP Android 10系统编译SDK
  • LeetCode 30 —— 30.串联所有单词的子串
  • MobaXterm(远程终极工具箱) v25.1 Build 5288 汉化绿色版
  • Rust语言介绍和猜数字游戏的实现
  • Flink集群部署
  • 集装箱箱号OCR识别技术,在铁路物流场站集装箱装卸机械数字化系统中的应用
  • 高级java每日一道面试题-2025年3月05日-微服务篇[Eureka篇]-Eureka在微服务架构中的角色?
  • nginx keepalive设置失效k6显示i/o timeout解决方案
  • Redis项目:秒杀业务(优化)
  • 知识蒸馏:让大模型“瘦身”的魔法
  • LiteratureReading:[2016] Enriching Word Vectors with Subword Information
  • 习近平在上海考察
  • 4月人文社科联合书单|天文学家的椅子
  • 西湖大学独家回应本科招生走出浙江:经过三年试点,条件成熟
  • 《奇袭白虎团》原型人物之一赵顺合辞世,享年95岁
  • 人社部:将会同更多部门分行业、分领域制定专项培训计划
  • 财政部下达农业生产防灾救灾资金3.76亿元,支持黄淮海等地抗旱保春播