antd合并表格

const columns = [
        {
            title: '一级指标',
            dataIndex: 'one',
            key: 'one',
            align: 'center',
            render: (text, record) => {
                getTotal(list, 'one')
                const content = (
                    <span>{text}</span>
                );
                const obj = {
                    children: content,
                    props: {},
                };
                if (record.total) {
                    obj.props.rowSpan = record.total;
                } else {
                    obj.props.rowSpan = 0;
                }
                return obj;
            },
        },
        {
            title: '二级指标',
            dataIndex: 'two',
            key: 'two',
            align: 'center',
            render: (text, record, index) => {
                getTotal(list, 'two')
                const content = (
                    <span>{text}</span>
                );
                const obj = {
                    children: content,
                    props: {},
                };
                if (record.total) {
                    obj.props.rowSpan = record.total;
                } else {
                    obj.props.rowSpan = 0;
                }
                return obj;
            },
        },
        {
            title: '三级指标',
            dataIndex: 'three',
            key: 'three',
            align: 'center',
        },
        {
            title: '评价等级',
            dataIndex: 'rate',
            key: 'rate',
            align: 'center',
        },
    ];





/**
 * 表单表头合并
 * @param list 需要遍历的数组
 * @param key  合并依赖的字段名 后端字段必须保证有这么一个字段
 */
//主要赋予两个属性,一个是total:该条数据占几行    number:该条数据的序号
export const getTotal = (list, key) => {
  let number = 0;
  let lastMenber = 0;
  list?.map((item, index) => {
    if (index !== 0 && index !== list.length - 1) {
      if (item[key] != list[index - 1][key]) {
        if (number === 0) {
          //total表示该数据占几行
          list[0].total = index;
          lastMenber = index;
        } else {
          list[lastMenber].total = index - lastMenber;
          lastMenber = index;
        }
        number += 1;
      } else {
        item.total = 0;
      }
    } else if (index === list.length - 1) {
      if (item[key] != list[index - 1][key]) {
        list[lastMenber].total = list.length - 1 - lastMenber;
        item.total = 1;
        lastMenber = 0;
        number += 1;
      } else {
        list[lastMenber].total = list.length - lastMenber;
        lastMenber = 0;
      }
    }
    //number表示序号
    item.number = number;
  });
  return list;
};
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值