{ adjustColumnWidth(el, vnode); }, 0) },_el-table滚动条错位">
当前位置:   article > 正文

解决el-table滚动条错位问题(完美级)

el-table滚动条错位

创建一个js文件,并在main.js引入:

  1. import Vue from "vue";
  2. Vue.directive("tableFit", {
  3. //指令所在组件的 VNode 及其子 VNode 全部更新后调用。
  4. componentUpdated(el, binding, vnode) {
  5. setTimeout(() => {
  6. adjustColumnWidth(el, vnode);
  7. }, 0)
  8. },
  9. });
  10. function adjustColumnWidth(table, vnode) {
  11. //中文和全角正则
  12. const CN = new RegExp("[\u4E00-\u9FA5]|[^\uFF00-\uFFFF]");
  13. const NUM = new RegExp("[0-9]");
  14. //中文字体的像素宽度比例
  15. const CN_RATE = 1.1
  16. //数字字体的像素宽度比例
  17. const NUM_RATE = 0.65
  18. //其他字体的像素宽度比例
  19. const OTHER_RATE = 0.5
  20. const columns = vnode.child.columns.slice()
  21. //el-table通过colgroup标签设置html宽度
  22. const colgroup = table.querySelector("colgroup");
  23. const colDefs = [...colgroup.querySelectorAll("col")];
  24. //忽略 设置了宽度 序号 多选框 的列
  25. //判断gutter是否已存在
  26. const gutter = colgroup.querySelector(`col[name=gutter]`)
  27. const gutterx = colgroup.querySelector(`col[name=gutterx]`)
  28. let except = 0
  29. if (gutter || gutterx) {
  30. //删除gutter
  31. colDefs.pop()
  32. }
  33. //若有序号 多选框则删除
  34. except = colDefs.length - columns.length
  35. colDefs.splice(0, except)
  36. for (let i = columns.length - 1; i >= 0; i--) {
  37. if (columns[i].width) {
  38. colDefs.splice(i, 1)
  39. columns.splice(i, 1)
  40. }
  41. }
  42. //设置每列宽度
  43. colDefs.forEach((col, index) => {
  44. //colgroup中 和 表头标签的class名相同 通过class寻找相同列
  45. const clsName = col.getAttribute("name");
  46. const cells = [
  47. ...table.querySelectorAll(`.el-table__body-wrapper td.${clsName}`),
  48. ...table.querySelectorAll(`th.${clsName}`),
  49. ];
  50. const widthList = cells.map((el) => {
  51. const cell = el.querySelector(".cell")
  52. if (cell) {
  53. let fontSize = parseInt(window.getComputedStyle(cell, null).fontSize)
  54. fontSize = fontSize ? fontSize : 14
  55. let width = 0
  56. //计算每个字符的宽度
  57. for (let str of cell.innerText) {
  58. if (CN.test(str)) {
  59. width += fontSize * CN_RATE
  60. } else if (NUM.test(str)) {
  61. width += fontSize * NUM_RATE
  62. } else {
  63. width += fontSize * OTHER_RATE
  64. }
  65. }
  66. return Math.ceil(width)
  67. } else {
  68. return 0
  69. }
  70. });
  71. //取一列中的最大宽度
  72. const max = Math.max(...widthList);
  73. if (max !== 0) {
  74. //在表格数据中设置minWidth 防止尺寸变化重新计算原来的宽度
  75. //20 + 2 20 是cell类的padding 2 是给予额外空间
  76. columns[index].minWidth = max + 22
  77. table.querySelectorAll(`col[name=${clsName}]`).forEach((el) => {
  78. el.setAttribute("width", max + 22);
  79. });
  80. }
  81. });
  82. //设置完后调用el-table方法更新布局
  83. vnode.child.doLayout()
  84. tableRevise(table)
  85. }
  86. //修正el-table错位
  87. function tableRevise(table) {
  88. const tableWrapper = table.querySelector('.el-table__body-wrapper')
  89. const tableBody = table.querySelector('.el-table__body')
  90. const colgroup = table.querySelector("colgroup");
  91. /**
  92. * (以下数值为滚动条高度,可以自己根据情况通过class重新修改)
  93. */
  94. //内容大于容器时
  95. if (tableBody.clientWidth > tableWrapper.offsetWidth) {
  96. //设置x轴滚动
  97. tableWrapper.style.overflowX = 'auto'
  98. //解决固定列错位 (没有错位的可以忽略以下内容)
  99. let fixedWrap = table.querySelectorAll('.el-table .el-table__fixed-body-wrapper')
  100. if (fixedWrap.length > 0) {
  101. fixedWrap.forEach(item => {
  102. item.style.paddingBottom = 15 + 'px'
  103. })
  104. }
  105. //解决固定列覆盖滚动条
  106. let fixed_left = table.querySelector('.el-table .el-table__fixed')
  107. let fixed_right = table.querySelector('.el-table .el-table__fixed-right')
  108. if (fixed_left) {
  109. fixed_left.style.height = 'calc(100% - 15px)'
  110. }
  111. if (fixed_right) {
  112. fixed_right.style.height = 'calc(100% - 15px)'
  113. }
  114. //解决表头偏移
  115. //没有原生的gutter时自己新增一个
  116. const gutter = colgroup.querySelector(`col[name=gutter]`)
  117. const gutterx = colgroup.querySelector(`col[name=gutterx]`)
  118. if (!gutter && !gutterx) {
  119. let col = document.createElement('col')
  120. col.setAttribute('name', 'gutterx')
  121. col.setAttribute('width', '15')
  122. colgroup.appendChild(col)
  123. }
  124. }
  125. }

 使用:

 <el-table v-tableFit></el-table>

例如:

内容搬运地址 脚本之家,可以说是很耐斯了:https://www.jb51.net/article/203639.htm,找了许多文章,用下面的方法 还是会出现问题,最终选择了脚本之家

  1. .el-scrollbar__wrap::-webkit-scrollbar{
  2. width: 15px !important;
  3. height: 15px !important;
  4. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/850917
推荐阅读
相关标签
  

闽ICP备14008679号