element表格的多层级合并

两种方法:

       第一种通过数据的某个字段进行合并

       第二种根据行来合并

第一种效果如下

    <el-table
      ref="myTable"
      :span-method="objectSpanMethod"
      :data="tableData"
      border
    >
      <el-table-column
        :label="$t('global.process')"
        prop="process"
        align="center"
        width="120"
      />
      <el-table-column
        :label="$t('global.crew')"
        prop="crew"
        align="center"
        width="120"
      />
      <el-table-column
        :label="$t('global.productionPlanNumber')"
        align="center"
        prop="productionPlanNumber"
        width="140"
      />
      <el-table-column
        :label="$t('global.station')+'及'+$t('global.drillingCrew')"
        prop="stationAndDrillingCrew"
        align="center"
        width="120"
      />
      <el-table-column
        :label="$t('global.productName')"
        prop="productName"
        align="center"
        width="150"
      />
      <el-table-column
        :label="$t('global.productCode')"
        prop="productCode"
        align="center"
        width="150"
      />
      <el-table-column
        :label="$t('global.planToDispatch')"
        prop="planToDispatch"
        align="center"
        width="150"
      />
      <el-table-column
        :label="$t('global.actualProfit')"
        prop="actualProfit"
        align="center"
        width="150"
      />
      <el-table-column
        :label="$t('global.FPY')"
        prop="FPY"
        align="center"
        width="150"
      />
      <el-table-column
        :label="$t('global.badnessNum')"
        prop="badnessNum"
        align="center"
        width="150"
      />
      <el-table-column
        :label="$t('global.reworkNum')"
        prop="reworkNum"
        align="center"
        width="150"
      />
      <el-table-column
        :label="$t('global.scrapNum')"
        prop="scrapNum"
        align="center"
        width="150"
      />
      <el-table-column
        :label="$t('global.univalenceManhour')"
        prop="univalenceManhour"
        align="center"
        width="150"
      />
      <el-table-column
        :label="$t('global.crossClaim')"
        prop="crossClaim"
        align="center"
        width="150"
      />
      <el-table-column
        :label="$t('global.realWages')"
        prop="realWages"
        align="center"
        width="150"
      />
      <el-table-column
        :label="$t('global.summary')"
        prop="summary"
        align="center"
        width="150"
      />
    </el-table>

js代码:

