当前位置:   article > 正文

Element-UI 解决el-table合并行+固定列鼠标浮动高亮问题_:span-method 表格合并 鼠标悬浮交互优化

:span-method 表格合并 鼠标悬浮交互优化

今天在搬砖的时候发现了一个问题,当用el-table组件画表格,并且存在合并行时,鼠标浮动的高亮样式有些问题,如下图。


可以看到虽然已经合并成为了一行但是,高亮部分的显示样式仍然是分家状态。由于我画的表格需要有固定列,虽然百度了一些大神的方法,但是仍然没有完全解决我的问题。
找了好久,发现了两个可以解决同时包含固定列和合并列表格高亮错位问题的方法。

方法一

  1. <template>
  2. <el-table
  3. :row-class-name="tableRowClassName"
  4. :data="tableData"
  5. :span-method="objectSpanMethod"
  6. border
  7. style="width: 100%; margin-top: 20px"
  8. @cell-mouse-enter="handleCellMouseEnter"
  9. @cell-mouse-leave="handleCellMouseLeave">
  10. <el-table-column
  11. prop="id"
  12. fixed
  13. label="ID"
  14. width="180"/>
  15. <el-table-column
  16. prop="name"
  17. fixed
  18. label="姓名"/>
  19. <el-table-column
  20. prop="amount1"
  21. label="数值 1(元)"/>
  22. <el-table-column
  23. prop="amount2"
  24. label="数值 2(元)"/>
  25. <el-table-column
  26. prop="amount3"
  27. label="数值 3(元)"/>
  28. <el-table-column
  29. prop="amount4"
  30. label="数值 4(元)"/>
  31. </el-table>
  32. </template>
  33. <script>
  34. data() {
  35. return {
  36. currentIndex: '',
  37. tableData: [{
  38. id: '12987122',
  39. name: '王小虎',
  40. order: '1',
  41. amount1: '234',
  42. amount2: '3.2',
  43. amount4: '4.43',
  44. amount3: 10
  45. }, {
  46. id: '12987123',
  47. name: '王小虎',
  48. order: '1',
  49. amount1: '165',
  50. amount2: '4.43',
  51. amount4: '4.43',
  52. amount3: 12
  53. },
  54. {
  55. id: '12987124',
  56. name: '王小虎',
  57. amount1: '324',
  58. amount2: '1.9',
  59. order: '2',
  60. amount4: '4.43',
  61. amount3: 9
  62. }, {
  63. id: '12987125',
  64. name: '王小虎',
  65. amount1: '621',
  66. amount2: '2.2',
  67. order: '2',
  68. amount4: '4.43',
  69. amount3: 17
  70. }, {
  71. id: '12987126',
  72. name: '王小虎',
  73. amount1: '539',
  74. order: '3',
  75. amount2: '4.1',
  76. amount4: '4.43',
  77. amount3: 15
  78. }]
  79. }
  80. }
  81. methods: {
  82. // 鼠标移入
  83. handleCellMouseEnter(row, column, rowIndex, columnIndex) {
  84. //标记当前是哪个分组
  85. this.currentIndex = row.order
  86. },
  87. // 鼠标移出
  88. handleCellMouseLeave() {
  89. //移出是清空光标
  90. this.currentIndex = ''
  91. },
  92. tableRowClassName({ row, column, rowIndex, columnIndex }) {
  93. //逻辑判断决定是否修改样式,如果分组一致则使用同一个样式
  94. if (row.order == this.currentIndex) {
  95. return 'hover-bg'
  96. } else {
  97. return ''
  98. }
  99. },
  100. // 合并行方法
  101. objectSpanMethod({ row, column, rowIndex, columnIndex }) {
  102. if (columnIndex === 0 || columnIndex === 1) {
  103. if (rowIndex % 2 === 0) {
  104. return {
  105. rowspan: 2,
  106. colspan: 1
  107. }
  108. } else {
  109. return {
  110. rowspan: 0,
  111. colspan: 0
  112. }
  113. }
  114. }
  115. }
  116. }
  117. </script>
  118. <style lang="scss" scoped>
  119. .el-table{
  120. border: 1px solid #e6e6e6;
  121. /deep/ thead th {
  122. background-color: #f5f5f5;
  123. }
  124. /deep/ tr {
  125. height: 20px;
  126. }
  127. /deep/ .el-table__body .el-table__row.hover-bg td{
  128. background-color: #F5F7FA;
  129. }
  130. }
  131. </style>

