group-table.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <template>
  2. <el-table class="custom-table" :class="customClass" stripe :data="data.data" :height="height" style="width: 100%;cursor: pointer;" @cell-click="onClick" @header-click="onHeaderClick">
  3. <template v-for="col in data.column" :key="col">
  4. <el-table-column v-if="col.child && col.child.length > 0" :label="col.name" :key="col">
  5. <el-table-column
  6. v-for="sub in col.child"
  7. :key="sub"
  8. :label="sub.name"
  9. :prop="sub.field"
  10. :width="sub.width"
  11. :sortable="sub.sortable"
  12. :show-overflow-tooltip="!sub.slot"
  13. :fixed="sub.fixed"
  14. :align="sub.align ? sub.align : 'center'"
  15. :resizable="sub.resizable"
  16. :header-align="'center'"
  17. >
  18. <template v-if="sub.slot == true" #default="item">
  19. <slot :name="sub.field" :column="sub" :row="item.row" :all="item" :data="item.row[item.field]"></slot>
  20. </template>
  21. </el-table-column>
  22. </el-table-column>
  23. <el-table-column
  24. v-if="!col.child"
  25. :label="col.name"
  26. :prop="col.field"
  27. :width="col.width"
  28. :sortable="col.sortable"
  29. :show-overflow-tooltip="!col.slot"
  30. :fixed="col.fixed"
  31. :align="col.align ? col.align : 'center'"
  32. :resizable="col.resizable"
  33. :header-align="'center'"
  34. >
  35. <template v-if="col.slot == true" #default="item">
  36. <slot :name="col.field" :column="col" :row="item.row" :all="item" :data="item.row[col.field]"></slot>
  37. </template>
  38. </el-table-column>
  39. </template>
  40. </el-table>
  41. <el-pagination class="mg-t-8" v-if="pageable" @current-change="handleCurrentChange" :current-page="currentPage" :page-size="pageSize" :total="data.total" v-bind="elPaggingProps"> </el-pagination>
  42. </template>
  43. <script>
  44. export default {
  45. // 名称
  46. name: "ComTable",
  47. // 使用组件
  48. components: {},
  49. // 传入参数
  50. props: {
  51. /**
  52. * {
  53. column: [{
  54. name: "风机名称",
  55. child:[{
  56. field: "name",
  57. width:'', // 宽度
  58. click:function(){} // 点击事件
  59. sortable:fasle,
  60. slot:false,
  61. fixed:false,
  62. align:'center',
  63. resizable :false,
  64. }]
  65. }],
  66. total:200
  67. }
  68. */
  69. data: Object,
  70. height: {
  71. type: String,
  72. default: "",
  73. },
  74. pageSize: {
  75. type: Number,
  76. default: 0,
  77. },
  78. customClass: {
  79. type: String,
  80. default: "",
  81. },
  82. elPaggingProps: {
  83. type: Object,
  84. default: () => {
  85. return {
  86. layout: "total, sizes, prev, pager, next, jumper",
  87. // "page-sizes": [100, 200, 300, 400],
  88. };
  89. },
  90. },
  91. },
  92. emits: {
  93. onPagging: null,
  94. headerClick: null,
  95. },
  96. // 数据
  97. data() {
  98. return {
  99. currentPage: 1,
  100. };
  101. },
  102. computed: {
  103. tableData() {
  104. let that = this;
  105. if (this.sortCol == "") {
  106. return this.data.data;
  107. } else {
  108. let data = this.data.data;
  109. data.sort((a, b) => {
  110. let rev = 1;
  111. if (that.sortType == "ASC") rev = 1;
  112. else if (that.sortType == "DESC") rev = -1;
  113. if (a[that.sortCol] > b[that.sortCol]) return rev * 1;
  114. if (a[that.sortCol] < b[that.sortCol]) return rev * -1;
  115. return 0;
  116. });
  117. return data;
  118. }
  119. },
  120. pageable() {
  121. return this.pageSize != 0;
  122. },
  123. pages() {
  124. if (this.pageable) return parseInt(this.data.total / this.pageSize) + 1;
  125. else return 0;
  126. },
  127. startRow() {
  128. if (this.pageable) return (this.currentPage - 1) * this.pageSize;
  129. else return 0;
  130. },
  131. endRow() {
  132. if (this.pageable) return this.currentPage * this.pageSize;
  133. else return this.data.data.length;
  134. },
  135. },
  136. // 函数
  137. methods: {
  138. onClick(row, column, cell, event) {
  139. if (column.rawColumnKey.click) column.rawColumnKey.click(event, row);
  140. },
  141. onHeaderClick(column, event) {
  142. this.$emit("headerClick", { event: event, col: column.rawColumnKey, data: this.data.data });
  143. },
  144. handleCurrentChange(val) {
  145. this.currentPage = val;
  146. this.$emit("onPagging", {
  147. pageIndex: this.currentPage,
  148. pageSize: this.pageSize,
  149. start: this.startRow,
  150. end: this.endRow,
  151. });
  152. },
  153. },
  154. // 生命周期钩子
  155. beforeCreate() {
  156. // 创建前
  157. },
  158. created() {
  159. // 创建后
  160. },
  161. beforeMount() {
  162. // 渲染前
  163. },
  164. mounted() {
  165. // 渲染后
  166. },
  167. beforeUpdate() {},
  168. updated() {},
  169. };
  170. </script>
  171. <style lang="less">
  172. @titleGray: #9ca5a8;
  173. @rowGray: #606769;
  174. @darkBack: #536268;
  175. .com-table {
  176. width: 100%;
  177. border-collapse: collapse;
  178. thead {
  179. tr {
  180. display: table;
  181. table-layout: fixed;
  182. width: 100%;
  183. th {
  184. background-color: fade(@darkBack, 20%);
  185. height: 30px;
  186. line-height: 30px;
  187. color: @titleGray;
  188. font-weight: 400;
  189. font-size: @fontsize-s;
  190. position: sticky;
  191. top: 0;
  192. cursor: pointer;
  193. &.light,
  194. &.always-light {
  195. color: @green;
  196. }
  197. }
  198. }
  199. }
  200. tbody {
  201. display: block;
  202. tr {
  203. display: table;
  204. table-layout: fixed;
  205. width: 100%;
  206. &:nth-child(2n) {
  207. background-color: fade(@rowGray, 20%);
  208. }
  209. td {
  210. padding: 0.556vh 0;
  211. color: @rowGray;
  212. text-align: center;
  213. font-size: @fontsize-s;
  214. white-space: nowrap;
  215. overflow: hidden;
  216. text-overflow: ellipsis;
  217. &.light,
  218. &.always-light {
  219. color: @green !important;
  220. }
  221. &.num {
  222. font-family: "Bicubik";
  223. font-weight: 400;
  224. }
  225. }
  226. }
  227. }
  228. .el-pagination {
  229. color: @gray;
  230. .el-pagination__total {
  231. color: @gray;
  232. }
  233. button {
  234. &.btn-next,
  235. &.btn-prev {
  236. background: center center no-repeat fade(@gray, 20);
  237. color: @gray-l;
  238. }
  239. &:disabled {
  240. color: @gray-l;
  241. background-color: fade(@gray, 20);
  242. cursor: not-allowed;
  243. }
  244. }
  245. .el-pager li {
  246. color: @gray-l;
  247. background: fade(@gray, 20);
  248. &.active {
  249. color: @green;
  250. }
  251. }
  252. .el-input__inner {
  253. color: @gray-l;
  254. background: fade(@gray, 20);
  255. border: 1px solid fade(@gray, 20);
  256. }
  257. }
  258. }
  259. </style>