check-table.vue 6.2 KB

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