You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.5 KiB

3 weeks ago
  1. <script setup>
  2. import { ref, onMounted, reactive, computed, nextTick } from 'vue'
  3. // 变量
  4. const allData = ref([])
  5. const tableData = ref([])
  6. const elTableHeight = ref('')
  7. const theadHeight = ref('')
  8. const contentHeight = ref(0)
  9. const showRowCount = ref(0)
  10. const falseBox = ref(null)
  11. const scollBoxHeight = ref(0)
  12. const scrollTopRowCount = ref(0)
  13. // 方法
  14. onMounted(async function () {
  15. allData.value = []
  16. for (let i = 0; i < 10000; i++) {
  17. allData.value.push({
  18. name: '张三' + i,
  19. age: 20 + i,
  20. address: '北京市海淀区' + i,
  21. salary: 50000 + i,
  22. date: '2022-01-01'
  23. })
  24. }
  25. scollBoxHeight.value = 45 * allData.value.length + 45 //多加一行,要不然滚动到底差一行
  26. nextTick(() => {
  27. elTableHeight.value = document.querySelector(
  28. '.table-box .el-table'
  29. ).offsetHeight
  30. theadHeight.value = document.querySelector(
  31. '.table-box .el-table__header-wrapper'
  32. ).offsetHeight
  33. console.log('elTableHeight', elTableHeight.value)
  34. console.log('theadHeight', theadHeight.value)
  35. contentHeight.value = elTableHeight.value - theadHeight.value
  36. console.log('contentHeight', contentHeight.value)
  37. showRowCount.value = Math.floor(contentHeight.value / 45)
  38. console.log('showRowCount', showRowCount.value)
  39. // 获取默认显示的前 <showRowCount> 条
  40. tableData.value = JSON.parse(JSON.stringify(allData.value)).splice(
  41. 0,
  42. showRowCount.value
  43. )
  44. })
  45. falseBox.value = document.querySelector('.false-box')
  46. falseBox.value.addEventListener('scroll', function (e) {
  47. scrollTopRowCount.value = Math.ceil(e.target.scrollTop / 45)
  48. // 获取从索引<scrollTopRowCount> 开始 <showRowCount> 条
  49. tableData.value = JSON.parse(JSON.stringify(allData.value)).splice(
  50. scrollTopRowCount.value,
  51. showRowCount.value
  52. )
  53. })
  54. })
  55. </script>
  56. <template>
  57. <div
  58. class="box"
  59. style="width: 1000px; height: 500px; overflow: hidden; position: relative"
  60. >
  61. <!-- false-box这是一个用来显示滚动条的方法 -->
  62. <div
  63. class="false-box"
  64. style="
  65. width: 100%;
  66. height: 100%;
  67. position: absolute;
  68. top: 0%;
  69. left: 0%;
  70. overflow: auto;
  71. "
  72. >
  73. <div class="scroll-box" :style="{ height: scollBoxHeight + 'px' }">
  74. <!-- scroll-box是用来撑起盒子的方法 -->
  75. </div>
  76. </div>
  77. <div class="table-box">
  78. <el-table class="el-table" :data="allData" style="width: 100%">
  79. <el-table-column prop="name" label="姓名" width="180">
  80. </el-table-column>
  81. <el-table-column prop="age" label="年龄" width="180"> </el-table-column>
  82. <el-table-column prop="address" label="地址"> </el-table-column>
  83. <el-table-column prop="salary" label="薪资" width="180">
  84. </el-table-column>
  85. <el-table-column prop="date" label="日期" width="180">
  86. </el-table-column>
  87. </el-table>
  88. </div>
  89. </div>
  90. </template>
  91. <style scoped>
  92. .box {
  93. width: 1000px;
  94. height: 500px;
  95. overflow: hidden;
  96. position: relative;
  97. }
  98. .false-box {
  99. width: 100%;
  100. height: 100%;
  101. position: absolute;
  102. top: 0%;
  103. left: 0%;
  104. overflow: auto;
  105. }
  106. .scroll-box {
  107. width: 100%;
  108. height: 1000px;
  109. position: absolute;
  110. top: 0%;
  111. left: 0%;
  112. }
  113. .table-box {
  114. width: calc(100% - 20px);
  115. height: 100%;
  116. position: absolute;
  117. top: 0%;
  118. left: 0%;
  119. }
  120. :deep(.el-table) {
  121. width: 100%;
  122. height: 100%;
  123. }
  124. .el-table .el-table__row {
  125. height: 45px;
  126. }
  127. .el-table .el-table__row td {
  128. padding: 0px;
  129. }
  130. </style>