data() {
    return {
      tableData: [
        {
          grandpaId: 1,
          fatherId: 11,
          id: 11,
          process: '工序一',
          crew: '啊切 ',
          productionPlanNumber: 'A0001',
          stationAndDrillingCrew: '工位一 机台二',
          productName: '测试demo',
          productCode: 'ABC001',
          planToDispatch: '100',
          actualProfit: '88',
          FPY: '88',
          badnessNum: '2',
          reworkNum: '5',
          scrapNum: '0',
          univalenceManhour: '100',
          crossClaim: '20',
          realWages: '600',
          summary: '600'
        },
        {
          grandpaId: 1,
          fatherId: 11,
          id: 112,
          process: '工序二',
          crew: '啊切 ',
          productionPlanNumber: 'A0001',
          stationAndDrillingCrew: '工位二 机台三',
          productName: '测试demo1',
          productCode: 'ABC001',
          planToDispatch: '100',
          actualProfit: '88',
          FPY: '88',
          badnessNum: '2',
          reworkNum: '5',
          scrapNum: '0',
          univalenceManhour: '100',
          crossClaim: '20',
          realWages: '600',
          summary: '600'
        },
        {
          grandpaId: 1,
          fatherId: 11,
          id: 113,
          process: '工序一',
          crew: '啊切 ',
          productionPlanNumber: 'A0001',
          stationAndDrillingCrew: '工位三 机台四',
          productName: '测试demo',
          productCode: 'ABC001',
          planToDispatch: '100',
          actualProfit: '88',
          FPY: '88',
          badnessNum: '2',
          reworkNum: '5',
          scrapNum: '0',
          univalenceManhour: '100',
          crossClaim: '20',
          realWages: '600',
          summary: '600'
        },
        {
          grandpaId: 1,
          fatherId: 11,
          id: 114,
          process: '工序一',
          crew: '啊切 ',
          productionPlanNumber: 'A0001',
          stationAndDrillingCrew: '工位一 机台二',
          productName: '测试demo',
          productCode: 'ABC001',
          planToDispatch: '100',
          actualProfit: '88',
          FPY: '88',
          badnessNum: '2',
          reworkNum: '5',
          scrapNum: '0',
          univalenceManhour: '100',
          crossClaim: '20',
          realWages: '600',
          summary: '600'
        },
        {
          grandpaId: 1,
          fatherId: 12,
          id: 115,
          process: '工序一',
          crew: '啊切 ',
          productionPlanNumber: 'A0001',
          stationAndDrillingCrew: '工位二 机台三',
          productName: '测试demo1',
          productCode: 'ABC001',
          planToDispatch: '100',
          actualProfit: '88',
          FPY: '88',
          badnessNum: '2',
          reworkNum: '5',
          scrapNum: '0',
          univalenceManhour: '100',
          crossClaim: '20',
          realWages: '600',
          summary: '600'
        },
        {
          grandpaId: 1,
          fatherId: 12,
          id: 116,
          process: '工序一',
          crew: '啊切 ',
          productionPlanNumber: 'A0001',
          stationAndDrillingCrew: '工位三 机台四',
          productName: '测试demo',
          productCode: 'ABC001',
          planToDispatch: '100',
          actualProfit: '88',
          FPY: '88',
          badnessNum: '2',
          reworkNum: '5',
          scrapNum: '0',
          univalenceManhour: '100',
          crossClaim: '20',
          realWages: '600',
          summary: '600'
        },
        {
          grandpaId: 1,
          fatherId: 12,
          id: 117,
          process: '工序一',
          crew: '啊切 ',
          productionPlanNumber: 'A0001',
          stationAndDrillingCrew: '工位一 机台二',
          productName: '测试demo',
          productCode: 'ABC001',
          planToDispatch: '100',
          actualProfit: '88',
          FPY: '88',
          badnessNum: '2',
          reworkNum: '5',
          scrapNum: '0',
          univalenceManhour: '100',
          crossClaim: '20',
          realWages: '600',
          summary: '600'
        },
        {
          grandpaId: 1,
          fatherId: 12,
          id: 118,
          process: '工序一',
          crew: '啊切 111',
          productionPlanNumber: 'A00011',
          stationAndDrillingCrew: '工位二 机台三',
          productName: '测试demo1',
          productCode: 'ABC001111',
          planToDispatch: '100',
          actualProfit: '88',
          FPY: '88',
          badnessNum: '2',
          reworkNum: '5',
          scrapNum: '0',
          univalenceManhour: '100',
          crossClaim: '20',
          realWages: '600',
          summary: '600'
        },
        {
          grandpaId: 2,
          fatherId: 13,
          id: 119,
          process: '工序二',
          crew: '啊切 ',
          productionPlanNumber: 'A0001',
          stationAndDrillingCrew: '工位三 机台四',
          productName: '测试demo',
          productCode: 'ABC001',
          planToDispatch: '100',
          actualProfit: '88',
          FPY: '88',
          badnessNum: '2',
          reworkNum: '5',
          scrapNum: '0',
          univalenceManhour: '100',
          crossClaim: '20',
          realWages: '600',
          summary: '600'
        },
        {
          grandpaId: 2,
          fatherId: 13,
          id: 1110,
          process: '工序二',
          crew: '啊切 ',
          productionPlanNumber: 'A0001',
          stationAndDrillingCrew: '工位一 机台二',
          productName: '测试demo',
          productCode: 'ABC001',
          planToDispatch: '100',
          actualProfit: '88',
          FPY: '88',
          badnessNum: '2',
          reworkNum: '5',
          scrapNum: '0',
          univalenceManhour: '100',
          crossClaim: '20',
          realWages: '600',
          summary: '600'
        },
        {
          grandpaId: 2,
          fatherId: 13,
          id: 1111,
          process: '工序二',
          crew: '啊切 ',
          productionPlanNumber: 'A0001',
          stationAndDrillingCrew: '工位二 机台三',
          productName: '测试demo1',
          productCode: 'ABC001',
          planToDispatch: '100',
          actualProfit: '88',
          FPY: '88',
          badnessNum: '2',
          reworkNum: '5',
          scrapNum: '0',
          univalenceManhour: '100',
          crossClaim: '20',
          realWages: '600',
          summary: '600'
        },
        {
          grandpaId: 2,
          fatherId: 13,
          id: 1112,
          process: '工序二',
          crew: '啊切 ',
          productionPlanNumber: 'A0001',
          stationAndDrillingCrew: '工位三 机台四',
          productName: '测试demo',
          productCode: 'ABC001',
          planToDispatch: '100',
          actualProfit: '88',
          FPY: '88',
          badnessNum: '2',
          reworkNum: '5',
          scrapNum: '0',
          univalenceManhour: '100',
          crossClaim: '20',
          realWages: '600',
          summary: '600'
        },
        {
          grandpaId: 2,
          fatherId: 14,
          id: 1113,
          process: '工序二',
          crew: '啊切 ',
          productionPlanNumber: 'A0001',
          stationAndDrillingCrew: '工位一 机台二',
          productName: '测试demo',
          productCode: 'ABC001',
          planToDispatch: '100',
          actualProfit: '88',
          FPY: '88',
          badnessNum: '2',
          reworkNum: '5',
          scrapNum: '0',
          univalenceManhour: '100',
          crossClaim: '20',
          realWages: '600',
          summary: '600'
        },
        {
          grandpaId: 2,
          fatherId: 14,
          id: 1114,
          process: '工序二',
          crew: '啊切 ',
          productionPlanNumber: 'A0001',
          stationAndDrillingCrew: '工位二 机台三',
          productName: '测试demo1',
          productCode: 'ABC001',
          planToDispatch: '100',
          actualProfit: '88',
          FPY: '88',
          badnessNum: '2',
          reworkNum: '5',
          scrapNum: '0',
          univalenceManhour: '100',
          crossClaim: '20',
          realWages: '600',
          summary: '600'
        },
        {
          grandpaId: 2,
          fatherId: 14,
          id: 1115,
          process: '工序二',
          crew: '啊切 ',
          productionPlanNumber: 'A0001',
          stationAndDrillingCrew: '工位三 机台四',
          productName: '测试demo',
          productCode: 'ABC001',
          planToDispatch: '100',
          actualProfit: '88',
          FPY: '88',
          badnessNum: '2',
          reworkNum: '5',
          scrapNum: '0',
          univalenceManhour: '100',
          crossClaim: '20',
          realWages: '600',
          summary: '600'
        }
      ]
    };
  },
  mounted() {
    this.setrowspans(this.tableData);
  },
  // 方法集合
  methods: {
    setrowspans() {
      // 先给所有的数据都加一个v.rowspan = 1
      this.tableData.forEach(v => {
        v.frowspan = 1;
        v.growspan = 1;
      });
      // 双层循环
      for (let i = 0; i < this.tableData.length; i++) {
        // 内层循环,上面已经给所有的行都加了v.rowspan = 1
        // 这里进行判断
        // 如果当前行的id和下一行的id相等
        // 就把当前v.rowspan + 1
        // 下一行的v.rowspan - 1
        for (let j = i + 1; j < this.tableData.length; j++) {
          // 此处可根据相同字段进行合并,此处是根据的id
          if (this.tableData[i].grandpaId === this.tableData[j].grandpaId) {
            this.tableData[i].growspan++;
            this.tableData[j].growspan--;
          }
        }
        // 这里跳过已经重复的数据
        i = i + this.tableData[i].growspan - 1;
      }
      for (let i = 0; i < this.tableData.length; i++) {
        // 内层循环,上面已经给所有的行都加了v.rowspan = 1
        // 这里进行判断
        // 如果当前行的id和下一行的id相等
        // 就把当前v.rowspan + 1
        // 下一行的v.rowspan - 1
        for (let j = i + 1; j < this.tableData.length; j++) {
          // 此处可根据相同字段进行合并,此处是根据的id
          if (this.tableData[i].fatherId === this.tableData[j].fatherId) {
            this.tableData[i].frowspan++;
            this.tableData[j].frowspan--;
          }
        }
        // 这里跳过已经重复的数据
        i = i + this.tableData[i].frowspan - 1;
      }
      console.log(this.tableData);
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex < 3) {
        return {
          rowspan: row.growspan,
          colspan: 1
        };
      }
      if (columnIndex > 2 && columnIndex < 6) {
        return {
          rowspan: row.frowspan,
          colspan: 1
        };
      }
    }
  }

