index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <div>
  3. <el-alert
  4. effect="dark"
  5. :closable="false"
  6. title="基于 Element 的 Table 组件进行二次封装,实现数据驱动,支持所有 Table 参数 -- 合并行或列"
  7. type="info"
  8. style="margin-bottom: 20px;"
  9. />
  10. <com-table
  11. v-loading="loading"
  12. :columns="columns"
  13. :data="tableData"
  14. :span-method="arraySpanMethod"
  15. border
  16. />
  17. <com-table
  18. v-loading="loading"
  19. :columns="columns1"
  20. :data="tableData"
  21. :span-method="objectSpanMethod"
  22. border
  23. style="margin-top: 20px;"
  24. />
  25. </div>
  26. </template>
  27. <script lang="ts">
  28. import { defineComponent, ref } from 'vue'
  29. const columns = [
  30. {
  31. field: 'id',
  32. label: 'ID'
  33. },
  34. {
  35. field: 'name',
  36. label: '姓名'
  37. },
  38. {
  39. field: 'amount1',
  40. label: '数值1',
  41. sortable: true
  42. },
  43. {
  44. field: 'amount2',
  45. label: '数值2',
  46. sortable: true
  47. },
  48. {
  49. field: 'amount3',
  50. label: '数值4',
  51. sortable: true
  52. }
  53. ]
  54. const columns1 = [
  55. {
  56. field: 'id',
  57. label: 'ID'
  58. },
  59. {
  60. field: 'name',
  61. label: '姓名'
  62. },
  63. {
  64. field: 'amount1',
  65. label: '数值1(元)'
  66. },
  67. {
  68. field: 'amount2',
  69. label: '数值2(元)'
  70. },
  71. {
  72. field: 'amount3',
  73. label: '数值4(元)'
  74. }
  75. ]
  76. const tableData = [
  77. {
  78. id: '12987122',
  79. name: '王小虎',
  80. amount1: '234',
  81. amount2: '3.2',
  82. amount3: 10
  83. }, {
  84. id: '12987123',
  85. name: '王小虎',
  86. amount1: '165',
  87. amount2: '4.43',
  88. amount3: 12
  89. }, {
  90. id: '12987124',
  91. name: '王小虎',
  92. amount1: '324',
  93. amount2: '1.9',
  94. amount3: 9
  95. }, {
  96. id: '12987125',
  97. name: '王小虎',
  98. amount1: '621',
  99. amount2: '2.2',
  100. amount3: 17
  101. }, {
  102. id: '12987126',
  103. name: '王小虎',
  104. amount1: '539',
  105. amount2: '4.1',
  106. amount3: 15
  107. }
  108. ]
  109. export default defineComponent({
  110. // name: 'MergeTable',
  111. setup() {
  112. const loading = ref<boolean>(true)
  113. setTimeout(() => {
  114. loading.value = false
  115. }, 1000)
  116. function arraySpanMethod({ rowIndex, columnIndex }: any) {
  117. if (rowIndex % 2 === 0) {
  118. if (columnIndex === 0) {
  119. return [1, 2]
  120. } else if (columnIndex === 1) {
  121. return [0, 0]
  122. }
  123. }
  124. }
  125. function objectSpanMethod({ rowIndex, columnIndex }: any) {
  126. if (columnIndex === 0) {
  127. if (rowIndex % 2 === 0) {
  128. return {
  129. rowspan: 2,
  130. colspan: 1
  131. }
  132. } else {
  133. return {
  134. rowspan: 0,
  135. colspan: 0
  136. }
  137. }
  138. }
  139. }
  140. return {
  141. columns, columns1,
  142. tableData,
  143. loading,
  144. arraySpanMethod, objectSpanMethod
  145. }
  146. }
  147. })
  148. </script>
  149. <style>
  150. </style>