vue表格实现固定表头和首列

àì夳堔傛蜴生んèń 2022-12-05 12:12 970阅读 0赞

前言

前段时间做移动端的项目,项目中需要一个固定表头和首列的表格,但由于是移动端的,组件库里没有类似的,于是,就去网上找看有没有类似的,结果越找越气,10个文章9个抄,抄也行,你倒是抄个能用的啊,一篇根本就不能用的文章,抄个什么劲?有意义???
没办法,只有自己写一个了。

效果图

在这里插入图片描述

实现思路

1、首先分为四部分,左上角固定不动的表头,表头部分,首列部分,表格主体部分
2、整个表格添加定位position: relative;左上角表头添加position: fixed;
3、给白色主体部分添加滚动监听事件,在滑动的同时,使首列的scrollLeft等于主体部分的scrollLeft值;使表头的scrollTop值等于主体的scrollTop值;
4、完事,觉得可以就点个赞吧!

不多说,完整代码如下,拿去即用

  1. <template>
  2. <div class="pages">
  3. <div class="tables">
  4. <div class="tits">
  5. <div class="titsLeft"><p>左上角</p></div>
  6. <div class="titsRight" ref="titsRight">
  7. <div>
  8. <p v-for="(item, i) in 50" :key="i">表头{
  9. { i + 1 }}</p>
  10. </div>
  11. </div>
  12. </div>
  13. <div class="tbody">
  14. <div class="tbodyLeft" ref="tbodyLeft">
  15. <div>
  16. <p v-for="(item, i) in 50" :key="i">首列{
  17. { i + 1 }}</p>
  18. </div>
  19. </div>
  20. <div class="tbodyRight" @scroll="scrollEvent($event)" ref="tbodyRight">
  21. <div v-for="(item, i) in 50" :key="i" class="row">
  22. <p v-for="(item1, i1) in 50" :key="i1">{
  23. { i + 1 }}-{
  24. { i1 + 1 }}</p>
  25. </div>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. </template>
  31. <script>
  32. export default {
  33. name: 'tables',
  34. data() {
  35. return {};
  36. },
  37. methods: {
  38. scrollEvent(e) {
  39. let tbodyRightScrollLeft = e.target.scrollLeft;
  40. let tbodyRightScrollTop = e.target.scrollTop;
  41. this.$refs.titsRight.scrollLeft = tbodyRightScrollLeft;
  42. this.$refs.tbodyLeft.scrollTop = tbodyRightScrollTop;
  43. }
  44. }
  45. };
  46. </script>
  47. <style scoped lang="scss">
  48. * {
  49. margin: 0;
  50. padding: 0;
  51. }
  52. p {
  53. width: 50px;
  54. height: 40px;
  55. text-align: center;
  56. line-height: 40px;
  57. flex-shrink: 0;
  58. }
  59. .tables {
  60. width: 310px; //自定义表格整体宽度
  61. font-size: 12px;
  62. overflow: hidden;
  63. box-sizing: border-box;
  64. display: flex;
  65. flex-direction: column;
  66. position: relative;
  67. border-right: 1px solid red;
  68. border-bottom: 1px solid red;
  69. .tbody{
  70. height: 400px; //自定义表格内容高度
  71. }
  72. > div {
  73. display: flex;
  74. }
  75. div {
  76. flex-shrink: 0;
  77. }
  78. .tits {
  79. height: 40px;
  80. padding-left: 60px;
  81. }
  82. .titsLeft,
  83. .tbodyLeft {
  84. width: 60px;
  85. }
  86. .titsRight,
  87. .tbodyRight {
  88. width: 250px; //自定义表头表体内容宽度
  89. }
  90. .titsLeft {
  91. position: fixed;
  92. top: 0;
  93. left: 0;
  94. p {
  95. width: 60px;
  96. background-color: orange;
  97. }
  98. }
  99. .titsRight {
  100. overflow: hidden;
  101. white-space: nowrap;
  102. background-color: pink;
  103. div {
  104. width: 500px;
  105. height: 400px;
  106. display: flex;
  107. }
  108. }
  109. .tbodyLeft {
  110. overflow: hidden;
  111. white-space: nowrap;
  112. background-color: pink;
  113. div {
  114. width: 50px;
  115. }
  116. }
  117. .tbodyRight {
  118. overflow: hidden;
  119. overflow: auto;
  120. white-space: nowrap;
  121. background-color: none;
  122. display: flex;
  123. flex-direction: column;
  124. .row {
  125. display: flex;
  126. }
  127. > :nth-child(2n+1) {
  128. p{
  129. //background-color:#ccc;
  130. }
  131. }
  132. }
  133. }
  134. </style>

发表评论

表情:
评论列表 (有 0 条评论,970人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Table固定表头

    一、实现思路:表格数据中存在合并单元格,网上找了好多列子都没有实现。最后是把一个table拆分为两个table实现的,第一个table(table1)中是需要固定的列及表头(表