当前位置:   article > 正文

vue element ui 表格有相同数据合并单元格_element ui table 根据参数名是否相同合并单元格

element ui table 根据参数名是否相同合并单元格

先看效果  

 

前提是你的数据是扁平的数据因为要根据上下数据是否一样才合并的  如果是子级数据需要改一下数据格式了

下面是数据的样式

  1. tableData: [{
  2. id: '1',
  3. name: '王小虎',
  4. amount1: '165',
  5. amount2: '3.2',
  6. amount3: 10
  7. }, {
  8. id: '1',
  9. name: '王小虎',
  10. amount1: '162',
  11. amount2: '4.43',
  12. amount3: 12
  13. }, {
  14. id: '1',
  15. name: '王we虎',
  16. amount1: '621',
  17. amount2: '1.9',
  18. amount3: 9
  19. }, {
  20. id: '2',
  21. name: '王we虎',
  22. amount1: '621',
  23. amount2: '2.2',
  24. amount3: 17
  25. }, {
  26. id: '3',
  27. name: '王小虎',
  28. amount1: '621',
  29. amount2: '4.1',
  30. amount3: 15
  31. }],

 

 合并单元格的重点属性就是 :summary-method="" 这个是关键

  1. <el-table ref="tableRef" show-summary :summary-method="getSummaries" :span-method="handleSpanMethod" :data="listData" border style="width: 100%; margin-top: 5px">
  2. <el-table-column label="No." type="index" align="center" width="50"></el-table-column>
  3. <el-table-column prop="checkResult" label="Check result"> </el-table-column>
  4. <el-table-column prop="standardChineseName" label="Standard chinese name"> </el-table-column>
  5. <el-table-column prop="ingredientInciCtfaName" label="Ingredient INCI(CTFA) name"> </el-table-column>
  6. <el-table-column prop="RMInFormula" label="RM in formula(%)"> </el-table-column>
  7. <el-table-column prop="ingredientInRM" label="Ingredient in RM(%)"> </el-table-column>
  8. <el-table-column prop="ingredientInFormula" label="Ingredient in formula(%)"> </el-table-column>
  9. <el-table-column prop="purposeOfUse" label="Purpose of use"> </el-table-column>
  10. <el-table-column prop="templateUrl5" label="New RM" align="center" width="100"> </el-table-column>
  11. </el-table>
  1. data() {
  2. return {
  3. listData: [],
  4. // 合并单元格
  5. column1Arr: [], // column1
  6. column1Index: 0, // column1索引
  7. column2Arr: [], // column2
  8. column2Index: 0, // column2索引
  9. column3Arr: [], // column3
  10. column3Index: 0, // column3索引
  11. column4Arr: [], // column4
  12. column4Index: 0, // column4索引
  13. column5Arr: [], // column5
  14. column5Index: 0, // column5索引
  15. column6Arr: [], // column6
  16. column6Index: 0, // column6索引
  17. column7Arr: [], // column7
  18. column7Index: 0, // column7索引
  19. }
  20. },
  1. mergeTable(data) {
  2. if (data.length > 0) {
  3. for (var i = 0; i < data.length; i++) {
  4. if (i === 0) {
  5. // 第一行必须存在,以第一行为基准 合并的数量根据直接需求改
  6. this.column1Arr.push(1) // column1
  7. this.column1Index = 0
  8. this.column2Arr.push(1) // column2
  9. this.column2Index = 0
  10. this.column3Arr.push(1) // column3
  11. this.column3Index = 0
  12. this.column4Arr.push(1) // column4
  13. this.column4Index = 0
  14. this.column5Arr.push(1) // column5
  15. this.column5Index = 0
  16. this.column6Arr.push(1) // column6
  17. this.column6Index = 0
  18. this.column7Arr.push(1) // column7
  19. this.column7Index = 0
  20. } else {
  21. // 判断当前元素与上一元素是否相同
  22. // 我是根据第一个数据合并后续才可以合并
  23. //如果是 根据当前表格的前一列可以在每一个 column1++ 后面添加上前一个表格的列
  24. // 如果是只要相同就合并那就每个column 里面的条件直接当前列就可以了
  25. // column1
  26. if (data[i].checkResult === data[i - 1].checkResult) {
  27. this.column1Arr[this.column1Index] += 1
  28. this.column1Arr.push(0)
  29. } else {
  30. this.column1Arr.push(1)
  31. this.column1Index = i
  32. }
  33. // column2
  34. if (data[i].standardChineseName === data[i - 1].standardChineseName && data[i].checkResult === data[i - 1].checkResult) {
  35. this.column2Arr[this.column2Index] += 1
  36. this.column2Arr.push(0)
  37. } else {
  38. this.column2Arr.push(1)
  39. this.column2Index = i
  40. }
  41. // column3
  42. if (data[i].ingredientInciCtfaName === data[i - 1].ingredientInciCtfaName && data[i].checkResult === data[i - 1].checkResult) {
  43. this.column3Arr[this.column3Index] += 1
  44. this.column3Arr.push(0)
  45. } else {
  46. this.column3Arr.push(1)
  47. this.column3Index = i
  48. }
  49. // column4
  50. if (data[i].RMInFormula === data[i - 1].RMInFormula && data[i].checkResult === data[i - 1].checkResult) {
  51. this.column4Arr[this.column4Index] += 1
  52. this.column4Arr.push(0)
  53. } else {
  54. this.column4Arr.push(1)
  55. this.column4Index = i
  56. }
  57. // column5
  58. if (data[i].ingredientInRM === data[i - 1].ingredientInRM && data[i].checkResult === data[i - 1].checkResult) {
  59. this.column5Arr[this.column5Index] += 1
  60. this.column5Arr.push(0)
  61. } else {
  62. this.column5Arr.push(1)
  63. this.column5Index = i
  64. }
  65. // column6
  66. if (data[i].ingredientInFormula === data[i - 1].ingredientInFormula && data[i].checkResult === data[i - 1].checkResult) {
  67. this.column6Arr[this.column6Index] += 1
  68. this.column6Arr.push(0)
  69. } else {
  70. this.column6Arr.push(1)
  71. this.column6Index = i
  72. }
  73. // column7
  74. if (data[i].purposeOfUse === data[i - 1].purposeOfUse && data[i].checkResult === data[i - 1].checkResult) {
  75. this.column7Arr[this.column7Index] += 1
  76. this.column7Arr.push(0)
  77. } else {
  78. this.column7Arr.push(1)
  79. this.column7Index = i
  80. }
  81. }
  82. }
  83. }
  84. },
  85. //我这里是在表格的第二列开始合并因为第一列是 序号 不可以合并 从第一个开始合并就把 下表
  86. //改为0 columnIndex === 0
  87. handleSpanMethod({ rowIndex, columnIndex }) {
  88. if (columnIndex === 1) {
  89. // 第二列 column2
  90. const _row_1 = this.column1Arr[rowIndex]
  91. const _col_1 = _row_1 > 0 ? 1 : 0
  92. return {
  93. rowspan: _row_1,
  94. colspan: _col_1,
  95. }
  96. }
  97. else if (columnIndex === 2) {
  98. // 第三列 column3
  99. const _row_2 = this.column2Arr[rowIndex]
  100. const _col_2 = _row_2 > 0 ? 1 : 0
  101. return {
  102. rowspan: _row_2,
  103. colspan: _col_2,
  104. }
  105. } else if (columnIndex === 3) {
  106. // 第四列 column4
  107. const _row_3 = this.column3Arr[rowIndex]
  108. const _col_3 = _row_3 > 0 ? 1 : 0
  109. return {
  110. rowspan: _row_3,
  111. colspan: _col_3,
  112. }
  113. } else if (columnIndex === 4) {
  114. // 第五列 column5
  115. const _row_4 = this.column4Arr[rowIndex]
  116. const _col_4 = _row_4 > 0 ? 1 : 0
  117. return {
  118. rowspan: _row_4,
  119. colspan: _col_4,
  120. }
  121. } else if (columnIndex === 5) {
  122. // 第六列 column6
  123. const _row_5 = this.column5Arr[rowIndex]
  124. const _col_5 = _row_5 > 0 ? 1 : 0
  125. return {
  126. rowspan: _row_5,
  127. colspan: _col_5,
  128. }
  129. } else if (columnIndex === 6) {
  130. // 第七列 column7
  131. const _row_6 = this.column6Arr[rowIndex]
  132. const _col_6 = _row_6 > 0 ? 1 : 0
  133. return {
  134. rowspan: _row_6,
  135. colspan: _col_6,
  136. }
  137. } else if (columnIndex === 7) {
  138. // 第八列 column8
  139. const _row_7 = this.column7Arr[rowIndex]
  140. const _col_7 = _row_7 > 0 ? 1 : 0
  141. return {
  142. rowspan: _row_7,
  143. colspan: _col_7,
  144. }
  145. }
  146. },
  1. //调取接口获取数据
  2. getList() {
  3. const prams = {
  4. taskId: this.taskId,
  5. formulaId: this.formulaId
  6. }
  7. this.$http({
  8. headers: {
  9. 'Content-Type': 'application/json',
  10. },
  11. method: 'post',
  12. url: '/123123',
  13. data: prams,
  14. })
  15. .then(res=>{
  16. this.listData = res.data.records //给data变量赋值
  17. this.mergeTable(this.listData)//合并单元格传入数据
  18. })
  19. //清空之前的数据 不清空 表格数据就会乱套
  20. this.column1Arr = []
  21. this.column1Index = 0
  22. this.column2Arr = []
  23. this.column2Index = 0
  24. this.column3Arr = []
  25. this.column3Index = 0
  26. this.column4Arr = []
  27. this.column4Index = 0
  28. this.column5Arr = []
  29. this.column5Index = 0
  30. this.column6Arr = []
  31. this.column6Index = 0
  32. this.column7Arr = []
  33. this.column7Index = 0
  34. },

