当前位置: 首页 > article >正文

分组表格antd+ react +ts

import React from "react";
import { Table, Tag } from "antd";
import styles from "./index.less";
import GroupTag from "../Tag";
const GroupTable = () => {
  const columns = [
    {
      title: "姓名",
      dataIndex: "name",
      key: "name",
      render: (text, record) => customRowRender(record, <a>{text}</a>),
      /* 合并单元格列 */
      onCell: (record) => {
        if (record.isGroupTag) {
          return {
            colSpan: 5,
          };
        }
      },
    },
    {
      title: "性别",
      dataIndex: "gender",
      key: "gender",
      render: (text) => text && (text === 0 ? "男" : "女"),
      /* 合并单元格列 */
      onCell: (record) => {
        if (record.isGroupTag) {
          return {
            colSpan: 0,
          };
        }
      },
    },
    {
      title: "年龄",
      dataIndex: "age",
      key: "age",
      /* 合并单元格列 */
      onCell: (record) => {
        if (record.isGroupTag) {
          return {
            colSpan: 0,
          };
        }
      },
    },
    {
      title: "地址",
      dataIndex: "address",
      key: "address",
      /* 合并单元格列 */
      onCell: (record) => {
        if (record.isGroupTag) {
          return {
            colSpan: 0,
          };
        }
      },
    },

    {
      title: "出生日期",
      key: "birthday",
      dataIndex: "birthday",
      /* 合并单元格列 */
      onCell: (record) => {
        if (record.isGroupTag) {
          return {
            colSpan: 0,
          };
        }
      },
    },
  ];

  const data = [
    {
      key: "1",
      title: "家人",
      tagColor: "rgba(10, 196, 72, 1)",
      data: [
        {
          key: "1",
          name: "张三",
          gender: 0,
          age: 32,
          address: "浙江省杭州市余杭区五常街道幸福小区3幢1单元202室",
          birthday: "1991-03-04",
        },
        {
          key: "2",
          name: "李四",
          gender: 1,
          age: 19,
          address: "浙江省杭州市余杭区五常街道幸福小区5幢2单元301室",
          birthday: "2004-01-26",
        },
        {
          key: "3",
          name: "王五",
          gender: 0,
          age: 26,
          address: "浙江省杭州市余杭区五常街道幸福小区7幢3单元503室",
          birthday: "1997-10-17",
        },
      ],
    },
    {
      key: "2",
      title: "朋友",
      tagColor: "rgba(34, 139, 34, 1)",
      data: [
        {
          key: "4",
          name: "张三",
          gender: 0,
          age: 32,
          address: "浙江省杭州市余杭区五常街道幸福小区3幢1单元202室",
          birthday: "1991-03-04",
        },
        {
          key: "5",
          name: "李四",
          gender: 1,
          age: 19,
          address: "浙江省杭州市余杭区五常街道幸福小区5幢2单元301室",
          birthday: "2004-01-26",
        },
        {
          key: "6",
          name: "王五",
          gender: 0,
          age: 26,
          address: "浙江省杭州市余杭区五常街道幸福小区7幢3单元503室",
          birthday: "1997-10-17",
        },
      ],
    },
    {
      key: "3",
      title: "同事",
      tagColor: "rgba(255, 173, 10, 1)",
      data: [
        {
          key: "7",
          name: "张三",
          gender: 0,
          age: 32,
          address: "浙江省杭州市余杭区五常街道幸福小区3幢1单元202室",
          birthday: "1991-03-04",
        },
        {
          key: "8",
          name: "李四",
          gender: 1,
          age: 19,
          address: "浙江省杭州市余杭区五常街道幸福小区5幢2单元301室",
          birthday: "2004-01-26",
        },
        {
          key: "9",
          name: "王五",
          gender: 0,
          age: 26,
          address: "浙江省杭州市余杭区五常街道幸福小区7幢3单元503室",
          birthday: "1997-10-17",
        },
      ],
    },
  ] as any;

  const tableData = data.flatMap((group) => [
    {
      key: `${group.key}-tag`,
      groupTag: group.title,
      tagColor: group.tagColor,
      isGroupTag: true,
    },
    ...group.data.map((item) => ({ ...item, group })),
  ]);

  const customRowRender = (record, children) => {
    console.log(record, "???");
    if (record.isGroupTag) {
      return {
        children: (
          <td colSpan={columns.length}>
            <GroupTag color={record.tagColor} text={record.groupTag} />
            {/* <Tag color={record.tagColor}>{record.groupTag}</Tag> */}
          </td>
        ),
        props: {
          colSpan: columns.length,
        },
      };
    }
    return children;
  };

  return (
    <div>
      <Table
        columns={columns}
        dataSource={tableData}
        pagination={false}
        rowKey={(record) => record.key}
        rowClassName={(record) => (record.isGroupTag ? styles.groupTagRow : "")}
      />
    </div>
  );
};

export default GroupTable;

.groupTagRow {
    :global {
        td.ant-table-cell {
            padding:12px 0px 0px 0px  !important;
            border: unset !important;
        }
    }
}

效果
在这里插入图片描述


http://www.kler.cn/a/517554.html

相关文章:

  • python爬虫入门(一) - requests库与re库,一个简单的爬虫程序
  • K8S中的数据存储之基本存储
  • 【MySQL — 数据库增删改查操作】深入解析MySQL的create insert 操作
  • impact 影响分析学习笔记(一)
  • Python Pandas数据清洗与处理
  • WPF3-在xaml中引用其他程序集的名称空间
  • Python数据分析-数据加载与存储(六)
  • .NET 9.0 的 Blazor Web App 项目、Bootstrap Blazor 组件库、自定义日志 TLog 使用备忘
  • 【开源实录】从App Store审核失败到开源:一个AI辅助开发的SwiftUI项目
  • Docker 在Linux 系统中的使用说明
  • 数据结构与算法再探(六)动态规划
  • AIP-128 声明友好接口
  • Java语言程序设计(第3版) 课后练习
  • 《jEasyUI 动态改变列》
  • 解决lombok注解失效
  • 鸿蒙(HarmonyOS) HDC 命令合集分类记录
  • UDP/TCP ③-拥塞控制 || 滑动窗口 || 流量控制 || 快速重传
  • FineBI数据连接失败
  • 提示词工程(Prompt Engineering)
  • Android多语言开发自动化生成工具
  • Unity3D仿星露谷物语开发25之创建时钟界面
  • 【Rust自学】14.6. 安装二进制crate
  • 「 机器人 」利用数据驱动模型替代仿真器:加速策略训练并降低硬件依赖
  • CDSN 2024博客之星总评选-主题文章创作,我的AI之路-起手篇
  • 青少年CTF练习平台 PHP的XXE
  • 【深度学习|迁移学习】渐进式学习策略 (Progressive Learning Strategy)详述(一)