TechTable.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <div class="tech-table-wrapper" :style="height && height !== 'auto' ? { maxHeight: height } : {}">
  3. <table class="tech-table" cellspacing="0" cellpadding="0">
  4. <thead>
  5. <tr>
  6. <th v-for="(col, index) in columns" :key="index"
  7. :style="{ width: col.width, textAlign: col.align || 'center' }">
  8. {{ col.label }}
  9. </th>
  10. </tr>
  11. </thead>
  12. <tbody>
  13. <tr v-if="!data || data.length === 0">
  14. <td :colspan="columns.length" class="empty-text">暂无数据</td>
  15. </tr>
  16. <tr v-for="(row, rowIndex) in data" :key="rowIndex" class="table-row"
  17. @click="handleRowClick(row, rowIndex)">
  18. <td
  19. v-for="(col, colIndex) in columns"
  20. :key="colIndex"
  21. :style="{ textAlign: col.align || 'center' }"
  22. :title="row[col.key]"
  23. >
  24. <slot :name="col.key" :row="row" :index="rowIndex">
  25. {{ row[col.key] }}
  26. </slot>
  27. </td>
  28. </tr>
  29. </tbody>
  30. </table>
  31. </div>
  32. </template>
  33. <script>
  34. export default {
  35. name: 'TechTable',
  36. props: {
  37. // 表头配置:[{ label: '名称', key: 'name', width: '100px' }]
  38. columns: {
  39. type: Array,
  40. required: true
  41. },
  42. // 表格数据
  43. data: {
  44. type: Array,
  45. default: () => []
  46. },
  47. // 允许父组件传入高度来限制表格大小并开启滚动
  48. height: {
  49. type: String,
  50. default: 'auto' // 默认不限制高度
  51. }
  52. },
  53. methods: {
  54. // 处理行点击并向外派发事件
  55. handleRowClick(row, index) {
  56. this.$emit('row-click', { row, index });
  57. }
  58. }
  59. }
  60. </script>
  61. <style scoped>
  62. .tech-table-wrapper {
  63. flex: 1;
  64. height: 0;
  65. min-width: 0;
  66. width: 100%;
  67. overflow-y: auto;
  68. scrollbar-width: none; /* 兼容 Firefox 隐藏滚动条 */
  69. -ms-overflow-style: none; /* 兼容 IE/Edge 隐藏滚动条 */
  70. container-type: inline-size;
  71. }
  72. .tech-table-wrapper::-webkit-scrollbar {
  73. display: none; /* 兼容 Chrome/Safari/Edge 隐藏滚动条 */
  74. }
  75. .tech-table {
  76. width: 100%;
  77. border-collapse: collapse;
  78. table-layout: fixed;
  79. /* 让宽度分配更稳定 */
  80. }
  81. /* 如果你想让表格内部的滚动条也保持你 base.css 里的科技感,可以加上这个 */
  82. .tech-table-wrapper::-webkit-scrollbar {
  83. width: 1px; /* 表格滚动条可以稍微细一点 */
  84. }
  85. /* ================= 表头样式 ================= */
  86. .tech-table thead th {
  87. position: sticky;
  88. top: 0;
  89. z-index: 10;
  90. color: #6CFFD2;
  91. font-size: clamp(14px, 2.5cqw, 16px);
  92. font-weight: 600;
  93. padding: clamp(4px, 1.5cqw, 10px) clamp(4px, 1.5cqw, 10px);
  94. box-shadow: inset 0 -1px 0 rgba(255, 255, 255, 0.05);
  95. background-color: #112446;
  96. }
  97. /* ================= 表体样式 ================= */
  98. .tech-table tbody td {
  99. color: #ffffff;
  100. font-size: clamp(14px, 2.5cqw, 16px);
  101. padding: clamp(10px, 1.5cqw, 15px) clamp(10px, 1.5cqw, 15px);
  102. /* 文字超出省略号 */
  103. white-space: nowrap;
  104. overflow: hidden;
  105. text-overflow: ellipsis;
  106. }
  107. /* 斑马纹:奇数行和偶数行背景色区分 */
  108. .tech-table tbody tr:nth-child(odd) {
  109. background-color: #152C57;
  110. /* 偏亮的深蓝色 */
  111. }
  112. .tech-table tbody tr:nth-child(even) {
  113. background-color: transparent;
  114. /* 保持底色 */
  115. }
  116. /* 鼠标悬浮高亮反馈 */
  117. .tech-table tbody tr:hover {
  118. background-color: rgba(43, 220, 255, 0.1);
  119. }
  120. .empty-text {
  121. text-align: center;
  122. color: #64748b;
  123. padding: 30px !important;
  124. }
  125. /* 给数据行增加鼠标手型 */
  126. .tech-table tbody .table-row {
  127. cursor: pointer;
  128. transition: background-color 0.2s;
  129. /* 增加背景色过渡动画 */
  130. }
  131. /* 斑马纹和 hover 效果保持你原来的即可 */
  132. .tech-table tbody .table-row:nth-child(odd) {
  133. background-color: rgba(30, 50, 90, 0.4);
  134. }
  135. .tech-table tbody .table-row:hover {
  136. background-color: rgba(43, 220, 255, 0.15);
  137. /* 稍微调亮一点 hover 颜色反馈更好 */
  138. }
  139. </style>