关于element-ui中Table表格如何合并单元格

迷南。 2022-03-29 03:18 2231阅读 1赞

element-ui中Table表格如何合并单元格

相信大家在使用element-ui中table组件的时候在boss要求合并单元格时都犯过怵(仅指我这个小白),而element-ui中合并的时候也没有给全解释(也是我的问题),但工作在前,终于通过不懈努力,终于将它搞成功;文章最后贴上源码

第一步 给出的样式

我们需要的表格样式

第二步:代码开撸

首先在data中新建变量
  1. data() {
  2. return {
  3. skuListInfo: [ // 假数据
  4. {
  5. id: 1,
  6. name: '普通门店',
  7. storeIds: [1, 2, 3, 4],
  8. storeIdInfo: '[1, 2, 3, 4]',
  9. productType: '1',
  10. productInfo: '日托',
  11. mergeId: 1,
  12. feeType: '1',
  13. feeTypeInfo: '学费',
  14. billing: '月/季/年制度',
  15. },
  16. {
  17. id: 2,
  18. name: '普通门店',
  19. storeIds: [1, 2, 3, 4],
  20. storeIdInfo: '[1, 2, 3, 4]',
  21. productType: '2',
  22. productInfo: '晚托',
  23. feeType: '1',
  24. mergeId: 1,
  25. feeTypeInfo: '学费',
  26. billing: '月/季/年制度',
  27. },
  28. {
  29. id: 3,
  30. name: '普通门店',
  31. storeIds: [1, 2, 3, 4],
  32. storeIdInfo: '[1, 2, 3, 4]',
  33. productType: '3',
  34. productInfo: '临时托',
  35. feeType: '1',
  36. mergeId: 1,
  37. feeTypeInfo: '学费',
  38. billing: '月/季/年制度',
  39. },
  40. {
  41. id: 4,
  42. name: '普通门店',
  43. storeIds: [1, 2, 3, 4],
  44. storeIdInfo: '[1, 2, 3, 4]',
  45. productType: '4',
  46. productInfo: '越拖',
  47. feeType: '1',
  48. mergeId: 1,
  49. feeTypeInfo: '学费',
  50. billing: '月/季/年制度',
  51. },
  52. {
  53. id: 9,
  54. name: '普通门店',
  55. storeIds: [1, 2, 3, 4],
  56. storeIdInfo: '[1, 2, 3, 4]',
  57. productType: '4',
  58. productInfo: '全部',
  59. feeType: '2',
  60. mergeId: 1,
  61. feeTypeInfo: '定金',
  62. billing: '月/季/年制度',
  63. },
  64. {
  65. id: 10,
  66. name: '普通门店',
  67. storeIds: [1, 2, 3, 4],
  68. storeIdInfo: '[1, 2, 3, 4]',
  69. productType: '4',
  70. productInfo: '全部',
  71. feeType: '3',
  72. mergeId: 1,
  73. feeTypeInfo: '学杂费',
  74. billing: '月/季/年制度',
  75. },
  76. {
  77. id: 5,
  78. name: '团购',
  79. storeIds: [1, 2, 3, 4],
  80. storeIdInfo: '[1, 2, 3, 4]',
  81. productType: '5',
  82. productInfo: '寒假托',
  83. feeType: '2',
  84. mergeId: 1,
  85. feeTypeInfo: '定金',
  86. billing: '月/季/年制度',
  87. },
  88. {
  89. id: 6,
  90. name: '团购',
  91. storeIds: [1, 2, 3, 4],
  92. storeIdInfo: '[1, 2, 3, 4]',
  93. productType: '5',
  94. productInfo: '日托',
  95. feeType: '1',
  96. mergeId: 1,
  97. feeTypeInfo: '学费',
  98. billing: '月/季/年制度',
  99. },
  100. {
  101. id: 7,
  102. name: '团购',
  103. storeIds: [1, 2, 3, 4],
  104. storeIdInfo: '[1, 2, 3, 4]',
  105. productType: '5',
  106. productInfo: '晚托',
  107. feeType: '1',
  108. mergeId: 1,
  109. feeTypeInfo: '学费',
  110. billing: '月/季/年制度',
  111. },
  112. {
  113. id: 8,
  114. name: '大客户',
  115. storeIds: [1, 2, 3, 4],
  116. storeIdInfo: '[1, 2, 3, 4]',
  117. productType: '6',
  118. productInfo: '暑假托',
  119. feeType: '3',
  120. mergeId: 1,
  121. feeTypeInfo: '学杂费',
  122. billing: '月/季/年制度',
  123. },
  124. ],
  125. typeNameArr: [], // 第一列进行合并操作时存放的数组变量
  126. typeNamePos: 0, // 上面的数组的下标值
  127. storeArr: [], // 第二列进行合并操作时存放的数组变量
  128. storePos: 0,// 上面的数组的下标值
  129. feeArr: [], // 第三列进行合并操作时存放的数组变量
  130. feePos: 0,// 上面的数组的下标值
  131. };
  132. },
