table列表左右拖拽实现滑动效果

el-table表格实现左右拖动效果

全局注册指令:tableMove ,main.js引入文件

实现方式:<el-table :data="tableData" v-tableMove></el-table>

// 全局添加table左右拖动效果的指令
  Vue.directive('tableMove', {
    bind: function(el, binding, vnode) {
      var odiv = el // 获取当前表格元素

      // 修改样式小手标志
      // el.style.cursor = 'pointer'
      el.querySelector('.el-table .el-table__body-wrapper').style.cursor = 'pointer'

      var mouseDownAndUpTimer = null
      var mouseOffset = 0
      var mouseFlag = false

      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['yourRefName'].doLayout()
        }, 200)
        if (mouseFlag) {
          mouseFlag = false
        } else {
          clearTimeout(mouseDownAndUpTimer) // 清除延迟时间
        }
      }

      odiv.onmouseleave = (e) => {
        setTimeout(() => {
          // 解决拖动列宽行不对齐问题--渲染表格
          vnode.context.$refs['yourRefName'].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.onclick = (e) => {
      //   setTimeout(() => {
      //     if (mouseFlag) return
      //     if (e.target.nodeName === 'SPAN') {
      //       console.log(mouseFlag, e.target.nodeName, this)
      //       var aux = document.createElement('input')
      //       aux.setAttribute('value', e.target.outerText)
      //       document.body.appendChild(aux)
      //       aux.select()
      //       document.execCommand('copy')
      //       document.body.removeChild(aux)
      //       if (document.execCommand('copy')) {
      //         console.log('复制成功')
      //       } else {
      //         console.log('复制失败')
      //       }
      //     }
      //   }, 300)
      // }

      // 是否拖拽可选中文字
      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
      }
    }
  })

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值