el-table-scroll.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * el-table 中跳转到指定下标
  3. * @param tableRef 表格的ref
  4. * @param index 下标
  5. */
  6. export const setScrollTop = (tableRef, index) => {
  7. tableRef.setScrollTop(getScrollTop(tableRef, index))
  8. }
  9. /**
  10. * 计算想要数据距离表格的顶部距离
  11. * @param tableRef 表格
  12. * @param index 下标
  13. * @returns {number} 返回距离头部的距离
  14. */
  15. export const getScrollTop = (tableRef, index) => {
  16. try {
  17. const targetTop = tableRef.$el.querySelectorAll('.el-table__body tr')[index].getBoundingClientRect().top //该行的位置
  18. const containerTop = tableRef.$el.querySelector('.el-table__body').getBoundingClientRect().top //body的位置
  19. return targetTop - containerTop
  20. } catch (e) {
  21. return 0
  22. }
  23. }
  24. /**
  25. * 根据数据的下标平滑的显示那个人的信息并且高亮
  26. * @param tableRef 表格
  27. * @param columnIndex 下标
  28. * @param columnData 要高亮的数据信息
  29. */
  30. export function smoothScrollTableColumn(tableRef, columnIndex, columnData) {
  31. tableRef.setCurrentRow(columnData)
  32. tableRef.scrollTo({
  33. top: getScrollTop(tableRef, columnIndex),
  34. left: 0,
  35. behavior: 'smooth',
  36. })
  37. }