table.vue 6.1 KB

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