接着初始化上述定义的变量
  1. merageInit() { // 在下文的时候会用到,对数据进行初始化是很有必要的
  2. this.typeNameArr = [];
  3. this.typeNamePos = 0;
  4. this.storeArr = [];
  5. this.storePos = 0;
  6. this.feeArr = [];
  7. this.feePos = 0;
  8. },

对数据进行处理,通过merage处理之后才可以重新合并

  1. merage() {
  2. this.merageInit(); // 前文的初始化数据函数
  3. for (let i = 0; i < this.skuListInfo.length; i += 1) {
  4. if (i === 0) {
  5. // 第一行必须存在
  6. this.typeNameArr.push(1);
  7. this.typeNamePos = 0;
  8. this.storeArr.push(1);
  9. this.storePos = 0;
  10. this.feeArr.push(1);
  11. this.feePos = 0;
  12. } else {
  13. // 判断当前元素与上一个元素是否相同,eg:this.typeNamePos 是 this.typeNameArr序号
  14. // 第一列 下面的是eslint的不限制语法
  15. // eslint-disable-next-line no-lonely-if
  16. if (this.skuListInfo[i].name === this.skuListInfo[i - 1].name) {
  17. this.typeNameArr[this.typeNamePos] += 1;
  18. this.typeNameArr.push(0);
  19. } else {
  20. this.typeNameArr.push(1);
  21. this.typeNamePos = i;
  22. }
  23. // 第二列
  24. if (this.skuListInfo[i].storeIdInfo === this.skuListInfo[i - 1].storeIdInfo && this.skuListInfo[i].name ===
  25. this.skuListInfo[i - 1].name) {
  26. this.storeArr[this.storePos] += 1;
  27. this.storeArr.push(0);
  28. } else {
  29. this.storeArr.push(1);
  30. this.storePos = i;
  31. }
  32. // 第三列
  33. if (this.skuListInfo[i].feeType === this.skuListInfo[i - 1].feeType && this.skuListInfo[i].storeIdInfo
  34. === this.skuListInfo[i - 1].storeIdInfo && this.skuListInfo[i].name ===
  35. this.skuListInfo[i - 1].name) {
  36. this.feeArr[this.feePos] += 1;
  37. this.feeArr.push(0);
  38. } else {
  39. this.feeArr.push(1);
  40. this.feePos = i;
  41. }
  42. }
  43. }
  44. },

上述工作完成之后就到了最重要的一步——-使用element的自带函数

  1. objectSpanMethod({ row, column, rowIndex, columnIndex }) {
  2. // if (columnIndex === 0) { // 用于设置要合并的列
  3. // if (rowIndex % 2 === 0) { // 用于设置合并开始的行号
  4. // return {
  5. // rowspan: 2, // 合并的行数
  6. // colspan: 1, // 合并的猎术, 设置为0则直接不显示
  7. // };
  8. // }
  9. // return {
  10. // rowspan: 0,
  11. // colspan: 0,
  12. // };
  13. // }
  14. if (columnIndex === 0) {
  15. // 第一列的合并方法
  16. const row1 = this.typeNameArr[rowIndex];
  17. const col1 = row1 > 0 ? 1 : 0; // 如果被合并了row = 0; 则他这个列需要取消
  18. return {
  19. rowspan: row1,
  20. colspan: col1,
  21. };
  22. } else if (columnIndex === 1) {
  23. // 第二列的合并方法
  24. const row2 = this.storeArr[rowIndex];
  25. const col2 = row2 > 0 ? 1 : 0; // 如果被合并了row = 0; 则他这个列需要取消
  26. return {
  27. rowspan: row2,
  28. colspan: col2,
  29. };
  30. } else if (columnIndex === 2) {
  31. // 第三列的合并方法
  32. const row3 = this.feeArr[rowIndex];
  33. const col3 = row3 > 0 ? 1 : 0; // 如果被合并了row = 0; 则他这个列需要取消
  34. return {
  35. rowspan: row3,
  36. colspan: col3,
  37. };
  38. }
  39. },

以上代码经过本人努力,测试成功,大家可以踊跃尝试更好方法,希望大家不吝赐教,共同进步!!!

