table.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 }" :style="{ width: col.width }" @click="onSort(col)">
  6. {{ col.name }}
  7. </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
  14. v-for="(col, i) of data.column"
  15. :key="i"
  16. :style="{ width: col.width }"
  17. :class="{ light: hoverRow == row || hoverCol == col, num: col.is_num, 'always-light': col.is_light || row.is_light }"
  18. @mouseenter="hover(row, col)"
  19. @mouseleave="leave()"
  20. >
  21. <component :is="col.type ? col.type : 'div'" v-bind="col.props" v-html="template(col, row[col.field])" @click="onClick(col, row)"> </component>
  22. </td>
  23. </tr>
  24. </tbody>
  25. </el-scrollbar>
  26. <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>
  27. </table>
  28. </template>
  29. <script>
  30. export default {
  31. // 名称
  32. name: "ComTable",
  33. // 使用组件
  34. components: {},
  35. // 传入参数
  36. props: {
  37. /**
  38. * {
  39. column: [{
  40. name: "风机名称",
  41. field: "name",
  42. type:'div',
  43. width:'', // 宽度
  44. is_num: false, // 是否为数字
  45. is_light: false, // 是否高亮
  46. template:function(){ }
  47. click:function(){} //点击事件
  48. sortable:fasle // 排序
  49. // 新增用于在表格中使用动态三方组件
  50. type:'el-tag', // * 新增 用于传入三方组件名称 实现三方组件引入 component :is 方式实现
  51. props:{}, // * 新增 用户传入三方组件的 props 与type同时使用
  52. },{
  53. name: "冷却风温度",
  54. field: "lqf",
  55. is_num: true,
  56. is_light: false
  57. }],
  58. data: [{
  59. name: "1E01",
  60. lqf: 15.78,
  61. is_light: false
  62. }],
  63. total:200
  64. }
  65. */
  66. data: Object,
  67. // hover 样式
  68. showHover: {
  69. type: Boolean,
  70. default: true,
  71. },
  72. // 列高亮
  73. showColHover: {
  74. type: Boolean,
  75. default: false,
  76. },
  77. canScroll: {
  78. type: Boolean,
  79. default: true,
  80. },
  81. pageSize: {
  82. type: Number,
  83. default: 0,
  84. },
  85. height: {
  86. type: String,
  87. default: "",
  88. },
  89. },
  90. // 自定义事件
  91. emits: {
  92. // 分页事件
  93. onPagging: null,
  94. },
  95. // 数据
  96. data() {
  97. return {
  98. hoverRow: -1,
  99. hoverCol: -1,
  100. sortCol: "",
  101. sortType: "",
  102. currentPage: 1,
  103. };
  104. },
  105. computed: {
  106. tableData() {
  107. let that = this;
  108. if (this.sortCol == "") {
  109. return this.data.data;
  110. } else {
  111. let data = this.data.data;
  112. data.sort((a, b) => {
  113. let rev = 1;
  114. if (that.sortType == "ASC") rev = 1;
  115. else if (that.sortType == "DESC") rev = -1;
  116. if (a[that.sortCol] > b[that.sortCol]) return rev * 1;
  117. if (a[that.sortCol] < b[that.sortCol]) return rev * -1;
  118. return 0;
  119. });
  120. return data;
  121. }
  122. },
  123. pageable() {
  124. return this.pageSize != 0;
  125. },
  126. pages() {
  127. if (this.pageable) return parseInt(this.data.total / this.pageSize) + 1;
  128. else return 0;
  129. },
  130. startRow() {
  131. if (this.pageable) return (this.currentPage - 1) * this.pageSize;
  132. else return 0;
  133. },
  134. endRow() {
  135. if (this.pageable) return this.currentPage * this.pageSize;
  136. else return this.data.data.length;
  137. },
  138. },
  139. // 函数
  140. methods: {
  141. clearCheckBox(time){
  142. this.$nextTick(()=>{
  143. setTimeout(()=>{
  144. const domArray = document.querySelectorAll(".curCheckBox");
  145. for(let i=0;i<domArray.length;i++){
  146. domArray[i].checked=false;
  147. }
  148. },(time || 300));
  149. });
  150. },
  151. onClick(col, data) {
  152. if (col.click) col.click(event, data);
  153. },
  154. onSort(col) {
  155. if (col.sortable == true) {
  156. this.sortCol = col.field;
  157. switch (this.sortType) {
  158. case "":
  159. this.sortType = "DESC";
  160. break;
  161. case "DESC":
  162. this.sortType = "ASC";
  163. break;
  164. case "ASC":
  165. this.sortType = "";
  166. break;
  167. }
  168. }
  169. },
  170. template(col, data) {
  171. if (!col.template) return data;
  172. else return col.template(data);
  173. },
  174. hover(row, col) {
  175. if (this.showHover) {
  176. this.hoverRow = row;
  177. if (this.showColHover) this.hoverCol = col;
  178. }
  179. },
  180. leave() {
  181. this.hoverRow = -1;
  182. this.hoverCol = -1;
  183. },
  184. handleCurrentChange(val) {
  185. this.currentPage = val;
  186. this.$emit("onPagging", {
  187. pageIndex: this.currentPage,
  188. pageSize: this.pageSize,
  189. start: this.startRow,
  190. end: this.endRow,
  191. });
  192. },
  193. },
  194. // 生命周期钩子
  195. beforeCreate() {
  196. // 创建前
  197. },
  198. created() {
  199. // 创建后
  200. },
  201. beforeMount() {
  202. // 渲染前
  203. },
  204. mounted() {
  205. // 渲染后
  206. },
  207. beforeUpdate() {},
  208. updated() {},
  209. };
  210. </script>
  211. <style lang="less">
  212. </style>