el-table实现表格内部横向拖拽效果

2024.4.2今天我学习了如何对el-table表格组件实现内部横向拖拽的效果,效果:

代码如下:

一、创建utils/底下文件

const crosswise_drag_table = function (Vue){
  // 全局添加table左右拖动效果的指令
  Vue.directive('tableMove', {
    bind: function (el, binding, vnode) {
      let odiv = el // 获取当前表格元素
      // 修改样式小手标志
      // el.style.cursor = 'pointer'
      el.querySelector('.el-table .el-table__body-wrapper').style.cursor = 'pointer'
      let mouseDownAndUpTimer = null
      let mouseOffset = 0
      let mouseFlag = false
      let bindRef = binding.value[0] //绑定的表格的ref属性
      odiv.onmousedown = (e) => {
        const ePath = composedPath(e)
        // 拖拽表头不滑动
        if (ePath.some(res => {
          return res && res.className && res.className.indexOf('el-table__header') > -1
        })) return
        if (e.which !== 1) return
        mouseOffset = e.clientX
        mouseDownAndUpTimer = setTimeout(function () {
          mouseFlag = true
        }, 80)
      }
      odiv.onmouseup = (e) => {
        setTimeout(() => {
          // 解决拖动列宽行不对齐问题--渲染表格
          vnode.context.$refs[bindRef].doLayout()
        }, 200)
        if (mouseFlag) {
          mouseFlag = false
        } else {
          clearTimeout(mouseDownAndUpTimer) // 清除延迟时间
        }
      }

      odiv.onmouseleave = (e) => {
        setTimeout(() => {
          // 解决拖动列宽行不对齐问题--渲染表格
          vnode.context.$refs[bindRef].doLayout()
        }, 200)
        mouseFlag = false
      }

      odiv.onmousemove = (e) => {
        if (e.which !== 1) return

        const divData = odiv.querySelector('.el-table .el-table__body-wrapper')
        if (mouseFlag && divData) {
          // 设置水平方向的元素的位置
          divData.scrollLeft -= (-mouseOffset + (mouseOffset = e.clientX))
        }
      }

      // 解决有些时候,在鼠标松开的时候,元素仍然可以拖动;
      odiv.ondragstart = (e) => {
        e.preventDefault()
      }

      odiv.ondragend = (e) => {
        e.preventDefault()
      }

      // 是否拖拽可选中文字
      odiv.onselectstart = () => {
        return false
      }

      //浏览器Event.path属性不存在
      function composedPath(e) {
        // 存在则直接return
        if (e.path) {
          return e.path
        }
        // 不存在则遍历target节点
        let target = e.target
        e.path = []
        while (target.parentNode !== null) {
          e.path.push(target)
          target = target.parentNode
        }
        // 最后补上document和window
        e.path.push(document, window)
        return e.path
      }
    }
  })
}


export default crosswise_drag_table

二、在main.js中引入

// 横向拖拽表格
import  crosswise_drag_table   from '@/utils/crosswiseDragTable'

Vue.use(crosswise_drag_table   )

三、在vue文件中使用

<el-table 
  ref='table_drag'
  v-table-move="['table_drag']"
>

</el-table>

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-03 08:36:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-03 08:36:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-03 08:36:01       82 阅读
  4. Python语言-面向对象

    2024-04-03 08:36:01       91 阅读

热门阅读

  1. 微信小程序中路由跳转方式

    2024-04-03 08:36:01       40 阅读
  2. Python网络爬虫(二):Requests库

    2024-04-03 08:36:01       39 阅读
  3. SQLAlchemy 的数据库引擎engine与连接对象Connection

    2024-04-03 08:36:01       34 阅读
  4. Knife4j配置使用笔记

    2024-04-03 08:36:01       36 阅读
  5. nodejs的express负载均衡

    2024-04-03 08:36:01       35 阅读
  6. 每天学习一个Linux命令之dpkg

    2024-04-03 08:36:01       42 阅读
  7. linux扩展正则表达式之+

    2024-04-03 08:36:01       38 阅读
  8. JVM中一次完整的 GC 流程

    2024-04-03 08:36:01       43 阅读
  9. 2023年网络安全领域新兴技术的发展特点

    2024-04-03 08:36:01       36 阅读