check-table.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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"> </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. },
  93. // 自定义事件
  94. emits: {
  95. // 分页事件
  96. onPagging: null,
  97. check: null,
  98. },
  99. // 数据
  100. data() {
  101. return {
  102. hoverRow: -1,
  103. hoverCol: -1,
  104. sortCol: "",
  105. sortType: "",
  106. currentPage: 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. },
  143. // 函数
  144. methods: {
  145. onClick(col, data) {
  146. if (col.click) col.click(event, data);
  147. },
  148. onCheck(data) {
  149. data.checked = event.target.checked;
  150. this.$emit("check", { data: data, checked: data.checked });
  151. },
  152. onSort(col) {
  153. if (col.sortable == true) {
  154. this.sortCol = col.field;
  155. switch (this.sortType) {
  156. case "":
  157. this.sortType = "DESC";
  158. break;
  159. case "DESC":
  160. this.sortType = "ASC";
  161. break;
  162. case "ASC":
  163. this.sortType = "";
  164. break;
  165. }
  166. }
  167. },
  168. template(col, data) {
  169. if (!col.template) return data;
  170. else return col.template(data);
  171. },
  172. hover(row, col) {
  173. if (this.showHover) {
  174. this.hoverRow = row;
  175. this.hoverCol = col;
  176. }
  177. },
  178. leave() {
  179. this.hoverRow = -1;
  180. this.hoverCol = -1;
  181. },
  182. handleCurrentChange(val) {
  183. this.currentPage = val;
  184. this.$emit("onPagging", { pageIndex: this.currentPage, pageSize: this.pageSize, start: this.startRow, end: this.endRow });
  185. },
  186. },
  187. // 生命周期钩子
  188. beforeCreate() {
  189. // 创建前
  190. },
  191. created() {
  192. // 创建后
  193. },
  194. beforeMount() {
  195. // 渲染前
  196. },
  197. mounted() {
  198. // 渲染后
  199. },
  200. beforeUpdate() {
  201. // 数据更新前
  202. },
  203. updated() {
  204. // 数据更新后
  205. },
  206. };
  207. </script>
  208. <style lang="less"></style>