完整代码

  1. <template>
  2. <div>
  3. <el-table ref="tableRef" show-summary :summary-method="getSummaries" :span-method="handleSpanMethod" :data="listData" border style="width: 100%; margin-top: 5px">
  4. <el-table-column label="No." type="index" align="center" width="50"></el-table-column>
  5. <el-table-column prop="checkResult" label="Check result"> </el-table-column>
  6. <el-table-column prop="standardChineseName" label="Standard chinese name"> </el-table-column>
  7. <el-table-column prop="ingredientInciCtfaName" label="Ingredient INCI(CTFA) name"> </el-table-column>
  8. <el-table-column prop="RMInFormula" label="RM in formula(%)"> </el-table-column>
  9. <el-table-column prop="ingredientInRM" label="Ingredient in RM(%)"> </el-table-column>
  10. <el-table-column prop="ingredientInFormula" label="Ingredient in formula(%)"> </el-table-column>
  11. <el-table-column prop="purposeOfUse" label="Purpose of use"> </el-table-column>
  12. <el-table-column prop="templateUrl5" label="New RM" align="center" width="100"> </el-table-column>
  13. </el-table>
  14. </div>
  15. </template>
  16. <script>
  17. export default {
  18. components: {},
  19. data() {
  20. return {
  21. listData: [],
  22. // 合并单元格
  23. column1Arr: [], // column1
  24. column1Index: 0, // column1索引
  25. column2Arr: [], // column2
  26. column2Index: 0, // column2索引
  27. column3Arr: [], // column3
  28. column3Index: 0, // column3索引
  29. column4Arr: [], // column4
  30. column4Index: 0, // column4索引
  31. column5Arr: [], // column5
  32. column5Index: 0, // column5索引
  33. column6Arr: [], // column6
  34. column6Index: 0, // column6索引
  35. column7Arr: [], // column7
  36. column7Index: 0, // column7索引
  37. }
  38. },
  39. computed: {},
  40. created() {
  41. this.getList()
  42. },
  43. mounted() {
  44. },
  45. methods: {
  46. getList() {
  47. const prams = {
  48. taskId: this.taskId,
  49. formulaId: this.formulaId
  50. }
  51. this.$http({
  52. headers: {
  53. 'Content-Type': 'application/json',
  54. },
  55. method: 'post',
  56. url: '/1123123123',
  57. data: prams,
  58. })
  59. .then(res=>{
  60. this.listData = res.data.records//给data中的变量赋值 把res.data.records换成自己
  61. //的据接口 更改一下数据的字段 便可以直接使用了
  62. this.mergeTable(this.listData)//合并单元格传入数据
  63. })
  64. //清空之前的数据
  65. this.column1Arr = []
  66. this.column1Index = 0
  67. this.column2Arr = []
  68. this.column2Index = 0
  69. this.column3Arr = []
  70. this.column3Index = 0
  71. this.column4Arr = []
  72. this.column4Index = 0
  73. this.column5Arr = []
  74. this.column5Index = 0
  75. this.column6Arr = []
  76. this.column6Index = 0
  77. this.column7Arr = []
  78. this.column7Index = 0
  79. },
  80. mergeTable(data) {
  81. if (data.length > 0) {
  82. for (var i = 0; i < data.length; i++) {
  83. if (i === 0) {
  84. // 第一行必须存在,以第一行为基准
  85. this.column1Arr.push(1) // column1
  86. this.column1Index = 0
  87. this.column2Arr.push(1) // column2
  88. this.column2Index = 0
  89. this.column3Arr.push(1) // column3
  90. this.column3Index = 0
  91. this.column4Arr.push(1) // column4
  92. this.column4Index = 0
  93. this.column5Arr.push(1) // column5
  94. this.column5Index = 0
  95. this.column6Arr.push(1) // column6
  96. this.column6Index = 0
  97. this.column7Arr.push(1) // column7
  98. this.column7Index = 0
  99. } else {
  100. // 判断当前元素与上一元素是否相同
  101. // column1
  102. if (data[i].checkResult === data[i - 1].checkResult) {
  103. this.column1Arr[this.column1Index] += 1
  104. this.column1Arr.push(0)
  105. } else {
  106. this.column1Arr.push(1)
  107. this.column1Index = i
  108. }
  109. // column2
  110. if (data[i].standardChineseName === data[i - 1].standardChineseName && data[i].checkResult === data[i - 1].checkResult) {
  111. this.column2Arr[this.column2Index] += 1
  112. this.column2Arr.push(0)
  113. } else {
  114. this.column2Arr.push(1)
  115. this.column2Index = i
  116. }
  117. // column3
  118. if (data[i].ingredientInciCtfaName === data[i - 1].ingredientInciCtfaName && data[i].checkResult === data[i - 1].checkResult) {
  119. this.column3Arr[this.column3Index] += 1
  120. this.column3Arr.push(0)
  121. } else {
  122. this.column3Arr.push(1)
  123. this.column3Index = i
  124. }
  125. // column4
  126. if (data[i].RMInFormula === data[i - 1].RMInFormula && data[i].checkResult === data[i - 1].checkResult) {
  127. this.column4Arr[this.column4Index] += 1
  128. this.column4Arr.push(0)
  129. } else {
  130. this.column4Arr.push(1)
  131. this.column4Index = i
  132. }
  133. // column5
  134. if (data[i].ingredientInRM === data[i - 1].ingredientInRM && data[i].checkResult === data[i - 1].checkResult) {
  135. this.column5Arr[this.column5Index] += 1
  136. this.column5Arr.push(0)
  137. } else {
  138. this.column5Arr.push(1)
  139. this.column5Index = i
  140. }
  141. // column6
  142. if (data[i].ingredientInFormula === data[i - 1].ingredientInFormula && data[i].checkResult === data[i - 1].checkResult) {
  143. this.column6Arr[this.column6Index] += 1
  144. this.column6Arr.push(0)
  145. } else {
  146. this.column6Arr.push(1)
  147. this.column6Index = i
  148. }
  149. // column7
  150. if (data[i].purposeOfUse === data[i - 1].purposeOfUse && data[i].checkResult === data[i - 1].checkResult) {
  151. this.column7Arr[this.column7Index] += 1
  152. this.column7Arr.push(0)
  153. } else {
  154. this.column7Arr.push(1)
  155. this.column7Index = i
  156. }
  157. }
  158. }
  159. }
  160. },
  161. handleSpanMethod({ rowIndex, columnIndex }) {
  162. if (columnIndex === 1) {
  163. // 第二列 column2
  164. const _row_1 = this.column1Arr[rowIndex]
  165. const _col_1 = _row_1 > 0 ? 1 : 0
  166. return {
  167. rowspan: _row_1,
  168. colspan: _col_1,
  169. }
  170. }
  171. else if (columnIndex === 2) {
  172. // 第三列 column3
  173. const _row_2 = this.column2Arr[rowIndex]
  174. const _col_2 = _row_2 > 0 ? 1 : 0
  175. return {
  176. rowspan: _row_2,
  177. colspan: _col_2,
  178. }
  179. } else if (columnIndex === 3) {
  180. // 第四列 column4
  181. const _row_3 = this.column3Arr[rowIndex]
  182. const _col_3 = _row_3 > 0 ? 1 : 0
  183. return {
  184. rowspan: _row_3,
  185. colspan: _col_3,
  186. }
  187. } else if (columnIndex === 4) {
  188. // 第五列 column5
  189. const _row_4 = this.column4Arr[rowIndex]
  190. const _col_4 = _row_4 > 0 ? 1 : 0
  191. return {
  192. rowspan: _row_4,
  193. colspan: _col_4,
  194. }
  195. } else if (columnIndex === 5) {
  196. // 第六列 column6
  197. const _row_5 = this.column5Arr[rowIndex]
  198. const _col_5 = _row_5 > 0 ? 1 : 0
  199. return {
  200. rowspan: _row_5,
  201. colspan: _col_5,
  202. }
  203. } else if (columnIndex === 6) {
  204. // 第七列 column7
  205. const _row_6 = this.column6Arr[rowIndex]
  206. const _col_6 = _row_6 > 0 ? 1 : 0
  207. return {
  208. rowspan: _row_6,
  209. colspan: _col_6,
  210. }
  211. } else if (columnIndex === 7) {
  212. // 第八列 column8
  213. const _row_7 = this.column7Arr[rowIndex]
  214. const _col_7 = _row_7 > 0 ? 1 : 0
  215. return {
  216. rowspan: _row_7,
  217. colspan: _col_7,
  218. }
  219. }
  220. }
  221. }
  222. </script>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/155099
推荐阅读
相关标签
  

闽ICP备14008679号