当前位置:   article > 正文

element表格 合并行两种方法_el-table 合并行

el-table 合并行

第一种:后端传过来的数据是线性结构,用饿了么的span-method属性来做

效果:

 

 后端给的数据格式

 

 代码:

主要代码 :     给el-table加上span-method="objectSpanMethod" 

html: 

  1. <el-table :data="tableData" :header-cell-style="{ background: '#EFF3F7', height: '50px', color: ' #4D4D4D', fontSize: '14px' }" :cell-style="{ fontSize: '14px' }" border :span-method="objectSpanMethod">
  2. <!-- 序号 -->
  3. <el-table-column label="序号" width="80" align="center" prop="sequenceNo">
  4. </el-table-column>
  5. <el-table-column prop="sequenceNo" align="center" label="托盘/电瓶车编号">
  6. <template slot-scope="{row}">
  7. {{ row.carrierCode }}
  8. </template>
  9. </el-table-column>
  10. <el-table-column prop="containerImage,tireModel,containerCapacity" header-align="center" label="轮胎型号" width="350">
  11. <template slot-scope="{row}">
  12. <div style="display:flex;align-items: center;">
  13. <el-image
  14. style="height: 33px;"
  15. :src="row.containerImage"
  16. :preview-src-list="[row.containerImage]"
  17. fit="contain"></el-image>
  18. <div style="margin-left:10px">
  19. {{ row.tireModel }}-{{ row.containerCapacity }}条
  20. </div>
  21. </div>
  22. </template>
  23. </el-table-column>
  24. <el-table-column prop="actualCapacity,carrierCapacity" align="center" label="箱/袋数">
  25. <template slot-scope="{row}">
  26. <div>
  27. {{ row.actualCapacity }}/{{ row.carrierCapacity }}
  28. </div>
  29. </template>
  30. </el-table-column>
  31. <el-table-column prop="sequenceNo" align="center" label="仓库">
  32. <template slot-scope="{row}">
  33. {{ row.warehouseName }}
  34. </template>
  35. </el-table-column>
  36. <el-table-column prop="sequenceNo" align="center" label="分拣人/分拣线" width="250">
  37. <template slot-scope="{row}">
  38. <div>
  39. {{ row.sorter }} {{ row.sortingLine }}号
  40. </div>
  41. </template>
  42. </el-table-column>
  43. <el-table-column prop="sequenceNo" align="center" label="入库时间">
  44. <template slot-scope="{row}">
  45. {{ row.inboundTime }}
  46. </template>
  47. </el-table-column>
  48. <!-- <el-table-column prop="remainingQuantity" align="center" label="状态">
  49. </el-table-column> -->
  50. <el-table-column align="center" label="操作" prop="sequenceNo">
  51. <template slot-scope="{row}">
  52. <el-button type="text" size="small" style="color: #1156B2;"
  53. v-if="row.carrierType==2"
  54. @click="toPath('/putStorage/info',{carrierCode:row.carrierCode})">详情
  55. </el-button>
  56. </template>
  57. </el-table-column>
  58. </el-table>

js:

  1. var this_;
  2. export default {
  3. data() {
  4. return {
  5. mergeObj: {},
  6. mergeArr: ['sequenceNo'],
  7. tableData: [],
  8. }
  9. },
  10. mounted() {
  11. this_=this
  12. this_.init()
  13. },
  14. methods: {
  15. init(){
  16. let params={
  17. ...this_.page,
  18. status:this_.activeName,
  19. ...this_.searchForm
  20. }
  21. if (this_.activeName=='2') {
  22. delete params['status']
  23. }
  24. if (this_.searchForm.time && this_.searchForm.time.length > 0) {
  25. params['inboundTimeFrom'] = this_.searchForm.time[0] + ' 00:00:00';
  26. params['inboundTimeTo'] = this_.searchForm.time[1] + ' 23:59:59';
  27. delete params.time
  28. }
  29. delete params.total
  30. get_request(url,params).then((res) => {
  31. if(res.data.code==0){
  32. let {current,size,total}=res.data.data
  33. this_.page={current,size,total}
  34. this_.tableData=res.data.data.records
  35. this.getSpanArr(res.data.data.records);
  36. }
  37. }).catch(() => { })
  38. },
  39. getSpanArr(data) {
  40. this.mergeArr.forEach((key, index1) => {
  41. let count = 0; // 用来记录需要合并行的起始位置
  42. this.mergeObj[key] = []; // 记录每一列的合并信息
  43. data.forEach((item, index) => {
  44. // index == 0表示数据为第一行,直接 push 一个 1
  45. if(index === 0) {
  46. this.mergeObj[key].push(1);
  47. } else {
  48. // 判断当前行是否与上一行其值相等 如果相等 在 count 记录的位置其值 +1 表示当前行需要合并 并push 一个 0 作为占位
  49. if(item[key] === data[index - 1][key]) {
  50. this.mergeObj[key][count] += 1;
  51. this.mergeObj[key].push(0);
  52. } else {
  53. // 如果当前行和上一行其值不相等
  54. count = index; // 记录当前位置
  55. this.mergeObj[key].push(1); // 重新push 一个 1
  56. }
  57. }
  58. })
  59. })
  60. },
  61. objectSpanMethod({ row, column, rowIndex, columnIndex }) {
  62. // 判断列的属性
  63. if(this.mergeArr.indexOf(column.property) !== -1) {
  64. // 判断其值是不是为0
  65. if(this.mergeObj[column.property][rowIndex]) {
  66. return [this.mergeObj[column.property][rowIndex], 1]
  67. } else {
  68. // 如果为0则为需要合并的行
  69. return [0, 0];
  70. }
  71. }
  72. },
  73. },
  74. }

