w
import React, { useState, useMemo } from 'react';
import { Table, Button } from 'antd';
import { DownOutlined, RightOutlined } from '@ant-design/icons';
// ---------- 唯一 key 生成器 ----------
let childSeq = 0;
const newKey = () => `child-${Date.now()}-${childSeq++}`;
// ---------- 重算合计 ----------
const recalcSummary = (person) => {
const sum = (f) => (person.children || []).reduce((s, d) => s + (Number(d[f]) || 0), 0);
return {
...person,
jul: sum('jul'),
aug: sum('aug'),
sep: sum('sep'),
total: sum('total'),
};
};
// ---------- 初始数据 ----------
const initialData = [
{
key: 'person-1',
name: '张三',
isSummary: true,
jul: 300, aug: 400, sep: 500, total: 1200,
children: [
{ key: newKey(), dept: 'A部门', jul: 100, aug: 150, sep: 200, total: 450 },
{ key: newKey(), dept: 'B部门', jul: 200, aug: 250, sep: 300, total: 750 },
],
},
{
key: 'person-2',
name: '李四',
isSummary: true,
jul: 0, aug: 0, sep: 0, total: 0,
children: [],
},
];
export default function SummaryTable() {
const [data, setData] = useState(initialData);
