table.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <table class="com-table">
  3. <thead>
  4. <tr>
  5. <th v-for="(col, index) of data.column" :key="index" :class="{ light: col.is_light }">{{ col.name }}</th>
  6. </tr>
  7. </thead>
  8. <tbody :class="{ scroll: canScroll }">
  9. <tr v-for="(row, index) of data.data" :key="index">
  10. <td
  11. v-for="(col, i) of data.column"
  12. :key="i"
  13. :class="{
  14. light: hoverRow == row || hoverCol == col,
  15. num: col.is_num,
  16. 'always-light': col.is_light || row.is_light,
  17. }"
  18. v-html="template(col, row[col.field])"
  19. @mouseenter="hover(row, col)"
  20. @mouseleave="leave()"
  21. ></td>
  22. </tr>
  23. </tbody>
  24. </table>
  25. </template>
  26. <script>
  27. export default {
  28. // 名称
  29. name: "ComTable",
  30. // 使用组件
  31. components: {},
  32. // 传入参数
  33. props: {
  34. /**
  35. * {
  36. column: [{
  37. name: "风机名称",
  38. field: "name",
  39. is_num: false, // 是否为数字
  40. is_light: false, // 是否高亮
  41. template:function(){ }
  42. },{
  43. name: "冷却风温度",
  44. field: "lqf",
  45. is_num: true,
  46. is_light: false
  47. }],
  48. data: [{
  49. name: "1E01",
  50. lqf: 15.78,
  51. is_light: false
  52. }]
  53. }
  54. */
  55. data: Object,
  56. // hover 样式
  57. showHover: {
  58. type: Boolean,
  59. default: true,
  60. },
  61. canScroll: {
  62. type: Boolean,
  63. default: true,
  64. },
  65. },
  66. // 自定义事件
  67. emits: {},
  68. // 数据
  69. data() {
  70. return {
  71. hoverRow: -1,
  72. hoverCol: -1,
  73. };
  74. },
  75. // 函数
  76. methods: {
  77. template(col, data) {
  78. if (!col.template) return data;
  79. else return col.template(data);
  80. },
  81. hover(row, col) {
  82. if (this.showHover) {
  83. this.hoverRow = row;
  84. this.hoverCol = col;
  85. }
  86. },
  87. leave() {
  88. this.hoverRow = -1;
  89. this.hoverCol = -1;
  90. },
  91. },
  92. // 生命周期钩子
  93. beforeCreate() {
  94. // 创建前
  95. },
  96. created() {
  97. // 创建后
  98. },
  99. beforeMount() {
  100. // 渲染前
  101. },
  102. mounted() {
  103. // 渲染后
  104. },
  105. beforeUpdate() {
  106. // 数据更新前
  107. },
  108. updated() {
  109. // 数据更新后
  110. },
  111. };
  112. </script>
  113. <style lang="less">
  114. @titleGray: #9ca5a8;
  115. @rowGray: #606769;
  116. @darkBack: #536268;
  117. .com-table {
  118. width: 100%;
  119. border-collapse: collapse;
  120. thead {
  121. tr {
  122. display: table;
  123. table-layout: fixed;
  124. width: 100%;
  125. th {
  126. background-color: fade(@darkBack, 20%);
  127. padding: 1.481vh 0;
  128. color: @titleGray;
  129. font-weight: 400;
  130. font-size: @fontsize-s;
  131. position: sticky;
  132. top: 0;
  133. &.light,
  134. &.always-light {
  135. color: @green;
  136. }
  137. }
  138. }
  139. }
  140. tbody {
  141. display: block;
  142. tr {
  143. display: table;
  144. table-layout: fixed;
  145. width: 100%;
  146. &:nth-child(2n) {
  147. background-color: fade(@rowGray, 20%);
  148. }
  149. td {
  150. padding: 0.556vh 0;
  151. color: @rowGray;
  152. text-align: center;
  153. font-size: @fontsize-s;
  154. &.light,
  155. &.always-light {
  156. color: @green !important;
  157. }
  158. &.num {
  159. font-family: "Bicubik";
  160. font-weight: 400;
  161. }
  162. }
  163. }
  164. }
  165. }
  166. </style>