elementUI表格行列动态合并

需求:总计行合并多列,单位、子公司、生产产线、产品大类四列中相同行动态合并

// 合并单元格
    getSpanArr (data) {
      this.spanArr1 = []
      this.spanArr2 = []
      this.spanArr3 = []
      this.spanArr4 = []
      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          this.spanArr1.push(1)
          this.spanArr2.push(1)
          this.spanArr3.push(1)
          this.spanArr4.push(1)
          this.pos1 = 0
          this.pos2 = 0
          this.pos3 = 0
          this.pos4 = 0
        } else {
          // 判断第二列
          if (data[i].productSaleDept === data[i - 1].productSaleDept) {
            this.spanArr1[this.pos1] += 1
            this.spanArr1.push(0)
          } else {
            this.spanArr1.push(1)
            this.pos1 = i
          }
          // 判断第三列
          if (data[i].productSaleDept === data[i - 1].productSaleDept && data[i].subCode === data[i - 1].subCode) {
            this.spanArr2[this.pos2] += 1
            this.spanArr2.push(0)
          } else {
            this.spanArr2.push(1)
            this.pos2 = i
          }
          // 判断第四列
          if (data[i].productSaleDept === data[i - 1].productSaleDept && data[i].subCode === data[i - 1].subCode && data[i].productLineNumber === data[i - 1].productLineNumber) {
            this.spanArr3[this.pos3] += 1
            this.spanArr3.push(0)
          } else {
            this.spanArr3.push(1)
            this.pos3 = i
          }
          // 判断第五列
          if (data[i].productSaleDept === data[i - 1].productSaleDept && data[i].subCode === data[i - 1].subCode && data[i].productLineNumber === data[i - 1].productLineNumber && data[i].hbisProductTypeNumber === data[i - 1].hbisProductTypeNumber) {
            this.spanArr4[this.pos4] += 1
            this.spanArr4.push(0)
          } else {
            this.spanArr4.push(1)
            this.pos4 = i
          }
        }
      }
    },
    objectSpanMethod ({ row, rowIndex, columnIndex }) {

      // 合并小计
      if (columnIndex === 4 && row.hbisProductTypeNumber === "小计") {
        return [1, 2]
      } else if (columnIndex === 5 && row.hbisProductTypeNumber === "小计") {
        return [0, 0]
      }
      // 合并合计
      if (columnIndex === 3 && row.productLineNumber === "合计") {
        return [1, 3]

      } else if (columnIndex === 4 && row.productLineNumber === "合计") {
        return [0, 0]

      } else if (columnIndex === 5 && row.productLineNumber === "合计") {
        return [0, 0]

      }
      // 合并总计
      if (columnIndex === 2 && row.subCode === "总计") {
        return [1, 4]

      } else if (columnIndex === 3 && row.subCode === "总计") {
        return [0, 0]

      } else if (columnIndex === 4 && row.subCode === "总计") {
        return [0, 0]

      } else if (columnIndex === 5 && row.subCode === "总计") {
        return [0, 0]

      }




      if (columnIndex === 1) {
        const _row = this.spanArr1[rowIndex]
        const _col = _row > 0 ? 1 : 0
        // console.log(`rowspan:${_row} colspan:${_col}`)
        return { // [0,0] 表示这一行不显示, [2,1]表示行的合并数
          rowspan: _row,
          colspan: _col
        }
      }
      if (columnIndex === 2) {
        const _row = this.spanArr2[rowIndex]
        const _col = _row > 0 ? 1 : 0
        // console.log(`rowspan:${_row} colspan:${_col}`)
        return { // [0,0] 表示这一行不显示, [2,1]表示行的合并数
          rowspan: _row,
          colspan: _col
        }
      }
      if (columnIndex === 3) {
        const _row = this.spanArr3[rowIndex]
        const _col = _row > 0 ? 1 : 0
        // console.log(`rowspan:${_row} colspan:${_col}`)
        return { // [0,0] 表示这一行不显示, [2,1]表示行的合并数
          rowspan: _row,
          colspan: _col
        }
      }
      if (columnIndex === 4) {
        const _row = this.spanArr4[rowIndex]
        const _col = _row > 0 ? 1 : 0
        // console.log(`rowspan:${_row} colspan:${_col}`)
        return { // [0,0] 表示这一行不显示, [2,1]表示行的合并数
          rowspan: _row,
          colspan: _col
        }
      }
    },

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值