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

4KB原生html实现table下tr的上下次序自由拖动

AI代码,没有翻译和原文链接;经典代码珍藏备用。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Draggable Table Rows with Logs</title>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: center;
        }
        tr.dragging {
            background-color: #ffa;
            opacity: 0.7;
        }
        tr.drop-target {
            border-top: 3px solid #007bff;
        }
        #log {
            margin-top: 20px;
            font-family: monospace;
        }
    </style>
</head>
<body>
    <h2>Draggable Table Rows with Logs</h2>
    <table>
        <thead>
            <tr>
                <th>Item</th>
            </tr>
        </thead>
        <tbody id="tableBody">
            <tr draggable="true"><td>Row 1</td></tr>
            <tr draggable="true"><td>Row 2</td></tr>
            <tr draggable="true"><td>Row 3</td></tr>
            <tr draggable="true"><td>Row 4</td></tr>
            <tr draggable="true"><td>Row 5</td></tr>
        </tbody>
    </table>

    <div id="log">
        <h3>Action Log</h3>
    </div>

    <script>
        const tableBody = document.getElementById('tableBody');
        let draggedRow = null;
        let dropTargetRow = null;

        function logAction(fromIndex, toIndex) {
            const logDiv = document.getElementById('log');
            const logMessage = `${fromIndex + 1} --> ${toIndex + 1}`;
            const logEntry = document.createElement('div');
            logEntry.textContent = logMessage;
            logDiv.appendChild(logEntry);
        }

        tableBody.addEventListener('dragstart', (event) => {
            draggedRow = event.target;
            draggedRow.classList.add('dragging');
        });

        tableBody.addEventListener('dragover', (event) => {
            event.preventDefault();
            const target = event.target.closest('tr');
            if (target && target !== draggedRow) {
                dropTargetRow && dropTargetRow.classList.remove('drop-target');
                dropTargetRow = target;
                dropTargetRow.classList.add('drop-target');
            }
        });

        tableBody.addEventListener('dragleave', (event) => {
            const target = event.target.closest('tr');
            if (target && target === dropTargetRow) {
                dropTargetRow.classList.remove('drop-target');
                dropTargetRow = null;
            }
        });

        tableBody.addEventListener('drop', (event) => {
            event.preventDefault();
            const target = event.target.closest('tr');
            if (dropTargetRow && dropTargetRow !== draggedRow) {
                const fromIndex = Array.from(tableBody.children).indexOf(draggedRow);
                const toIndex = Array.from(tableBody.children).indexOf(dropTargetRow);

                // Correct the insert position based on the drag direction
                if (fromIndex < toIndex) {
                    tableBody.insertBefore(draggedRow, dropTargetRow.nextSibling);
                    logAction(fromIndex, toIndex);
                } else {
                    tableBody.insertBefore(draggedRow, dropTargetRow);
                    logAction(fromIndex, toIndex);
                }
            }
            resetDragState();
        });

        tableBody.addEventListener('dragend', resetDragState);

        function resetDragState() {
            draggedRow && draggedRow.classList.remove('dragging');
            dropTargetRow && dropTargetRow.classList.remove('drop-target');
            draggedRow = null;
            dropTargetRow = null;
        }
    </script>
</body>
</html>


http://www.kler.cn/a/369768.html

相关文章:

  • 图漾相机——C++语言属性设置
  • 剑指 Offer II 008. 和大于等于 target 的最短子数组
  • pytorch逻辑回归实现垃圾邮件检测
  • 文献阅读 250128-Tropical forests are approaching critical temperature thresholds
  • Versal - 基础3(AXI NoC 专题+仿真+QoS)
  • Windows安装Miniconda和PySide6以及配置PyCharm
  • 【AI绘画】Midjourney进阶:对角线构图详解
  • Python 爬虫的寻宝大冒险:如何捕获 API 数据的宝藏
  • springboot092安康旅游网站的设计与实现(论文+源码)_kaic
  • 基 础 入 门
  • 【大数据知识】HBase入门知识
  • 一文解决单调栈的应用
  • 【无标题】 text = text.encode(“utf-8“)
  • 下载数据集用于图像分类并自动分为训练集和测试集方法
  • 解决RabbitMQ脑裂问题
  • (蓝桥杯C/C++)—— 编程基础
  • PyTorch 中常用的函数方法
  • 代码随想录:513. 找树左下角的值
  • 大数据新视界 -- 大数据大厂之大数据重塑影视娱乐产业的未来(4 - 1)
  • 项目组件:(Json\Muduo)
  • Linux系统操作篇 one -文件指令及文件知识铺垫
  • 计算机网络-MSTP的基础概念
  • 衡石分析平台系统分析人员手册-导入图表库图表
  • 数据库课程 第一周
  • 熵与信息论
  • ip命令设置固定IP(暂时设置,重启失效)