table.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 : 'span'" 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="currentPage" :page-size="pageSize" :total="data.total" v-bind="elPaggingProps"> </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. // 新增 支持 pagging 组件
  90. // elPaggingProps: {
  91. // type: Object,
  92. // default: () => {
  93. // return {
  94. // layout: "total, sizes, prev, pager, next, jumper",
  95. // // "page-sizes": [100, 200, 300, 400],
  96. // };
  97. // },
  98. // },
  99. },
  100. // 自定义事件
  101. emits: {
  102. // 分页事件
  103. // onPagging: null,
  104. },
  105. // 数据
  106. data() {
  107. return {
  108. hoverRow: -1,
  109. hoverCol: -1,
  110. sortCol: "",
  111. sortType: "",
  112. currentPage: 1,
  113. };
  114. },
  115. computed: {
  116. tableData() {
  117. let that = this;
  118. if (this.sortCol == "") {
  119. return this.data.data;
  120. } else {
  121. let data = this.data.data;
  122. data.sort((a, b) => {
  123. let rev = 1;
  124. if (that.sortType == "ASC") rev = 1;
  125. else if (that.sortType == "DESC") rev = -1;
  126. if (a[that.sortCol] > b[that.sortCol]) return rev * 1;
  127. if (a[that.sortCol] < b[that.sortCol]) return rev * -1;
  128. return 0;
  129. });
  130. return data;
  131. }
  132. },
  133. pageable() {
  134. return this.pageSize != 0;
  135. },
  136. pages() {
  137. if (this.pageable) return parseInt(this.data.total / this.pageSize) + 1;
  138. else return 0;
  139. },
  140. startRow() {
  141. if (this.pageable) return (this.currentPage - 1) * this.pageSize;
  142. else return 0;
  143. },
  144. endRow() {
  145. if (this.pageable) return this.currentPage * this.pageSize;
  146. else return this.data.data.length;
  147. },
  148. },
  149. // 函数
  150. methods: {
  151. clearCheckBox(time){
  152. this.$nextTick(()=>{
  153. setTimeout(()=>{
  154. const domArray = document.querySelectorAll(".curCheckBox");
  155. for(let i=0;i<domArray.length;i++){
  156. domArray[i].checked=false;
  157. }
  158. },(time || 300));
  159. });
  160. },
  161. onClick(col, data) {
  162. if (col.click) col.click(event, data);
  163. },
  164. onSort(col) {
  165. if (col.sortable == true) {
  166. this.sortCol = col.field;
  167. switch (this.sortType) {
  168. case "":
  169. this.sortType = "DESC";
  170. break;
  171. case "DESC":
  172. this.sortType = "ASC";
  173. break;
  174. case "ASC":
  175. this.sortType = "";
  176. break;
  177. }
  178. }
  179. },
  180. template(col, data) {
  181. if (!col.template) return data;
  182. else return col.template(data);
  183. },
  184. hover(row, col) {
  185. if (this.showHover) {
  186. this.hoverRow = row;
  187. if (this.showColHover) this.hoverCol = col;
  188. }
  189. },
  190. leave() {
  191. this.hoverRow = -1;
  192. this.hoverCol = -1;
  193. },
  194. handleCurrentChange(val) {
  195. this.currentPage = val;
  196. this.$emit("onPagging", {
  197. pageIndex: this.currentPage,
  198. pageSize: this.pageSize,
  199. start: this.startRow,
  200. end: this.endRow,
  201. });
  202. },
  203. },
  204. // 生命周期钩子
  205. beforeCreate() {
  206. // 创建前
  207. },
  208. created() {
  209. // 创建后
  210. },
  211. beforeMount() {
  212. // 渲染前
  213. },
  214. mounted() {
  215. // 渲染后
  216. console.log('tabthis:',this)
  217. },
  218. beforeUpdate() {},
  219. updated() {},
  220. };
  221. </script>
  222. <style lang="less"></style>