当前位置:   article > 正文

el-table 表格从下往上滚动,触底自动请求新数据

el-table 表格从下往上滚动,触底自动请求新数据

关键点:

1、 el-table 需要设置高度 height;

2、el-table 外层盒子需要设置一个高度,并且设置 overflow:hidden;

3、获取 el-table 的 bodyWrapper:divData

      divData.scrollTop + divData.clientHeight + 1 >= divData.scrollHeight;(触底)

      重新请求数据;并将列表置顶:设置 divData.scrollTop = 0;

4、为el-table 添加鼠标移入移除事件,启停滚动;

  1. <!--
  2. * @Author: xxx
  3. * @objectDescription: 滚动、合并行table
  4. * @Date: 2024-04-16 14:35:58
  5. -->
  6. <template>
  7. <div class="span-roll-table">
  8. <div class="table-contain">
  9. <el-table
  10. ref="rollTable"
  11. :data="list"
  12. height="100%"
  13. :span-method="objectSpanMethod"
  14. :header-cell-style="{
  15. textAlign: 'center',
  16. width: 'fit-content',
  17. backgroundColor: '#F2F6FC',
  18. }"
  19. class="table"
  20. @mouseenter.native="stopScroll"
  21. @mouseleave.native="startScroll"
  22. >
  23. </el-table>
  24. </div>
  25. </div>
  26. </template>
  27. <script>
  28. export default {
  29. data() {
  30. return {
  31. list: [],
  32. rollTimer: null,
  33. };
  34. },
  35. mounted() {
  36. this.startScroll();
  37. },
  38. methods: {
  39. // 获取新数据
  40. async onChange() {
  41. this.list = [
  42. {
  43. name: "1",
  44. },
  45. {
  46. name: "2",
  47. },
  48. {
  49. name: "3",
  50. },
  51. {
  52. name: "4",
  53. },
  54. ];
  55. // 将列表置顶
  56. this.$nextTick(() => {
  57. const table = this.$refs.rollTable;
  58. const divData = table.bodyWrapper;
  59. divData.scrollTop = 0;
  60. });
  61. },
  62. // 开始滚动
  63. startScroll() {
  64. this.tableScroll(false);
  65. },
  66. // 停止滚动
  67. stopScroll() {
  68. this.tableScroll(true);
  69. },
  70. // 列表滚动
  71. tableScroll(stop) {
  72. if (stop) {
  73. if (this.rollTimer) {
  74. clearInterval(this.rollTimer);
  75. return;
  76. }
  77. }
  78. const table = this.$refs.rollTable;
  79. const divData = table.bodyWrapper;
  80. this.rollTimer = setInterval(() => {
  81. divData.scrollTop += 2;
  82. this.$nextTick(() => {
  83. if (
  84. divData.scrollTop + divData.clientHeight + 1 >=
  85. divData.scrollHeight
  86. ) {
  87. this.onChange();
  88. }
  89. });
  90. }, 200);
  91. },
  92. // 合并列表第一个单元格(并列排名)
  93. objectSpanMethod({ row, column, rowIndex, columnIndex }) {
  94. if (columnIndex === 0) {
  95. // 获取当前单元格的值(单元格项一定要配置 prop 属性,否则拿不到值!!!!)
  96. const currentValue = row[column.property];
  97. // 获取上一行相同列的值
  98. const preRow = this.typeTableData[rowIndex - 1];
  99. const preValue = preRow ? preRow[column.property] : null;
  100. // 如果当前值和上一行的值相同,则将当前单元格隐藏
  101. if (currentValue === preValue) {
  102. return { rowspan: 0, colspan: 0 };
  103. } else {
  104. // 否则计算当前单元格应该跨越多少行
  105. let rowspan = 1;
  106. for (let i = rowIndex + 1; i < this.typeTableData.length; i++) {
  107. const nextRow = this.typeTableData[i];
  108. const nextValue = nextRow[column.property];
  109. if (nextValue === currentValue) {
  110. rowspan++;
  111. } else {
  112. break;
  113. }
  114. }
  115. return { rowspan, colspan: 1 };
  116. }
  117. }
  118. },
  119. },
  120. beforeDestroy() {
  121. clearInterval(this.rollTimer);
  122. this.startScroll = () => {};
  123. },
  124. };
  125. </script>
  126. <style lang="less" scoped>
  127. .span-roll-table {
  128. height: 400px;
  129. .table-contain {
  130. height: 100%;
  131. overflow: hidden;
  132. }
  133. }
  134. /* 隐藏滚动条,但仍然可以滚动 */
  135. ::v-deep .el-table__body-wrapper::-webkit-scrollbar {
  136. display: none; /* 针对Webkit浏览器 */
  137. }
  138. </style>

参考文章

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

闽ICP备14008679号