当然也有小bug,当你鼠标移动到上面时hover不准确,当然肯定是有办法滴——cell-mouse-enter cell-mouse-leave cell-class-name

这三个方法可以有效修改,但我还是没搞太懂,就不献丑了,大家可以去看看这位兄台的小白变怪兽文章url

源码奉上

  1. <template>
  2. <div>
  3. <div class="panel">
  4. 1231
  5. </div>
  6. <div class="panel">
  7. <el-table :data="skuListInfo" :span-method="objectSpanMethod" border>
  8. <el-table-column prop="name" label="名称">
  9. </el-table-column>
  10. <el-table-column prop="storeIds" label="适应门店">
  11. </el-table-column>
  12. <el-table-column prop="feeTypeInfo" label="费用类型">
  13. </el-table-column>
  14. <el-table-column prop="productInfo" label="适用产品">
  15. </el-table-column>
  16. <el-table-column prop="billing" label="计费方法">
  17. </el-table-column>
  18. </el-table>
  19. </div>
  20. </div>
  21. </template>
  22. <script>
  23. export default {
  24. data() {
  25. return {
  26. skuListInfo: [
  27. {
  28. id: 1,
  29. name: '普通门店',
  30. storeIds: [1, 2, 3, 4],
  31. storeIdInfo: '[1, 2, 3, 4]',
  32. productType: '1',
  33. productInfo: '日托',
  34. mergeId: 1,
  35. feeType: '1',
  36. feeTypeInfo: '学费',
  37. billing: '月/季/年制度',
  38. },
  39. {
  40. id: 2,
  41. name: '普通门店',
  42. storeIds: [1, 2, 3, 4],
  43. storeIdInfo: '[1, 2, 3, 4]',
  44. productType: '2',
  45. productInfo: '晚托',
  46. feeType: '1',
  47. mergeId: 1,
  48. feeTypeInfo: '学费',
  49. billing: '月/季/年制度',
  50. },
  51. {
  52. id: 3,
  53. name: '普通门店',
  54. storeIds: [1, 2, 3, 4],
  55. storeIdInfo: '[1, 2, 3, 4]',
  56. productType: '3',
  57. productInfo: '临时托',
  58. feeType: '1',
  59. mergeId: 1,
  60. feeTypeInfo: '学费',
  61. billing: '月/季/年制度',
  62. },
  63. {
  64. id: 4,
  65. name: '普通门店',
  66. storeIds: [1, 2, 3, 4],
  67. storeIdInfo: '[1, 2, 3, 4]',
  68. productType: '4',
  69. productInfo: '越拖',
  70. feeType: '1',
  71. mergeId: 1,
  72. feeTypeInfo: '学费',
  73. billing: '月/季/年制度',
  74. },
  75. {
  76. id: 9,
  77. name: '普通门店',
  78. storeIds: [1, 2, 3, 4],
  79. storeIdInfo: '[1, 2, 3, 4]',
  80. productType: '4',
  81. productInfo: '全部',
  82. feeType: '2',
  83. mergeId: 1,
  84. feeTypeInfo: '定金',
  85. billing: '月/季/年制度',
  86. },
  87. {
  88. id: 10,
  89. name: '普通门店',
  90. storeIds: [1, 2, 3, 4],
  91. storeIdInfo: '[1, 2, 3, 4]',
  92. productType: '4',
  93. productInfo: '全部',
  94. feeType: '3',
  95. mergeId: 1,
  96. feeTypeInfo: '学杂费',
  97. billing: '月/季/年制度',
  98. },
  99. {
  100. id: 5,
  101. name: '团购',
  102. storeIds: [1, 2, 3, 4],
  103. storeIdInfo: '[1, 2, 3, 4]',
  104. productType: '5',
  105. productInfo: '寒假托',
  106. feeType: '2',
  107. mergeId: 1,
  108. feeTypeInfo: '定金',
  109. billing: '月/季/年制度',
  110. },
  111. {
  112. id: 6,
  113. name: '团购',
  114. storeIds: [1, 2, 3, 4],
  115. storeIdInfo: '[1, 2, 3, 4]',
  116. productType: '5',
  117. productInfo: '日托',
  118. feeType: '1',
  119. mergeId: 1,
  120. feeTypeInfo: '学费',
  121. billing: '月/季/年制度',
  122. },
  123. {
  124. id: 7,
  125. name: '团购',
  126. storeIds: [1, 2, 3, 4],
  127. storeIdInfo: '[1, 2, 3, 4]',
  128. productType: '5',
  129. productInfo: '晚托',
  130. feeType: '1',
  131. mergeId: 1,
  132. feeTypeInfo: '学费',
  133. billing: '月/季/年制度',
  134. },
  135. {
  136. id: 8,
  137. name: '大客户',
  138. storeIds: [1, 2, 3, 4],
  139. storeIdInfo: '[1, 2, 3, 4]',
  140. productType: '6',
  141. productInfo: '暑假托',
  142. feeType: '3',
  143. mergeId: 1,
  144. feeTypeInfo: '学杂费',
  145. billing: '月/季/年制度',
  146. },
  147. ],
  148. typeNameArr: [],
  149. typeNamePos: 0,
  150. storeArr: [],
  151. storePos: 0,
  152. feeArr: [],
  153. feePos: 0,
  154. hoverOrderArr: [],
  155. };
  156. },
  157. mounted() {
  158. this.merage();
  159. },
  160. methods: {
  161. merageInit() {
  162. this.typeNameArr = [];
  163. this.typeNamePos = 0;
  164. this.storeArr = [];
  165. this.storePos = 0;
  166. this.feeArr = [];
  167. this.feePos = 0;
  168. },
  169. merage() {
  170. this.merageInit();
  171. for (let i = 0; i < this.skuListInfo.length; i += 1) {
  172. if (i === 0) {
  173. // 第一行必须存在
  174. this.typeNameArr.push(1);
  175. this.typeNamePos = 0;
  176. this.storeArr.push(1);
  177. this.storePos = 0;
  178. this.feeArr.push(1);
  179. this.feePos = 0;
  180. } else {
  181. // 判断当前元素与上一个元素是否相同,eg:this.typeNamePos 是 this.typeNameArr序号
  182. // 第一列
  183. // eslint-disable-next-line no-lonely-if
  184. if (this.skuListInfo[i].name === this.skuListInfo[i - 1].name) {
  185. this.typeNameArr[this.typeNamePos] += 1;
  186. this.typeNameArr.push(0);
  187. } else {
  188. this.typeNameArr.push(1);
  189. this.typeNamePos = i;
  190. }
  191. // 第二列
  192. if (this.skuListInfo[i].storeIdInfo === this.skuListInfo[i - 1].storeIdInfo && this.skuListInfo[i].name ===
  193. this.skuListInfo[i - 1].name) {
  194. this.storeArr[this.storePos] += 1;
  195. this.storeArr.push(0);
  196. } else {
  197. this.storeArr.push(1);
  198. this.storePos = i;
  199. }
  200. // 第三列
  201. if (this.skuListInfo[i].feeType === this.skuListInfo[i - 1].feeType && this.skuListInfo[i].storeIdInfo
  202. === this.skuListInfo[i - 1].storeIdInfo && this.skuListInfo[i].name ===
  203. this.skuListInfo[i - 1].name) {
  204. this.feeArr[this.feePos] += 1;
  205. this.feeArr.push(0);
  206. } else {
  207. this.feeArr.push(1);
  208. this.feePos = i;
  209. }
  210. }
  211. }
  212. },
  213. objectSpanMethod({ row, column, rowIndex, columnIndex }) {
  214. // if (columnIndex === 0) { // 用于设置要合并的列
  215. // if (rowIndex % 2 === 0) { // 用于设置合并开始的行号
  216. // return {
  217. // rowspan: 2, // 合并的行数
  218. // colspan: 1, // 合并的猎术, 设置为0则直接不显示
  219. // };
  220. // }
  221. // return {
  222. // rowspan: 0,
  223. // colspan: 0,
  224. // };
  225. // }
  226. if (columnIndex === 0) {
  227. // 第一列的合并方法
  228. const row1 = this.typeNameArr[rowIndex];
  229. const col1 = row1 > 0 ? 1 : 0; // 如果被合并了row = 0; 则他这个列需要取消
  230. return {
  231. rowspan: row1,
  232. colspan: col1,
  233. };
  234. } else if (columnIndex === 1) {
  235. // 第二列的合并方法
  236. const row2 = this.storeArr[rowIndex];
  237. const col2 = row2 > 0 ? 1 : 0; // 如果被合并了row = 0; 则他这个列需要取消
  238. return {
  239. rowspan: row2,
  240. colspan: col2,
  241. };
  242. } else if (columnIndex === 2) {
  243. // 第三列的合并方法
  244. const row3 = this.feeArr[rowIndex];
  245. const col3 = row3 > 0 ? 1 : 0; // 如果被合并了row = 0; 则他这个列需要取消
  246. return {
  247. rowspan: row3,
  248. colspan: col3,
  249. };
  250. }
  251. },
  252. },
  253. };
  254. </script>
  255. <style lang="less" scoped> </style>

发表评论

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

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

相关阅读