check-table.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <table class="com-table">
  3. <thead>
  4. <tr>
  5. <th @click="onSort(0)">排序</th>
  6. <th @click="onSort(1)"></th>
  7. <th v-for="(col, index) of data.column" :key="index" :class="{ light: col.is_light }" @click="onSort(col)">{{ col.name }}</th>
  8. </tr>
  9. </thead>
  10. <el-scrollbar>
  11. <tbody :style="{ height: height }">
  12. <tr v-for="(row, index) of tableData" :key="index">
  13. <td :class="{ light: hoverRow == row || hoverCol == 0, num: true }" @mouseenter="hover(row, col)" @mouseleave="leave()">
  14. {{ index + 1 }}
  15. </td>
  16. <td :class="{ light: hoverRow == row || hoverCol == 1 }" @mouseenter="hover(row, col)" @mouseleave="leave()">
  17. <input class="check" type="CheckBox" :key="index" :id="'check-' + index" :checked="row.checked ? row.checked : false" @change="onCheck(row)" />
  18. </td>
  19. <td v-for="(col, i) of data.column" :key="i" :class="{ light: hoverRow == row || hoverCol == col + 2, num: col.is_num, 'always-light': col.is_light || row.is_light }" @mouseenter="hover(row, col)" @mouseleave="leave()">
  20. <component :is="col.type ? col.type : 'div'" v-bind="col.props" v-html="template(col, row[col.field])" @click="onClick(col, row)"> </component>
  21. </td>
  22. </tr>
  23. </tbody>
  24. <el-pagination class="mg-t-8" v-if="pageable" @current-change="handleCurrentChange" :current-page="currentPage4" :page-size="pageSize" layout="total, prev, pager, next, jumper" :total="data.total"> </el-pagination>
  25. </el-scrollbar>
  26. </table>
  27. </template>
  28. <script>
  29. export default {
  30. // 名称
  31. name: "ComTable",
  32. // 使用组件
  33. components: {},
  34. // 传入参数
  35. props: {
  36. /**
  37. * {
  38. column: [{
  39. name: "风机名称",
  40. field: "name",
  41. type:'div',
  42. is_num: false, // 是否为数字
  43. is_light: false, // 是否高亮
  44. template:function(){ }
  45. click:function(){} //点击事件
  46. sortable:fasle // 排序
  47. // 新增用于在表格中使用动态三方组件
  48. type:'el-tag', // * 新增 用于传入三方组件名称 实现三方组件引入 component :is 方式实现
  49. props:{}, // * 新增 用户传入三方组件的 props 与type同时使用
  50. },{
  51. name: "冷却风温度",
  52. field: "lqf",
  53. is_num: true,
  54. is_light: false
  55. }],
  56. data: [{
  57. name: "1E01",
  58. lqf: 15.78,
  59. is_light: false
  60. }]
  61. }
  62. */
  63. data: Object,
  64. // hover 样式
  65. showHover: {
  66. type: Boolean,
  67. default: true,
  68. },
  69. canScroll: {
  70. type: Boolean,
  71. default: true,
  72. },
  73. pageSize: {
  74. type: Number,
  75. default: 0,
  76. },
  77. height: {
  78. type: String,
  79. default: "",
  80. },
  81. },
  82. // 自定义事件
  83. emits: {
  84. // 分页事件
  85. onPagging: null,
  86. check: null,
  87. },
  88. // 数据
  89. data() {
  90. return {
  91. hoverRow: -1,
  92. hoverCol: -1,
  93. sortCol: "",
  94. sortType: "",
  95. currentPage: 1,
  96. };
  97. },
  98. computed: {
  99. tableData() {
  100. let that = this;
  101. if (this.sortCol == "") {
  102. return this.data.data;
  103. } else {
  104. let data = this.data.data;
  105. data.sort((a, b) => {
  106. let rev = 1;
  107. if (that.sortType == "ASC") rev = 1;
  108. else if (that.sortType == "DESC") rev = -1;
  109. if (a[that.sortCol] > b[that.sortCol]) return rev * 1;
  110. if (a[that.sortCol] < b[that.sortCol]) return rev * -1;
  111. return 0;
  112. });
  113. return data;
  114. }
  115. },
  116. pageable() {
  117. return this.pageSize != 0;
  118. },
  119. pages() {
  120. if (this.pageable) return parseInt(this.data.total / this.pageSize) + 1;
  121. else return 0;
  122. },
  123. startRow() {
  124. if (this.pageable) return (this.currentPage - 1) * this.pageSize;
  125. else return 0;
  126. },
  127. endRow() {
  128. if (this.pageable) return this.currentPage * this.pageSize;
  129. else return this.data.data.length;
  130. },
  131. },
  132. // 函数
  133. methods: {
  134. onClick(col, data) {
  135. if (col.click) col.click(event, data);
  136. },
  137. onCheck(data) {
  138. data.checked = event.target.checked;
  139. this.$emit("check", { data: data, checked: data.checked });
  140. },
  141. onSort(col) {
  142. if (col.sortable == true) {
  143. this.sortCol = col.field;
  144. switch (this.sortType) {
  145. case "":
  146. this.sortType = "DESC";
  147. break;
  148. case "DESC":
  149. this.sortType = "ASC";
  150. break;
  151. case "ASC":
  152. this.sortType = "";
  153. break;
  154. }
  155. }
  156. },
  157. template(col, data) {
  158. if (!col.template) return data;
  159. else return col.template(data);
  160. },
  161. hover(row, col) {
  162. if (this.showHover) {
  163. this.hoverRow = row;
  164. this.hoverCol = col;
  165. }
  166. },
  167. leave() {
  168. this.hoverRow = -1;
  169. this.hoverCol = -1;
  170. },
  171. handleCurrentChange(val) {
  172. this.currentPage = val;
  173. this.$emit("onPagging", { pageIndex: this.currentPage, pageSize: this.pageSize, start: this.startRow, end: this.endRow });
  174. },
  175. },
  176. // 生命周期钩子
  177. beforeCreate() {
  178. // 创建前
  179. },
  180. created() {
  181. // 创建后
  182. },
  183. beforeMount() {
  184. // 渲染前
  185. },
  186. mounted() {
  187. // 渲染后
  188. },
  189. beforeUpdate() {
  190. // 数据更新前
  191. },
  192. updated() {
  193. // 数据更新后
  194. },
  195. };
  196. </script>
  197. <style lang="less">
  198. @titleGray: #9ca5a8;
  199. @rowGray: #606769;
  200. @darkBack: #536268;
  201. .com-table {
  202. width: 100%;
  203. border-collapse: collapse;
  204. thead {
  205. tr {
  206. display: table;
  207. table-layout: fixed;
  208. width: 100%;
  209. th {
  210. background-color: fade(@darkBack, 20%);
  211. padding: 1.481vh 0;
  212. color: @titleGray;
  213. font-weight: 400;
  214. font-size: @fontsize-s;
  215. position: sticky;
  216. top: 0;
  217. cursor: pointer;
  218. &.light,
  219. &.always-light {
  220. color: @green;
  221. }
  222. }
  223. }
  224. }
  225. tbody {
  226. display: block;
  227. tr {
  228. display: table;
  229. table-layout: fixed;
  230. width: 100%;
  231. &:nth-child(2n) {
  232. background-color: fade(@rowGray, 20%);
  233. }
  234. td {
  235. padding: 0.556vh 0;
  236. color: @rowGray;
  237. text-align: center;
  238. font-size: @fontsize-s;
  239. &.light,
  240. &.always-light {
  241. color: @green !important;
  242. }
  243. &.num {
  244. font-family: "Bicubik";
  245. font-weight: 400;
  246. }
  247. }
  248. }
  249. }
  250. }
  251. </style>