element table 行高列宽拖拽调整

因为需求的不同,需要自己实现element表格拖拽调整列宽和行高,目前已实现,现记录了实现的方式。

实现说明:全部基于原生js,不依赖任何插件,可以直接使用。

 实现思路:1.列宽的调整最终是通过调整如上图的col元素的width属性实现列宽调整的。

                    2.行高的调整是动态改变tbody的 el-table__row 的高度实现调整的。

注意点:本实现是针对当前引入页面的所有表格都有效,如需要只针对某个表格,可以在查找元素的地方传参进行判断。给el-border添加自带的属性border可能对拖拽有点影响。

 const ths = document.querySelectorAll('.el-table__header th')
  const tBodyTr = document.querySelectorAll('.el-table__body tbody')
  const tbodys = document.querySelectorAll('.el-table__body-wrapper')

 实现代码如下,可以单独封装为一个js文件,哪里需要用,就在哪里引入使用。

export default function resizeTable() {
  let thCol,
    pointX,
    currentWidth,
    pointY,
    currentHeight,
    TagEl,
    timer,
    tagTab,
    tagTabHeight,
    changeValue

  const ths = document.querySelectorAll('.el-table__header th')
  const tBodyTr = document.querySelectorAll('.el-table__body tbody')
  const tbodys = document.querySelectorAll('.el-table__body-wrapper')

  //---------------------- 行高调整JS------------------------
  function showRowTip(e) {
    const currentTagHeight = e.target.offsetHeight
    const cursorPositionY = e.offsetY
    const classname = e.target.parentNode.className
    if (
      (classname === 'el-table__row' ||
        classname === 'el-table__row el-table__row--striped') &&
      currentTagHeight - cursorPositionY < 8
    ) {
      document.body.style.cursor = 'row-resize'
    } else {
      document.body.style.cursor = 'auto'
    }
  }

  function moveRow(event) {
    const newPointY = getMousePos(event).y
    const moveY = newPointY - pointY
    const Height = currentHeight + moveY
    // 防卡,异步挂起
    timer = setTimeout(() => {
      TagEl.style.height = Height + 'px'
      if (changeValue !== TagEl.offsetHeight) {
        changeValue = TagEl.offsetHeight
      } else {
        TagEl.style.height = changeValue + 'px'
      }
    }, 0)
  }

  function downRowMouse(event) {
    tagTab = event.path.filter((item) => {
      const list = item.classList ? Array.from(item.classList) : []
      if (list.includes('el-table')) {
        return item
      }
    })[0]
    if (tagTab) tagTabHeight = tagTab.offsetHeight
    if (document.body.style.cursor !== 'row-resize') return
    document.body.style['user-select'] = 'none'
    const e = event || window.event
    TagEl = e.target.parentNode
    if (
      TagEl.className === 'el-table__row' ||
      TagEl.classname === 'el-table__row el-table__row--striped'
    ) {
      pointY = getMousePos(e).y
      currentHeight = e.target.offsetHeight
      changeValue = TagEl.offsetHeight
    }
    window.addEventListener('mousemove', moveRow, false)
  }
  tBodyTr.forEach((item) => {
    item.addEventListener('mousemove', showRowTip, false)
    // 补丁,解决从表格body下方离开后鼠标样式没有改变问题
    item.addEventListener(
      'mouseleave',
      function() {
        document.body.style['user-select'] = 'auto'
        document.body.style.cursor = 'auto'
      },
      false
    )
  })
  window.addEventListener('mousedown', downRowMouse, false)

  // -------------------------列宽调整JS----------------------

  tbodys.forEach((item) => {
    item.style.overflow = 'auto'
  })
  // 根据鼠标位置去显示是否可以移动表格
  function showTip(e) {
    const currentTagWidth = e.target.offsetWidth
    const cursorPositionX = e.offsetX
    if (currentTagWidth - cursorPositionX < 8) {
      document.body.style = 'user-select: auto; cursor: col-resize'
      ths.forEach((item) => (item.style.cursor = 'col-resize'))
    } else {
      document.body.style.cursor = 'auto'
      ths.forEach((item) => (item.style.cursor = ''))
    }
  }
  // 计算鼠标位置函数
  function getMousePos(event) {
    const e = event || window.event
    const scrollX =
      document.documentElement.scrollLeft || document.body.scrollLeft
    const scrollY =
      document.documentElement.scrollTop || document.body.scrollTop
    const x = e.pageX || e.clientX + scrollX
    const y = e.pageY || e.clientY + scrollY
    return { x, y }
  }

  function downMouse(e) {
    if (document.body.style.cursor === 'col-resize') {
      let targetThClassName
      const classNameList = e.target.classList
      const flag = [...classNameList].some((item) => item === 'cell')
      if (flag) {
        targetThClassName = e.target.parentNode.classList[0]
      } else {
        targetThClassName = classNameList[0]
      }
      pointX = getMousePos(e).x
      currentWidth = e.target.offsetWidth
      thCol = document.getElementsByName(targetThClassName)
      window.addEventListener('mousemove', tableHandle, false)
    }
  }

  function tableHandle(event) {
    const newPointX = getMousePos(event).x
    const moveX = newPointX - pointX
    timer = setTimeout(() => {
      thCol.forEach((el) => {
        const width = currentWidth + moveX
        if (width >= 50) {
          el.setAttribute('width', width)
        } else {
          el.setAttribute('width', 50)
        }
      })
    }, 0)
  }

  ths.forEach((item) => {
    // 鼠标移入右边界的时候要改变鼠标的样式
    item.addEventListener('mousemove', showTip, false)
    // 当鼠标的样式改变的时候如果按下鼠标,此时需要出现垂直辅助线
  })
  window.addEventListener('mousedown', downMouse, false)

  window.addEventListener(
    'mouseup',
    function(e) {
      clearTimeout(timer)
      document.body.style['user-select'] = 'auto'
      window.removeEventListener('mousemove', moveRow, false)
      window.removeEventListener('mousemove', tableHandle, false)
    },
    false
  )
}

使用方法如下:1.引入js。2.在mounted钩子函数中调用

// 1.引入js文件  ps:注意路径是否正确 
 
import resizeTable from "@/utils/tabelleAnpassen";

// 2.调用方法

  mounted() {
    this.$nextTick(() => {
      resizeTable();
    });
   }

22/07/06补充

     有同学反应在 el-table-column 标签内加上 show-overflow-tooltip 属性后,影响了拖拽的效果,其实并没有影响到拖拽效果,只是在el-table-column 上加上这个属性后,会影响到 tbody tr td 的 cell 的样式,这种情况下,请给  .cell.el-tooltip 添加一个样式  width: auto !important;

 可全局设置样式如下

.el-table .cell.el-tooltip {
  width: auto !important;
}

  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 26
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端 - wei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值