第二种效果如下

代码如下:

html:

<el-table
      :data="tableData"
      :span-method="objectSpanMethod"
      border
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column
        prop="id"
        label="ID"
        width="180"
      />
      <el-table-column
        prop="name"
        label="姓名"
      />
      <el-table-column
        prop="amount1"
        label="数值 1(元)"
      />
      <el-table-column
        prop="amount2"
        label="数值 2(元)"
      />
      <el-table-column
        prop="amount3"
        label="数值 3(元)"
      />
      <el-table-column
        prop="amount1"
        label="数值 1(元)"
      />
      <el-table-column
        prop="amount2"
        label="数值 2(元)"
      />
      <el-table-column
        prop="amount3"
        label="数值 3(元)"
      />
      <el-table-column
        prop="amount1"
        label="数值 1(元)"
      />
      <el-table-column
        prop="amount2"
        label="数值 2(元)"
      />
      <el-table-column
        prop="amount3"
        label="数值 3(元)"
      />
      <el-table-column
        prop="amount1"
        label="数值 1(元)"
      />
      <el-table-column
        prop="amount2"
        label="数值 2(元)"
      />
      <el-table-column
        prop="amount3"
        label="数值 3(元)"
      />
      <el-table-column
        prop="amount1"
        label="数值 1(元)"
      />
      <el-table-column
        prop="amount2"
        label="数值 2(元)"
      />
      <el-table-column
        prop="amount3"
        label="数值 3(元)"
      />
      <el-table-column
        prop="amount1"
        label="数值 1(元)"
      />
      <el-table-column
        prop="amount2"
        label="数值 2(元)"
      />
      <el-table-column
        prop="amount3"
        label="数值 3(元)"
      />
      <el-table-column
        prop="amount1"
        label="数值 1(元)"
      />
      <el-table-column
        prop="amount2"
        label="数值 2(元)"
      />
    </el-table>

