table合并单元格

柔情只为你懂 2022-09-04 04:54 376阅读 0赞

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L01yX2RvbmdfeWFfeXVu_size_16_color_FFFFFF_t_70

遇到这种设计,记录一下

  1. <el-table
  2. :data="tableDataNew"
  3. :span-method="objectSpanMethod"
  4. border
  5. style="width: 100%; margin-top: 20px">
  6. <el-table-column
  7. prop="id"
  8. label="ID"
  9. width="180">
  10. </el-table-column>
  11. <el-table-column
  12. prop="name"
  13. label="姓名">
  14. </el-table-column>
  15. <el-table-column
  16. prop="amount1"
  17. label="数值 1(元)">
  18. </el-table-column>
  19. <el-table-column
  20. prop="amount2"
  21. label="数值 2(元)">
  22. </el-table-column>
  23. <el-table-column
  24. prop="productName"
  25. label="商品名称">
  26. </el-table-column>
  27. <el-table-column
  28. prop="price"
  29. label="价格">
  30. </el-table-column>
  31. <el-table-column
  32. prop="address"
  33. label="产地">
  34. </el-table-column>
  35. </el-table>
  36. <script>
  37. export default {
  38. data() {
  39. return {
  40. tableDataNew:[],
  41. tableData: [{
  42. id: '12987122',
  43. name: '王小虎1',
  44. amount1: '234',
  45. amount2: '3.2',
  46. amount3: 10,
  47. children:[
  48. {
  49. address:'上海',
  50. productName:'苹果',
  51. price:'12.00'
  52. }, {
  53. address:'上海',
  54. productName:'苹果',
  55. price:'13.00'
  56. },
  57. {
  58. address:'上海',
  59. productName:'苹果',
  60. price:'15.00'
  61. }
  62. ]
  63. }, {
  64. id: '12987124',
  65. name: '王小虎2',
  66. amount1: '324',
  67. amount2: '1.9',
  68. amount3: 9,
  69. children:[
  70. {
  71. address:'上海',
  72. productName:'苹果',
  73. price:'12.00'
  74. },
  75. {
  76. address:'上海',
  77. productName:'苹果',
  78. price:'12.00'
  79. }
  80. ]
  81. }, {
  82. id: '12987125',
  83. name: '王小虎3',
  84. amount1: '621',
  85. amount2: '2.2',
  86. amount3: 17,
  87. children:[
  88. {
  89. address:'上海',
  90. productName:'苹果',
  91. price:'12.00'
  92. }, {
  93. address:'上海',
  94. productName:'苹果',
  95. price:'13.00'
  96. },
  97. {
  98. address:'上海',
  99. productName:'苹果',
  100. price:'15.00'
  101. }
  102. ]
  103. }, {
  104. id: '12987126',
  105. name: '王小虎4',
  106. amount1: '539',
  107. amount2: '4.1',
  108. amount3: 15,
  109. children:[
  110. {
  111. address:'上海',
  112. productName:'苹果',
  113. price:'12.00'
  114. }, {
  115. address:'上海',
  116. productName:'苹果',
  117. price:'13.00'
  118. },
  119. {
  120. address:'上海',
  121. productName:'苹果',
  122. price:'15.00'
  123. }
  124. ]
  125. }]
  126. };
  127. },
  128. mounted() {
  129. this.dataFilter()
  130. },
  131. methods: {
  132. dataFilter() {
  133. // 主要是数据符合table的要求,用foreach for 循环都可,两层循环,把数组扁平化,提供一个思路
  134. this.tableDataNew =[]
  135. console.log('thisdate',this.tableData)
  136. for (let j=0; j<this.tableData.length; j++) {
  137. console.log('this.tableData.length',this.tableData.length)
  138. console.log('this.tableData[j].children.length',this.tableData[j].children)
  139. if(this.tableData[j].children === undefined || this.tableData[j].children === 'undefined') {
  140. this.tableDataNew.push({...{
  141. id: this.tableData[j].id,
  142. name: this.tableData[j].name,
  143. amount1: this.tableData[j].amount1,
  144. amount2: this.tableData[j].amount2,
  145. }})
  146. console.log('结果',this.tableDataNew)
  147. } else {
  148. for (let i = 0;i < this.tableData[j].children.length; i++) {
  149. console.log('this.tableData[j].children.length',this.tableData[j].children.length)
  150. this.tableDataNew = this.tableDataNew.concat({...this.tableData[j].children[i],...{
  151. id: this.tableData[j].id,
  152. name: this.tableData[j].name,
  153. amount1: this.tableData[j].amount1,
  154. amount2: this.tableData[j].amount2,
  155. length: this.tableData[j].children.length,
  156. istarget: i ===0 || i === this.tableData[j].children.length+1? true :false // 记录合并单元格位置
  157. }})
  158. }
  159. }
  160. }
  161. console.log('新数据', this.tableDataNew)
  162. },
  163. // 合并单元格
  164. objectSpanMethod({ row, column, rowIndex, columnIndex }) {
  165. if (columnIndex < 4) {
  166. if(row.istarget) {
  167. return {
  168. rowspan: row.length,
  169. colspan: 1
  170. };
  171. } else {
  172. return {
  173. rowspan: 0,
  174. colspan: 0
  175. };
  176. }
  177. }
  178. console.log('row.istarget', row.istarget )
  179. }
  180. }
  181. };
  182. </script>

发表评论

表情:
评论列表 (有 0 条评论,376人围观)

还没有评论,来说两句吧...

相关阅读