Element-ui表格组件合并单元格方法

4 篇文章 0 订阅

参数说明
spanArr: [], // 合并表格二维数组
rowCols: [‘day’], //需要合并的字段属性数组

  • table表格通用合并方法

table 表格合并 通用方法
list table数据
arr 需要合并的数据列的 prop 数组

export function mergeTableRow(list, arr) {
  let pos = 0
  const len = []
  const spanArr = []
  arr.forEach((col, cIndex) => {
    // 列  第几需要合并的个元素
    const rows = []
    list.forEach((row, rIndex) => {
      // 多少行数据 最大可合并行数
      if (!cIndex) {
        len.push(1)
      }
      if (!rIndex) {
        rows.push(1)
        pos = rIndex
      } else {
        // 是否与上一行 元素值相同
        if (list[rIndex][col] === list[rIndex - 1][col]) {
          rows[pos] += 1
          rows.push(0)
          // 更新第一个元素相同值的个数
          if (!cIndex) {
            len[rIndex] = rows[pos]
          }
          // 超出最大个数 重新赋值
          if (rows[pos] > len[rIndex]) {
            rows[pos] -= 1
            pos = rIndex
            rows[pos] += 1
          }
        } else {
          rows.push(1)
          pos = rIndex
        }
      }
    })
    spanArr.push(rows)
  })
  return spanArr
}
  • 调用合并通用方法
this.spanArr = mergeTableRow(this.tableData, this.rowCols)
    // 表格合并展示
    mergeRowSpan({ row, column, rowIndex, columnIndex }) {
      let index = this.rowCols.findIndex((v) => v === column.property)
      if (
        (index !== -1 && column.property === this.rowCols[index])
      ) {
        if (index === -1) {
          index = 0
        }
        // console.log(this.spanArr[index][rowIndex], '====================spanArr ', column.property)
        const _row = this.spanArr[index][rowIndex]
        const _col = _row ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col
        }
      }
    },
  • 表格使用合并方法
<el-table v-loading="loading"
              :span-method="mergeRowSpan"
              :data="tableData" border stripe
    >
    ...
    </el-table>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值