table.vue 6.9 KB

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