table.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <template>
  2. <div class="auto-scroll-container">
  3. <!-- :height="tableHeight" :height="tableData.length>4?tableHeight:'auto'"-->
  4. <el-table
  5. ref="scrollTable"
  6. :data="tableData"
  7. :height="tableHeight"
  8. @mouseenter.native="pauseScroll"
  9. @mouseleave.native="resumeScroll"
  10. @scroll.passive="handleScroll"
  11. :style="'width: 100%;background: transparent;max-height:'+tableHeight"
  12. >
  13. <el-table-column :cell-style="{ whiteSpace: 'pre-line' }" v-for="(ite,idx) in columns" :key="idx" align='center' :prop="ite.prop" :label="ite.label"
  14. :width="ite.width">
  15. <template slot-scope="scope" >
  16. <span v-if="ite.type&&ite.type=='katype'" :class="[scope.row.name == '1'||scope.row.name == '5'? 'coa' : scope.row.name == '2'? 'cob':scope.row.name == '3'||scope.row.name == '4'? 'coc' : '']" >{{ statusFormat(scope.row.name,statusOptions)}}</span>
  17. <div v-else-if="ite.type&&ite.type=='hh'">
  18. <div>{{ scope.row[ite.prop]}}</div>
  19. <div>{{ scope.row['time']}}</div>
  20. </div>
  21. <div v-else >{{ scope.row[ite.prop] }}</div>
  22. </template>
  23. </el-table-column>
  24. </el-table>
  25. <!-- 可选的滚动控制按钮 -->
  26. <!-- <div class="scroll-controls">
  27. <el-button @click="scrollToTop">滚动到顶部</el-button>
  28. <el-button @click="toggleAutoScroll">
  29. {{ autoScrollEnabled ? '暂停滚动' : '继续滚动' }}
  30. </el-button>
  31. </div> -->
  32. </div>
  33. </template>
  34. <script>
  35. export default {
  36. name: 'AutoScrollTable',
  37. props: {
  38. columns: {
  39. type: Array,
  40. required: true
  41. },
  42. data: {
  43. type: Array,
  44. required: true
  45. },
  46. statusOptions: {
  47. type: Array,
  48. default: () => []
  49. },
  50. tableHeight:{
  51. type: String,
  52. default: '100px' // ElementUI默认行高
  53. },
  54. scrollTop:{
  55. type: Number,
  56. default: 36 // 滚动的高度
  57. }
  58. },
  59. data() {
  60. return {
  61. tableData: [],
  62. autoScrollEnabled: true,
  63. scrollInterval: null,
  64. dataRefreshInterval: null,
  65. isUserScrolling: false,
  66. lastScrollTime: 0,
  67. }
  68. },
  69. mounted() {
  70. this.loadData();
  71. // this.startAutoScroll();
  72. // this.setupDataRefresh();
  73. },
  74. beforeDestroy() {
  75. this.stopAutoScroll();
  76. this.clearDataRefresh();
  77. },
  78. methods: {
  79. statusFormat(row, list) {
  80. return this.selectDictLabel(list, row);
  81. },
  82. async loadData() {
  83. try {
  84. // const data = await this.fetchData();
  85. this.tableData = this.data;
  86. } catch (error) {
  87. console.error('加载数据失败:', error);
  88. }
  89. },
  90. startAutoScroll() {
  91. this.stopAutoScroll();
  92. const tableWrapper = this.$refs.scrollTable?.bodyWrapper;
  93. if (!tableWrapper) return;
  94. // 初始滚动位置设为0
  95. tableWrapper.scrollTop = 0;
  96. this.scrollInterval = setInterval(() => {
  97. if (!this.autoScrollEnabled || this.isUserScrolling) return;
  98. const tableWrapper = this.$refs.scrollTable?.bodyWrapper;
  99. if (!tableWrapper) return;
  100. // 如果已经滚动到底部,回到顶部
  101. if (tableWrapper.scrollTop + tableWrapper.clientHeight >= tableWrapper.scrollHeight - 2) {
  102. tableWrapper.scrollTop = 0;
  103. } else {
  104. tableWrapper.scrollTop += 2;
  105. }
  106. }, 100);
  107. },
  108. stopAutoScroll() {
  109. if (this.scrollInterval) {
  110. clearInterval(this.scrollInterval);
  111. this.scrollInterval = null;
  112. }
  113. },
  114. pauseScroll() {
  115. this.autoScrollEnabled = false;
  116. },
  117. resumeScroll() {
  118. // 如果用户正在手动滚动,不恢复自动滚动
  119. if (!this.isUserScrolling) {
  120. this.autoScrollEnabled = true;
  121. }
  122. },
  123. toggleAutoScroll() {
  124. this.autoScrollEnabled = !this.autoScrollEnabled;
  125. if (this.autoScrollEnabled) {
  126. this.isUserScrolling = false;
  127. }
  128. },
  129. handleScroll({ scrollTop }) {
  130. // 记录最后一次滚动时间
  131. this.lastScrollTime = Date.now();
  132. // 如果用户手动滚动,暂停自动滚动
  133. if (Math.abs(scrollTop - this.lastScrollTop) > 5) {
  134. this.isUserScrolling = true;
  135. this.autoScrollEnabled = false;
  136. }
  137. this.lastScrollTop = scrollTop;
  138. // 如果用户停止滚动超过2秒,恢复自动滚动
  139. clearTimeout(this.scrollResumeTimeout);
  140. this.scrollResumeTimeout = setTimeout(() => {
  141. this.isUserScrolling = false;
  142. this.autoScrollEnabled = true;
  143. }, 2000);
  144. },
  145. scrollToTop() {
  146. const tableWrapper = this.$refs.scrollTable?.bodyWrapper;
  147. if (tableWrapper) {
  148. tableWrapper.scrollTop = 0;
  149. }
  150. },
  151. scrollTo(position) {
  152. const tableWrapper = this.$refs.scrollTable?.bodyWrapper;
  153. if (tableWrapper) {
  154. tableWrapper.scrollTop = position;
  155. }
  156. },
  157. setupDataRefresh() {
  158. this.clearDataRefresh();
  159. this.dataRefreshInterval = setInterval(() => {
  160. this.loadData();
  161. }, 60000); // 每分钟刷新一次
  162. },
  163. clearDataRefresh() {
  164. if (this.dataRefreshInterval) {
  165. clearInterval(this.dataRefreshInterval);
  166. this.dataRefreshInterval = null;
  167. }
  168. }
  169. }
  170. }
  171. </script>
  172. <style scoped lang="scss">
  173. .coa{color: #FFBB37;}
  174. .cob{color: #0096FF;}
  175. .coc{color: #00FFFF;}
  176. .aimgs{min-height: 80px;width: 100%;display: flex;align-items: center;justify-content: center;}
  177. ::v-deep .el-table tr{background-color: transparent;color: #FFFFFF;}
  178. ::v-deep .el-table td{border:none;font-size: 14px;height: 36px;padding: 0;}
  179. ::v-deep .el-table .el-table__header-wrapper th{background-color: transparent !important;color: #fff;padding: 2px 0;height: 36px;box-sizing: border-box;font-size: 14px;border: none;z-index: 10;font-weight: bold;}
  180. ::v-deep .el-table__row--striped{background: rgba(185, 223, 250, 0.05) !important}
  181. ::v-deep .el-table--striped .el-table__body tr.el-table__row--striped td.el-table__cell{background-color: transparent;}
  182. ::v-deep .el-table .current-row > td {
  183. background-color: transparent !important; /* 或者你想要的颜色 */
  184. }
  185. ::v-deep .el-table--enable-row-hover .el-table__body tr:hover{
  186. background-color: transparent !important; /* 或者你想要的颜色 */
  187. }
  188. ::v-deep .el-table--enable-row-hover .el-table__body tr:hover > td.el-table__cell{
  189. background-color: transparent !important; /* 或者你想要的颜色 */
  190. }
  191. ::v-deep .el-table::before{height: 0;}
  192. .scrolling-table-container {
  193. width: 100%;
  194. overflow: hidden;
  195. }
  196. ::v-deep .el-table__header{width: 100% !important;height: 100%;}
  197. ::v-deep .el-table__body{width: 100% !important;height: 100%;}
  198. ::v-deep .el-table .cell{padding: 0 2px !important;word-break: break-word;}
  199. .btn{min-width: 82px;display: inline-flex;align-items: center;justify-content: center;font-size: 14px;padding: 0 2px;box-sizing: border-box;height: 23px;color: #FFFFFF;border-radius: 12px;
  200. &.btna{background: #009944;}
  201. &.btnb{background: #D85600;}
  202. }
  203. .auto-scroll-container {
  204. position: relative;
  205. }
  206. .scroll-controls {
  207. margin-top: 10px;
  208. text-align: center;
  209. }
  210. /* 隐藏滚动条 gutter */
  211. ::v-deep .el-table .el-table__body-wrapper::-webkit-scrollbar {
  212. width: 0 !important;
  213. }
  214. ::v-deep .el-table th.gutter {
  215. display: none;
  216. }
  217. /* 确保表头和表体对齐 */
  218. ::v-deep .el-table colgroup col[name='gutter'] {
  219. width: 0 !important;
  220. }
  221. </style>