table2.vue 6.2 KB

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