table2.vue 6.3 KB

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