table.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 }" @click="onSort(col)">{{ col.name }}</th>
  6. </tr>
  7. </thead>
  8. <el-scrollbar>
  9. <tbody>
  10. <tr v-for="(row, index) of tableData" :key="index">
  11. <td
  12. v-for="(col, i) of data.column"
  13. :key="i"
  14. :class="{
  15. light: hoverRow == row || hoverCol == col,
  16. num: col.is_num,
  17. 'always-light': col.is_light || row.is_light,
  18. }"
  19. v-html="template(col, row[col.field])"
  20. @click="onClick(col, row)"
  21. @mouseenter="hover(row, col)"
  22. @mouseleave="leave()"
  23. ></td>
  24. </tr>
  25. </tbody>
  26. </el-scrollbar>
  27. </table>
  28. </template>
  29. <script>
  30. export default {
  31. // 名称
  32. name: "ComTable",
  33. // 使用组件
  34. components: {},
  35. // 传入参数
  36. props: {
  37. /**
  38. * {
  39. column: [{
  40. name: "风机名称",
  41. field: "name",
  42. is_num: false, // 是否为数字
  43. is_light: false, // 是否高亮
  44. template:function(){ }
  45. click:function(){} //点击事件
  46. sortable:fasle // 排序
  47. },{
  48. name: "冷却风温度",
  49. field: "lqf",
  50. is_num: true,
  51. is_light: false
  52. }],
  53. data: [{
  54. name: "1E01",
  55. lqf: 15.78,
  56. is_light: false
  57. }]
  58. }
  59. */
  60. data: Object,
  61. // hover 样式
  62. showHover: {
  63. type: Boolean,
  64. default: true,
  65. },
  66. canScroll: {
  67. type: Boolean,
  68. default: true,
  69. },
  70. },
  71. // 自定义事件
  72. emits: {},
  73. // 数据
  74. data() {
  75. return {
  76. hoverRow: -1,
  77. hoverCol: -1,
  78. sortCol: "",
  79. sortType: "",
  80. };
  81. },
  82. computed: {
  83. tableData() {
  84. let that = this;
  85. if (this.sortCol == "") {
  86. return this.data.data;
  87. } else {
  88. let data = JSON.parse(JSON.stringify(this.data.data));
  89. data.sort((a, b) => {
  90. let rev = 1;
  91. if (that.sortType == "ASC") rev = 1;
  92. else if (that.sortType == "DESC") rev = -1;
  93. if (a[that.sortCol] > b[that.sortCol]) return rev * 1;
  94. if (a[that.sortCol] < b[that.sortCol]) return rev * -1;
  95. return 0;
  96. });
  97. return data;
  98. }
  99. },
  100. },
  101. // 函数
  102. methods: {
  103. onClick(col, data) {
  104. if (col.click) col.click(event, data);
  105. },
  106. onSort(col) {
  107. console.log(col)
  108. if (col.sortable == true) {
  109. this.sortCol = col.field;
  110. switch (this.sortType) {
  111. case "":
  112. this.sortType = "DESC";
  113. break;
  114. case "DESC":
  115. this.sortType = "ASC";
  116. break;
  117. case "ASC":
  118. this.sortType = "";
  119. break;
  120. }
  121. }
  122. },
  123. template(col, data) {
  124. if (!col.template) return data;
  125. else return col.template(data);
  126. },
  127. hover(row, col) {
  128. if (this.showHover) {
  129. this.hoverRow = row;
  130. this.hoverCol = col;
  131. }
  132. },
  133. leave() {
  134. this.hoverRow = -1;
  135. this.hoverCol = -1;
  136. },
  137. },
  138. // 生命周期钩子
  139. beforeCreate() {
  140. // 创建前
  141. },
  142. created() {
  143. // 创建后
  144. },
  145. beforeMount() {
  146. // 渲染前
  147. },
  148. mounted() {
  149. // 渲染后
  150. },
  151. beforeUpdate() {
  152. // 数据更新前
  153. },
  154. updated() {
  155. // 数据更新后
  156. },
  157. };
  158. </script>
  159. <style lang="less">
  160. @titleGray: #9ca5a8;
  161. @rowGray: #606769;
  162. @darkBack: #536268;
  163. .com-table {
  164. width: 100%;
  165. border-collapse: collapse;
  166. thead {
  167. tr {
  168. display: table;
  169. table-layout: fixed;
  170. width: 100%;
  171. th {
  172. background-color: fade(@darkBack, 20%);
  173. padding: 1.481vh 0;
  174. color: @titleGray;
  175. font-weight: 400;
  176. font-size: @fontsize-s;
  177. position: sticky;
  178. top: 0;
  179. cursor: pointer;
  180. &.light,
  181. &.always-light {
  182. color: @green;
  183. }
  184. }
  185. }
  186. }
  187. tbody {
  188. display: block;
  189. tr {
  190. display: table;
  191. table-layout: fixed;
  192. width: 100%;
  193. &:nth-child(2n) {
  194. background-color: fade(@rowGray, 20%);
  195. }
  196. td {
  197. padding: 0.556vh 0;
  198. color: @rowGray;
  199. text-align: center;
  200. font-size: @fontsize-s;
  201. &.light,
  202. &.always-light {
  203. color: @green !important;
  204. }
  205. &.num {
  206. font-family: "Bicubik";
  207. font-weight: 400;
  208. }
  209. }
  210. }
  211. }
  212. }
  213. </style>