js:

 data() {
    return {
      tableData: [
        {
          id: '12987122',
          name: '王小虎',
          amount1: '234',
          amount2: '3.2',
          amount3: 10
        },
        {
          id: '12987123',
          name: '王小虎',
          amount1: '165',
          amount2: '4.43',
          amount3: 12
        },
        {
          id: '12987124',
          name: '王小虎',
          amount1: '324',
          amount2: '1.9',
          amount3: 9
        },
        {
          id: '12987125',
          name: '王小虎',
          amount1: '621',
          amount2: '2.2',
          amount3: 17
        },
        {
          id: '12987126',
          name: '王小虎',
          amount1: '539',
          amount2: '4.1',
          amount3: 15
        },
        {
          id: '12987123',
          name: '王小虎',
          amount1: '165',
          amount2: '4.43',
          amount3: 12
        },
        {
          id: '12987124',
          name: '王小虎',
          amount1: '324',
          amount2: '1.9',
          amount3: 9
        },
        {
          id: '12987125',
          name: '王小虎',
          amount1: '621',
          amount2: '2.2',
          amount3: 17
        },
        {
          id: '12987126',
          name: '王小虎',
          amount1: '539',
          amount2: '4.1',
          amount3: 15
        },
        {
          id: '12987123',
          name: '王小虎',
          amount1: '165',
          amount2: '4.43',
          amount3: 12
        },
        {
          id: '12987124',
          name: '王小虎',
          amount1: '324',
          amount2: '1.9',
          amount3: 9
        },
        {
          id: '12987125',
          name: '王小虎',
          amount1: '621',
          amount2: '2.2',
          amount3: 17
        },
        {
          id: '12987126',
          name: '王小虎',
          amount1: '539',
          amount2: '4.1',
          amount3: 15
        }
      ]
    };
  },
  // 方法集合
  methods: {
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex < 4) {
        if (rowIndex % 8 === 0) {
          return {
            rowspan: 8,
            colspan: 1
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0
          };
        }
      } else if (columnIndex < 7) {
        if (rowIndex % 4 === 0) {
          return {
            rowspan: 4,
            colspan: 1
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0
          };
        }
      } else if (columnIndex < 11) {
        if (rowIndex % 2 === 0) {
          return {
            rowspan: 2,
            colspan: 1
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0
          };
        }
      }
    }
  }

  data() {
    return {
      isOpen: false,
      ottId: null,
      // 默认查询已审核与已评审的
      parameter: { ep: 'millMeritsApplyFor' },
      plateArr: [],
      wpArr: [],
      pos: 0
    };
  },



 successTable(res) {
      this.plateArr = [];
      this.wpArr = [];
      for (var i = 0; i < res.list.length; i++) {
        if (i === 0) {
          this.plateArr.push(1);
          this.wpArr.push(1);
          this.pos = 0;
        } else {
          if (res.list[i].plate === res.list[i - 1].plate) {
            this.plateArr[this.pos] += 1;
            this.plateArr.push(0);
          } else {
            this.plateArr.push(1);
            this.pos = i;
          }
        }
      }
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0 || columnIndex === 1) {
        const _row = this.plateArr[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col
        };
      }
    },

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值