CusTable.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <div class="cus-table">
  3. <div class="cus-table-table">
  4. <el-table
  5. ref="ref_table"
  6. v-bind="$attrs"
  7. :data="data"
  8. :row-key="rowKey"
  9. @sort-change="handleSort"
  10. @filter-change="handleFilter"
  11. @selection-change="v => $emit('update:selected', v)"
  12. max-height="100%"
  13. >
  14. <el-table-column v-if="selected" type="selection" :width="selectWidth" align="center" :selectable="selectable"/>
  15. <el-table-column v-if="singled" :width="singledWidth" align="center" class-name="single-column" fixed="left">
  16. <template #default="scope">
  17. <div class="single-circle" :class="{active: scope.row[singledKey] === singled[singledKey]}" @click.stop="$emit('update:singled', scope.row)">
  18. <div class="single-circle-in"></div>
  19. </div>
  20. </template>
  21. </el-table-column>
  22. <el-table-column v-if="showIndex" type="index" label="序号" align="center" fixed="left" width="60"/>
  23. <!-- 一级-->
  24. <template v-for="(one, oneI) in tableHead" :key="oneI">
  25. <template v-if="one.children?.length > 0">
  26. <el-table-column :label="one.label" align="center">
  27. <!-- 二级-->
  28. <template v-for="(two, twoI) in one.children" :key="twoI">
  29. <template v-if="two.children?.length > 0">
  30. <el-table-column :label="two.label" align="center">
  31. <!-- 三级-->
  32. <template v-for="(three, threeI) in two.children" :key="threeI">
  33. <template v-if="three.children?.length > 0">
  34. <el-table-column :label="three.label" align="center">
  35. <!-- 四级-->
  36. <template v-for="(four, fourI) in three.children" :key="fourI">
  37. <template v-if="four.children?.length > 0">
  38. <el-table-column :label="four.label" align="center">
  39. <!-- 五级-->
  40. <template v-for="(five, fiveI) in four.children" :key="fiveI">
  41. <CusTableColumn :item="five">
  42. <template #[`${five.value}-column`]="{column}">
  43. <slot :name="`${five.value}-column`" :column="column">
  44. </slot>
  45. </template>
  46. <template #[`${five.value}-column-value`]="{scope}">
  47. <slot :name="`${five.value}-column-value`" :scope="scope">
  48. </slot>
  49. </template>
  50. <template #[`${five.value}-header-value`]="{scope}">
  51. <slot :name="`${five.value}-header-value`" :scope="scope">
  52. </slot>
  53. </template>
  54. </CusTableColumn>
  55. </template>
  56. </el-table-column>
  57. </template>
  58. <template v-else>
  59. <CusTableColumn :item="four">
  60. <template #[`${four.value}-column`]="{column}">
  61. <slot :name="`${four.value}-column`" :column="column">
  62. </slot>
  63. </template>
  64. <template #[`${four.value}-column-value`]="{scope}">
  65. <slot :name="`${four.value}-column-value`" :scope="scope">
  66. </slot>
  67. </template>
  68. <template #[`${four.value}-header-value`]="{scope}">
  69. <slot :name="`${four.value}-header-value`" :scope="scope">
  70. </slot>
  71. </template>
  72. </CusTableColumn>
  73. </template>
  74. </template>
  75. </el-table-column>
  76. </template>
  77. <template v-else>
  78. <CusTableColumn :item="three">
  79. <template #[`${three.value}-column`]="{column}">
  80. <slot :name="`${three.value}-column`" :column="column">
  81. </slot>
  82. </template>
  83. <template #[`${three.value}-column-value`]="{scope}">
  84. <slot :name="`${three.value}-column-value`" :scope="scope">
  85. </slot>
  86. </template>
  87. <template #[`${three.value}-header-value`]="{scope}">
  88. <slot :name="`${three.value}-header-value`" :scope="scope">
  89. </slot>
  90. </template>
  91. </CusTableColumn>
  92. </template>
  93. </template>
  94. </el-table-column>
  95. </template>
  96. <template v-else>
  97. <CusTableColumn :item="two">
  98. <template #[`${two.value}-column`]="{column}">
  99. <slot :name="`${two.value}-column`" :column="column">
  100. </slot>
  101. </template>
  102. <template #[`${two.value}-column-value`]="{scope}">
  103. <slot :name="`${two.value}-column-value`" :scope="scope">
  104. </slot>
  105. </template>
  106. <template #[`${two.value}-header-value`]="{scope}">
  107. <slot :name="`${two.value}-header-value`" :scope="scope">
  108. </slot>
  109. </template>
  110. </CusTableColumn>
  111. </template>
  112. </template>
  113. </el-table-column>
  114. </template>
  115. <template v-else>
  116. <CusTableColumn :item="one">
  117. <template #[`${one.value}-column`]="{column}">
  118. <slot :name="`${one.value}-column`" :column="column">
  119. </slot>
  120. </template>
  121. <template #[`${one.value}-column-value`]="{scope}">
  122. <slot :name="`${one.value}-column-value`" :scope="scope">
  123. </slot>
  124. </template>
  125. <template #[`${one.value}-header-value`]="{scope}">
  126. <slot :name="`${one.value}-header-value`" :scope="scope">
  127. </slot>
  128. </template>
  129. </CusTableColumn>
  130. </template>
  131. </template>
  132. </el-table>
  133. </div>
  134. <div class="cus-table-page" v-if="!noPage">
  135. <CusPage
  136. :page-num="pageNum"
  137. :page-size="pageSize"
  138. :total="total"
  139. @page="onPage"
  140. />
  141. </div>
  142. </div>
  143. </template>
  144. <script setup lang="ts">
  145. defineOptions({
  146. name: 'CusTable',
  147. });
  148. import {computed, getCurrentInstance, reactive, ref, watch} from "vue";
  149. const emit = defineEmits(['handleSort', 'handleFilter', 'handlePage', 'update:selected'])
  150. const {proxy} = getCurrentInstance()
  151. const props = defineProps({
  152. pageNum: {},
  153. pageSize: {},
  154. total: {},
  155. rowKey: {default: 'id'},
  156. data: {},
  157. selected: {default: null},
  158. selectWidth: {default: 50},
  159. singledWidth: {default: 50},
  160. singled: {default: null},
  161. singledKey: {default: 'id'},
  162. showIndex: {default: true},
  163. selectable: {},
  164. tableHead: {default: []},
  165. noPage: {default: false}
  166. })
  167. const state: any = reactive({})
  168. const ref_table = ref()
  169. const handleSort = ({ column, prop, order }: any) => {
  170. emit('handleSort', {
  171. key: prop,
  172. value: order
  173. })
  174. }
  175. const handleFilter = (val: any) => {
  176. const key = Object.keys(val)[0]
  177. const head = props.tableHead.filter(v => v.value === key)[0]
  178. let value = null
  179. if (val[key].length > 0) {
  180. if (head.filterMultiple) {
  181. value = val[key]
  182. } else {
  183. value = val[key][0]
  184. }
  185. }
  186. emit('handleFilter', {key, value})
  187. }
  188. const onPage = (pageNum, pageSize) => {
  189. emit('handlePage', pageNum, pageSize)
  190. }
  191. watch(() => props.selected, (n: any) => {
  192. if (n.length === 0) {
  193. ref_table.value.clearSelection()
  194. } else {
  195. setTimeout(() => {
  196. n.forEach(v => {
  197. ref_table.value.toggleRowSelection(v, true)
  198. })
  199. }, 100)
  200. }
  201. })
  202. </script>
  203. <style lang="scss" scoped>
  204. .cus-table {
  205. width: 100%;
  206. height: 100%;
  207. display: flex;
  208. flex-direction: column;
  209. .cus-table-table {
  210. flex: 1;
  211. overflow: hidden;
  212. :deep(.el-table) {
  213. height: 100%;
  214. .el-table__header-wrapper, .el-table__fixed-right, .el-table__fixed {
  215. .el-table__header {
  216. border-bottom: 2px solid var(--cus-main-color);
  217. tr {
  218. >th {
  219. background-color: var(--cus-main-color-hex-2);
  220. height: 44px;
  221. padding: 0;
  222. .cell {
  223. font-weight: bold;
  224. font-size: 16px;
  225. color: var(--cus-text-color-1);
  226. }
  227. }
  228. }
  229. }
  230. }
  231. .el-table__body-wrapper {
  232. .el-scrollbar__wrap {
  233. max-height: 100% !important;
  234. .el-table__body {
  235. .el-table__row {
  236. >td {
  237. .cell {
  238. font-size: 14px;
  239. color: var(--cus-text-color-1);
  240. .el-link+.el-link, .cus-table-button+.cus-table-button {
  241. margin-left: 10px;
  242. }
  243. }
  244. }
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. .cus-table-page {
  252. margin-top: 10px;
  253. display: flex;
  254. justify-content: flex-end;
  255. }
  256. }
  257. </style>