element 合并单元格方法

你的数据是需要排列好的, 把所有一样的数据都排序到一起

// 获取需要合并的位置
const getSpanNumber = (data: User[], prop: string) => {
  const length = data.length
  if (length > 0) {
    let position = 0
    let temp = data[0][prop]
    const result = [1]
    for (let i = 1; i < length; i++) {
      if (data[i][prop] === temp) {
        result[position] += 1
        result[i] = 0
      } else {
        position = i
        result[i] = 1
        temp = data[i][prop]
      }
    }
    return result
  }

  return [0]
}

const objectSpanMethod = ({
  row,
  column,
  rowIndex,
  columnIndex,
}: SpanMethodProps) => {
  if (columnIndex === 0) {
    const nameSpan = getSpanNumber(tableData, 'classId')
    return {
      rowspan: nameSpan[rowIndex],
      colspan: 1,
    }
  }
  if (columnIndex === 6) {
    const nameSpan = getSpanNumber(tableData, 'classId')
    return {
      rowspan: nameSpan[rowIndex],
      colspan: 1,
    }
  }

  if (columnIndex === 5) {
    const nameSpan = getSpanNumber(tableData, 'classId')
    return {
      rowspan: nameSpan[rowIndex],
      colspan: 1,
    }
  }
}

如果手动去判断合并单元格则需要把被占位的单元格的空间设置为 0(此处是合并列)


// 这里合并了 第一列名为 本章均值 , 此处占位两列也就是说把第二列的空间占用了
  if (columnIndex === 0) {
        console.log(column);

        if (row.sectionName == '本章均值') {
            return {
                rowspan: 1,
                colspan: 2
            }
        }
    }
  // 这里就要把第二列名为 本章均值 设置为 0 因为他的空间被上面的占走了
  if (columnIndex === 1) {
        if (row.sectionName == '本章均值') {
            return {
                rowspan: 1,
                colspan: 0
            }
        }
    }
    
    return {
        rowspan: 1,
        colspan: 1
    }

posted @   阿臻  阅读(43)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2020-04-27 浏览器页面样式去缓存(包括微信浏览器)
点击右上角即可分享
微信分享提示