码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Vue3+Element-Plus项目 el-table 拖拽排序实现,Vue3项目sortablejs的安装与使用


    概述

    技术栈: Vue3 + Ts + Vite + Element-Plus
    实现:实现 sortablejs 实现 el-tabel 的拖拽排序,可滚动排序,并实现拖拽排序的开启与关闭


    文章目录

      • 概述
      • 一、先看效果
      • 二、安装 sortablejs
      • 三、sortablejs 封装
          • 3.1 utilts 封装
          • 3.2 全局拖拽样式封装
      • 四、在 .vue 文件中使用
          • 4.1 引用
          • 4.2 e-table 中定义 row-key 和 高度
          • 4.3 定义排序方法
          • 4.4 给按钮绑定方法
      • 五、有任何问题,欢迎评论区留言

    一、先看效果

    请添加图片描述

    二、安装 sortablejs

    npm install sortablejs --save
    
    • 1

    三、sortablejs 封装

    3.1 utilts 封装

    在 /src/utils 下新建 sortable.ts 文件,并写入如下代码

    // 引入 sortable 
    import Sortable from 'sortablejs'
    
    // 定义一个变量来存储Sortable实例。后续销毁时会用到
    let sortableInstance: Sortable | null = null 
    
    /**
     * 拖拽函数
     * @param getList 获取列表数据
     * @param isSort  控制表单是否开启拖拽排序
     */
    export const enableRowDrop = (getList: Function, isSort?: boolean) => {
      const tbody = document.querySelector('.el-table__body-wrapper tbody') as any
    
      // 销毁现有Sortable实例(如果存在)
      if (sortableInstance) {
        sortableInstance.destroy()
      }
    
      // 使用更新后的isSort值创建新的Sortable实例
      sortableInstance = new Sortable(tbody, {
        // 是否禁用拖拽排序
        disabled: !isSort,
        // ms, number 单位:ms,定义排序动画的时间
        animation: 150,
        // 设置拖拽样式类名
        dragClass: 'drop-dragClass',
        // 设置拖拽停靠样式类名
        ghostClass: 'drop-ghostClass',
        // 设置选中样式类名
        chosenClass: 'drop-chosenClass',
    
        onAdd(evt: any) {
          // 拖拽时候添加有新的节点的时候发生该事件
          console.log('onAdd.foo:', [evt.item, evt.from])
        },
        onUpdate(evt: any) {
          // 拖拽更新节点位置发生该事件
          console.log('onUpdate.foo:', [evt.item, evt.from])
        },
        onRemove(evt: any) {
          // 删除拖拽节点的时候促发该事件
          console.log('onRemove.foo:', [evt.item, evt.from])
        },
        onStart(evt: any) {
          // 开始拖拽出发该函数
          console.log('onStart.foo:', [evt.item, evt.from])
        },
        onSort(evt: any) {
          // 发生排序发生该事件
          console.log('onUpdate.foo:', [evt.item, evt.from])
        },
        
        // 关键代码
        onEnd(evt: any) {
          // 结束拖拽
          console.log('结束表格拖拽', `拖动前索引${evt.oldIndex}---拖动后索引${evt.newIndex}`)
          getList(evt)
        }
      })
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    3.2 全局拖拽样式封装
    // 表格开启排序后--拖拽样式
    // 拖拽
    .drop-dragClass {
      background: rgba($color: #blue, $alpha: 0.5) !important;
    }
    // 停靠
    .drop-ghostClass {
      background: rgba($color: #6cacf5, $alpha: 0.5) !important;
    }
    // 选择
    .drop-chosenClass:hover > td {
      background: rgba($color: #blue, $alpha: 0.5) !important;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    四、在 .vue 文件中使用

    4.1 引用
    <script lang="ts" setup>
    ... 其他代码
    
    // 引入我们封装的 表单拖拽函数
    import { enableRowDrop } from '@/utils/sortable'
    
    ...其他代码
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    4.2 e-table 中定义 row-key 和 高度

    在这里插入图片描述

    4.3 定义排序方法
    // 获取--列表页数据
    const getList = (evt: any) => {
      console.log(evt.oldIndex, 'evt')
      console.log('请求数据')
    }
    
    // 排序--当前是否开启排序
    const isSort = ref(false)
    // 排序--点击排序
    const handleSort = () => {
      isSort.value = !isSort.value
    
      /**
       * 1. 把获取列表数据方法闯过去,作为回调函数
       * 2. 把 isSort 传过去,控制表单是否开启排序
       */
      enableRowDrop(getList, isSort.value)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    4.4 给按钮绑定方法
     <!-- 操作按钮区 -->
     <div class="operate-box">
       <el-button type="primary" @click="handleAdd" :icon="CirclePlus" plain>新增</el-button>
        
        // 给按钮绑定 handleSort 方法
       <el-button type="default" @click="handleSort" :icon="Sort" plain>{{ !isSort ? '开始排序' : '关闭排序' }}</el-button>
     </div>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    五、有任何问题,欢迎评论区留言

    创作不易,点赞收藏不迷路

  • 相关阅读:
    现代综合农业拒接形式推广-国稻种芯:谋定关键产业突破口
    Raft 共识算法
    心肺运动试验----各类参数分析笔记
    关于使用API接口获取商品数据的那些事(使用API接口获取商品数据的步骤和注意事项。)
    阿里巴巴商品详情API接口(item_get-获得商品详情接口),阿里巴巴API接口
    Python与ArcGIS系列(七)自动化打印地图
    LeetCode 494.目标和 (动态规划 + 性能优化)二维数组 压缩成 一维数组
    JAVA毕业设计高校请假管理系统计算机源码+lw文档+系统+调试部署+数据库
    html动态爱心超文本标记代码,丝滑流畅有特效,附源码
    es6中proxy如何使用
  • 原文地址:https://blog.csdn.net/qq_61402485/article/details/134048815
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号