第二种:后端传过来的数据是树形结构,主要用css来做

效果:

 后端给的数据格式:

 

 代码:

html:

  1. <el-table :data="tableData" :header-cell-style="{ background: '#EFF3F7', height: '50px', color: ' #4D4D4D', fontSize: '14px' }" :cell-style="{ fontSize: '14px' }" border>
  2. <!-- 序号 -->
  3. <el-table-column label="序号" width="80" align="center" type="index">
  4. </el-table-column>
  5. <el-table-column prop="carrierCodeNew" align="center" label="新托盘编码">
  6. </el-table-column>
  7. <el-table-column prop="carrierCodeOld" align="center" label="原托盘编码">
  8. </el-table-column>
  9. <el-table-column prop="changeDetailList" align="center" label="轮胎型号">
  10. <template slot-scope="{row}">
  11. <div v-for="(item,index) in row.changeDetailList" :key="index"
  12. :class="row.changeDetailList.length > 0 && index != 0 ? 'hxwd_table_item_left' : 'hxwd_table_item_left_first'">
  13. <el-image
  14. style="height: 33px;"
  15. :src="item.containerImage"
  16. :preview-src-list="[item.containerImage]"
  17. fit="contain"></el-image>
  18. <div style="margin-left:10px">
  19. {{ item.goodsModel }}-{{ item.capacity }}条
  20. </div>
  21. </div>
  22. </template>
  23. </el-table-column>
  24. <el-table-column prop="changeDetailList" align="center" label="条数">
  25. <template slot-scope="{row}">
  26. <div v-for="(item,index) in row.changeDetailList" :key="index"
  27. :class="row.changeDetailList.length > 0 && index != 0 ? 'hxwd_table_item_center' : 'hxwd_table_item_center_first'">
  28. {{ item.goodsCnt }}
  29. </div>
  30. </template>
  31. </el-table-column>
  32. <el-table-column prop="warehouseCode" align="center" label="仓库">
  33. </el-table-column>
  34. <el-table-column prop="createdTime" align="center" label="更换时间">
  35. </el-table-column>
  36. </el-table>

css:

.hxwd_table_item_center_first  .hxwd_table_item_center   表格内容居中

.hxwd_table_item_left_first  .hxwd_table_item_left        表格内容居左

  1. .hxwd_table_item_center_first {padding-left: 10px;margin-left: -10px;margin-right: -10px;padding-right: 10px;min-height: 50px;display: flex;align-items: center;justify-content: center;}
  2. .hxwd_table_item_center {padding-left: 10px;margin-left: -10px;margin-right: -10px;padding-right: 10px;min-height: 50px;display: flex;align-items: center;justify-content: center;border-top: 1px solid #EAEAEA;}
  3. .hxwd_table_item_left_first {padding-left: 10px;margin-left: -10px;margin-right: -10px;padding-right: 10px;min-height: 50px;display: flex;align-items: center;}
  4. .hxwd_table_item_left {padding-left: 10px;margin-left: -10px;margin-right: -10px;padding-right: 10px;min-height: 50px;display: flex;align-items: center;border-top: 1px solid #EAEAEA;}

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

闽ICP备14008679号