自定义span-method方法达到合并表格目的getSpanArr方法 重置tableData数据格式arraySpanMethod 合并方法cellClassName 定义单元格类名tempList 储存添加过的字段getSpanArr() { let newTa..._el-table span-method">
当前位置:   article > 正文

el-table之合并表格自定义span-method_el-table span-method

el-table span-method

 

<el-table ref="tableList"  :data="tableData"  :span-method="arraySpanMethod" ></el-table>

自定义span-method方法达到合并表格目的

getSpanArr方法 重置tableData数据格式

arraySpanMethod 合并方法

cellClassName  定义单元格类名

tempList 储存添加过的字段

  1. getSpanArr() {
  2. let newTabelData=[]
  3. for (let index = 0; index < this.tableData.length; index++) {
  4. const element = this.tableData[index];
  5. for (let grindIndex = 0; grindIndex < (element.grindsStones.length||1); grindIndex++) {
  6. const grindElement = element.grindsStones[grindIndex]||{steelInfos:[]};
  7. for (let steelIndex = 0; steelIndex < (grindElement.steelInfos.length||1); steelIndex++) {
  8. const steelElement = grindElement.steelInfos[steelIndex]||{};
  9. let obj={
  10. objectId:element.objectId||'',
  11. deviceCode:element.deviceCode||'',
  12. deviceName:element.deviceName||'',
  13. deviceRowspan:element.rowSpan,
  14. name:grindElement.name||'',
  15. code:grindElement.code||'',
  16. nameRowspan:grindElement.rowSpan,
  17. gyscj:grindElement.gyscj||'',
  18. slld:grindElement.slld||'',
  19. sysl:grindElement.sysl||'',
  20. steelName:steelElement.steelName||'',
  21. steelCode:steelElement.steelCode||'',
  22. steelRowspan:steelElement.rowSpan,
  23. formatSteelWeight:steelElement.formatSteelWeight||'',
  24. formatConversionWeight:steelElement.formatConversionWeight||'',
  25. formatSandingWeight:steelElement.formatSandingWeight||'',
  26. formatSandingArea:steelElement.formatSandingArea||''
  27. }
  28. newTabelData.push(obj)
  29. }
  30. }
  31. }
  32. this.tableData=newTabelData
  33. this.$nextTick(() => {
  34. tempList=[]
  35. this.$refs.tableList.doLayout()
  36. })
  37. },
  38. arraySpanMethod({ row, column, rowIndex, columnIndex }) {
  39. let deviceCode=row.deviceCode
  40. let objectId=row.objectId
  41. let deviceName=row.deviceName
  42. let code=objectId+row.code
  43. //按数组顺序取值拼接保证每一行数据不重复
  44. let name='name'+objectId+code+row.name
  45. let gyscj='gyscj'+objectId+code+row.gyscj
  46. let slld='slld'+objectId+code+row.slld
  47. let sysl='sysl'+objectId+code+row.sysl
  48. let len=tempList.filter((item)=>{return item==deviceCode}).length
  49. let lendeviceName=tempList.filter((item)=>{return item==deviceName}).length
  50. let lenname=tempList.filter((item)=>{return item==name}).length
  51. let lengyscj=tempList.filter((item)=>{return item==gyscj}).length
  52. let lenslld=tempList.filter((item)=>{return item==slld}).length
  53. let lensysl=tempList.filter((item)=>{return item==sysl}).length
  54. if(columnIndex==0&&rowIndex==this.tableData.length-1){
  55. // 第一列 最后一行 合计 合并
  56. return {
  57. rowspan: 1,
  58. colspan: 5
  59. };
  60. }else if(rowIndex==this.tableData.length-1&&columnIndex<5){
  61. //最后一行 被合计 合并的设置0,0
  62. return {
  63. rowspan: 0,
  64. colspan: 0
  65. };
  66. }else{
  67. if(columnIndex==0&&len==0){
  68. //第一列 首次 合并列数
  69. tempList.push(deviceCode)
  70. return {
  71. rowspan: row.deviceRowspan,
  72. colspan: 1
  73. };
  74. }else if(columnIndex==0&&len<row.deviceRowspan){
  75. //第一列 合并列之外归零
  76. tempList.push(deviceCode)
  77. return {
  78. rowspan: 0,
  79. colspan: 0
  80. };
  81. }
  82. if(columnIndex==1&&lendeviceName==0){//第二列 合并
  83. tempList.push(deviceName)
  84. return {
  85. rowspan: row.deviceRowspan,
  86. colspan: 1
  87. };
  88. }else if(columnIndex==1&&lendeviceName<row.deviceRowspan){
  89. tempList.push(deviceName)
  90. return {
  91. rowspan: 0,
  92. colspan: 0
  93. };
  94. }
  95. if(columnIndex==2&&lenname==0){
  96. tempList.push(name)
  97. return {
  98. rowspan: row.nameRowspan,
  99. colspan: 1
  100. };
  101. }else if(columnIndex==2&&lenname<row.nameRowspan){
  102. tempList.push(name)
  103. return {
  104. rowspan: 0,
  105. colspan: 0
  106. };
  107. }
  108. if(columnIndex==3&&lengyscj==0){
  109. tempList.push(gyscj)
  110. return {
  111. rowspan: row.nameRowspan,
  112. colspan: 1
  113. };
  114. }else if(columnIndex==3&&lengyscj<row.nameRowspan){
  115. tempList.push(gyscj)
  116. return {
  117. rowspan: 0,
  118. colspan: 0
  119. };
  120. }
  121. if(columnIndex==4&&lenslld==0){
  122. tempList.push(slld)
  123. return {
  124. rowspan: row.nameRowspan,
  125. colspan: 1
  126. };
  127. }else if(columnIndex==4&&lenslld<row.nameRowspan){
  128. tempList.push(slld)
  129. return {
  130. rowspan: 0,
  131. colspan: 0
  132. };
  133. }
  134. if(columnIndex==5&&lensysl==0){
  135. tempList.push(sysl)
  136. return {
  137. rowspan: row.nameRowspan,
  138. colspan: 1
  139. };
  140. }else if(columnIndex==5&&lensysl<row.nameRowspan){
  141. tempList.push(sysl)
  142. return {
  143. rowspan: 0,
  144. colspan: 0
  145. };
  146. }
  147. }
  148. },
  149. cellClassName({ row, column, rowIndex, columnIndex }){
  150. if(row.deviceCode=='合计'){
  151. return 'emptyClassColor'
  152. }
  153. if(columnIndex >5&&row.steelName=='合计'){
  154. return 'emptyClass'
  155. }
  156. },

文档表格导出方法

  1. exportList() {
  2. //导出表头信息
  3. let excelArr = [
  4. ["设备编号",
  5. "设备名称",
  6. "砂轮种类",
  7. "厂家",
  8. "粒度",
  9. "数量(片)",
  10. "钢种名称",
  11. "产量(吨)",
  12. "折算重量(吨)",
  13. "磨削量(吨)",
  14. "修磨面积(m²)"
  15. ]
  16. ];
  17. let mergeArr=[],tempLIst=[];//合并数组 储存合并值
  18. this.tableData.forEach((item,rowIndex) => {
  19. //对应字典值
  20. excelArr.push([
  21. item.deviceCode,
  22. item.deviceName,
  23. item.name,
  24. item.gyscj,
  25. item.slld,
  26. item.sysl,
  27. item.steelName,
  28. item.formatSteelWeight,
  29. item.formatConversionWeight,
  30. item.formatSandingWeight,
  31. item.formatSandingArea,
  32. ]);
  33. if(!tempLIst.includes('objectId'+item.objectId)){
  34. // 未包含 添加id 合并开始
  35. tempLIst.push('objectId'+item.objectId)
  36. if(item.deviceCode=='合计'){
  37. mergeArr.push({ s: { r: rowIndex+1, c: 0 }, e: { r: (rowIndex+1), c: 4 } })
  38. }else{
  39. mergeArr.push({ s: { r: rowIndex+1, c: 0 }, e: { r: (rowIndex+item.deviceRowspan), c: 0 } })
  40. mergeArr.push({ s: { r: rowIndex+1, c: 1 }, e: { r: (rowIndex+item.deviceRowspan), c: 1 } })
  41. }
  42. }
  43. if(!tempLIst.includes('name'+item.objectId+item.code+item.name)&&item.deviceCode!=='合计'){
  44. tempLIst.push('name'+item.objectId+item.code+item.name)
  45. mergeArr.push({ s: { r: rowIndex+1, c: 2 }, e: { r: (rowIndex+item.nameRowspan), c: 2 } })
  46. mergeArr.push({ s: { r: rowIndex+1, c: 3 }, e: { r: (rowIndex+item.nameRowspan), c: 3 } })
  47. mergeArr.push({ s: { r: rowIndex+1, c: 4 }, e: { r: (rowIndex+item.nameRowspan), c: 4 } })
  48. mergeArr.push({ s: { r: rowIndex+1, c: 5 }, e: { r: (rowIndex+item.nameRowspan), c: 5 } })
  49. }
  50. });
  51. this.$outputXlsxFile(
  52. excelArr,
  53. [
  54. {wch: 30},
  55. {wch: 30},
  56. {wch: 30},
  57. {wch: 30},
  58. {wch: 15},
  59. {wch: 10},
  60. {wch: 10},
  61. {wch: 15},
  62. {wch: 15},
  63. {wch: 15},
  64. {wch: 15},
  65. ],
  66. "耗材统计",
  67. mergeArr
  68. );
  69. },

main.js中配置 xlsx导出配置

  1. import XLSX from 'xlsx'
  2. const outputXlsxFile = (data, wscols, xlsxName) => {
  3. /* convert state to workbook */
  4. const ws = XLSX.utils.aoa_to_sheet(data);
  5. ws['!cols'] = wscols;
  6. const wb = XLSX.utils.book_new();
  7. XLSX.utils.book_append_sheet(wb, ws, xlsxName);
  8. /* generate file and send to client */
  9. XLSX.writeFile(wb, xlsxName + ".xlsx");
  10. }
  11. Vue.prototype.$outputXlsxFile = outputXlsxFile;

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

闽ICP备14008679号