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

【VUE】ant design vue实现表格table上下拖拽排序

适合版本:ant design vue 1.7.8 

实现效果:

代码:

<template>
    <div class="table-container">
        <a-table
            :columns="columns"
            :dataSource="tableData"
            :rowKey="record => record.id"
            :rowClassName="getRowClassName"
        >
            <template v-slot:action="text,record">
                <a-button type="primary" icon="drag" @mousedown="startDrag(record, $event)"></a-button>
            </template>
        </a-table>
    </div>
</template>

<script>
import { Table, Button } from 'ant-design-vue';

export default {
    components: {
        'a-table': Table,
        'a-button': Button
    },
    data() {
        return {
            tableData: [
                { id: 1, name: 'Item 1' },
                { id: 2, name: 'Item 2' },
                { id: 3, name: 'Item 3' }
            ],
            columns: [
                { title: 'ID', dataIndex: 'id', key: 'id' },
                { title: 'Name', dataIndex: 'name', key: 'name' },
                {
                    title: 'Action',
                    key: 'action',
                    scopedSlots: { customRender: 'action' }
                }
            ],
            draggingItem: null,
            draggingIndex: -1
        };
    },
    methods: {
        getRowClassName(record, index) {
            return index === this.draggingIndex ? 'dragging-row' : '';
        },
        startDrag(record, event) {
            this.draggingItem = record;
            this.draggingIndex = this.tableData.findIndex(item => item.id === record.id);
            document.addEventListener('mousemove', this.onDrag);
            document.addEventListener('mouseup', this.stopDrag);
        },
        onDrag(event) {
            const mouseY = event.clientY;
            const tableRows = document.querySelectorAll('.ant-table-row');
            let targetIndex = -1;

            tableRows.forEach((row, index) => {
                const rect = row.getBoundingClientRect();
                if (mouseY >= rect.top && mouseY <= rect.bottom) {
                    targetIndex = index;
                }
            });

            if (targetIndex !== -1 && targetIndex !== this.draggingIndex) {
                const [draggedItem] = this.tableData.splice(this.draggingIndex, 1);
                this.tableData.splice(targetIndex, 0, draggedItem);
                this.draggingIndex = targetIndex;
            }
        },
        stopDrag() {
            document.removeEventListener('mousemove', this.onDrag);
            document.removeEventListener('mouseup', this.stopDrag);
            this.draggingItem = null;
            this.draggingIndex = -1;
        }
    }
};
</script>

<style>
.dragging-row {
    background-color: #f0f0f0;
    cursor: grabbing;
}
</style>

http://www.dtcms.com/a/73772.html

相关文章:

  • Node.js框架Express、Koa、Koa2、Egg 和 NestJS 的对比分析
  • 4张图,9个方法,搞定 “信贷风控策略调优”
  • 在vue项目中,使用Patch请求,实现根据id修改某张发票的日结状态
  • 【前端】入门基础(二)格式化标签、img标签
  • 【免费】2004-2017年各地级市进出口总额数据
  • 单片机外设快速入门篇(六)——中断篇
  • kali通过永恒之蓝漏洞攻击目标机
  • 解决diffusers加载stablediffusion模型,输入prompt总是报错token数超出clip最大长度限制
  • CSS @media print 使用详解
  • 如何使用Spring AI提示词模板PromptTemplate?
  • 华为OD机试 - 最长的完全交替连续方波信号(Java 2023 B卷 200分)
  • 如何在 SwiftUI 视图中使用分页(Paging)机制显示 SwiftData 数据(二)
  • 【微服务日志收集①】使用FileBeat+Logstash+ES搭建ELK日志系统
  • 利用hexo+github部署属于自己的个人博客网站(2025年3月所写)
  • Leetcode——151.反转字符串中的单词
  • 2025 年3月26日中国人民大学与加拿大女王大学金融硕士项目面试通知
  • Linux应用软件编程(多任务:进程间通信)
  • 单元测试、注解
  • c++入门基本知识掌握
  • SpringBoot集成Netty的方案以及Demo示列
  • SAP HANA on AWS Amazon Web Services
  • Git 面试问题,解决冲突
  • k8s常用操作 (一) ---根据上一篇文章用到写的 不全
  • __call__
  • fastpdf应用程序错误0xc0000142
  • 反汇编学习
  • 基于YOLOv8与SKU110K数据集实现超市货架物品目标检测与计算
  • Matlab 单球机器人动力学与LQR控制研究
  • P11229 [CSP-J 2024] 小木棍
  • 跳转到视图文件夹