方法一是通过el-table自带的 ‘row-class-name‘ 属性实现的,原理是每当鼠标切换一个行时,都会触发tableRowClassName方法,通过order判断是否为同一组的数据,如果是则修改样式。


方法二

  1. // 鼠标移入
  2. handleCellMouseEnter(row, column, cell, event) {
  3. this.$nextTick(() => {
  4. //获取鼠标移入时的tbody结点
  5. const tbody = this.$el.querySelector('.el-table__body-wrapper table tbody')
  6. //循环获取tr结点
  7. for (let i = 0; i < tbody.children.length; i++) {
  8. const tr = tbody.children[i]
  9. //逻辑判断,这步已经获取到了tr所以tableData[i]与tr是一致的
  10. if (this.tableData[i].order == row.order) {
  11. //改变tr的背景颜色
  12. tr.style.background = '#f5f5f5'
  13. //循环获取td,改变td的样式
  14. for (let j = 0; j < tr.children.length; j++) {
  15. const td = tr.children[j]
  16. td.style.background = '#f5f5f5'
  17. }
  18. } else {
  19. tr.style.background = '#fff'
  20. for (let j = 0; j < tr.children.length; j++) {
  21. const td = tr.children[j]
  22. td.style.background = '#fff'
  23. }
  24. }
  25. }
  26. //这部分是为了修改固定列部分样式的代码,逻辑与上面的一致
  27. const tbody_fix = this.$el.querySelector('.el-table__fixed table tbody')
  28. for (let i = 0; i < tbody_fix.children.length; i++) {
  29. const tr = tbody_fix.children[i]
  30. if (this.tableData[i].order == row.order) {
  31. tr.style.background = '#f5f5f5'
  32. for (let j = 0; j < tr.children.length; j++) {
  33. const td = tr.children[j]
  34. td.style.background = '#f5f5f5'
  35. }
  36. } else {
  37. tr.style.background = '#fff'
  38. for (let j = 0; j < tr.children.length; j++) {
  39. const td = tr.children[j]
  40. td.style.background = '#fff'
  41. }
  42. }
  43. }
  44. })
  45. },
  46. // 鼠标移出
  47. handleCellMouseLeave(row, column, cell, event) {
  48. //与鼠标移入的逻辑一致
  49. this.$nextTick(() => {
  50. const tbody = this.$el.querySelector('.el-table__body-wrapper table tbody')
  51. for (let i = 0; i < tbody.children.length; i++) {
  52. const tr = tbody.children[i]
  53. if (this.tableData[i].order == row.order) {
  54. tr.style.background = '#fff'
  55. for (let j = 0; j < tr.children.length; j++) {
  56. const td = tr.children[j]
  57. td.style.background = '#fff'
  58. }
  59. }
  60. }
  61. //这部分是为了修改固定列部分样式的代码
  62. const tbody_fix = this.$el.querySelector('.el-table__fixed table tbody')
  63. for (let i = 0; i < tbody_fix.children.length; i++) {
  64. const tr = tbody_fix.children[i]
  65. if (this.tableData[i].order == row.order) {
  66. tr.style.background = '#fff'
  67. for (let j = 0; j < tr.children.length; j++) {
  68. const td = tr.children[j]
  69. td.style.background = '#fff'
  70. }
  71. }
  72. }
  73. })
  74. },

方法二是直接通过nextTick获取结点样式进行逻辑判断修改样式实现的。

实现效果:

 

需要注意的是,这两种方法都需要事先将准备渲染的list进行分组,例如  tableData中的order就相当于一个组id,带有相同的order值的数据,显示的样式应该是一致的;这个无需特意添加,比如要求name相同的分为一组则name就可以作为组id,如果实在没有的话,那就需要手动循环进行添加啦!

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

闽ICP备14008679号