You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
salary-management-oneself/src/pages/reportTable/index.tsx

151 lines
5.5 KiB
TypeScript

import React, { FC, useEffect, useState } from "react";
import { Button, Table, Typography } from "antd";
12 months ago
import { exceptStr, paginationAction } from "@/utils/common";
import styles from "@/pages/atdTable/components/index.less";
12 months ago
import { PaginationData } from "rc-pagination";
1 year ago
const { Text, Paragraph } = Typography;
const ReportTable: FC = (props) => {
const [columns, setColumns] = useState<Array<any>>([]);
const [sumRow, setSumRow] = useState<Partial<{}>>({});
const [dataSource, setDataSource] = useState<Array<any>>([]);
const [showSumrow, setShowSumrow] = useState<boolean>(false);
1 year ago
const [SSHeaderInfo, setSSHeaderInfo] = useState<string>("");
12 months ago
const [pageInfo, setPageInfo] = useState<Partial<PaginationData>>({});
const [i18n, setI18n] = useState<any>({});
useEffect(() => {
window.parent.postMessage({ type: "init" }, "*");
window.addEventListener("message", receiveMessageFromIndex, false);
return () => {
window.removeEventListener("message", receiveMessageFromIndex, false);
};
}, []);
const receiveMessageFromIndex = (event: any) => {
const data: any = exceptStr(event.data);
if (!_.isEmpty(data)) {
12 months ago
const { i18n, columns, dataSource, countResult, showSum, SSHeaderInfo = "", pageInfo } = data;
setI18n(i18n);
setDataSource(dataSource);
setShowSumrow(showSum);
setSumRow(countResult);
1 year ago
setSSHeaderInfo(SSHeaderInfo);
12 months ago
setPageInfo(pageInfo);
setColumns(_.map(columns, (item, index: number) => {
if (index === 0) {
2 years ago
return { ...item, fixed: "left", ellipsis: true };
}
return {
...item,
children: _.map(item.children, child => {
return {
2 years ago
...child, ellipsis: true,
render: (text: string, record: any) => {
1 year ago
if (child.dataIndex?.endsWith("increase")) {
let lowerLimitIndex = child.dataIndex.replace("increase", "lowerLimit");
let upperLimitIndex = child.dataIndex.replace("increase", "upperLimit");
1 year ago
let rowData = !_.isEmpty(record[child.dataIndex]) ? record[child.dataIndex].split(",").join("") : "";
if (isNaN(parseFloat(rowData))) {
1 year ago
return record[child.dataIndex];
}
if (!isNaN(parseFloat(record[lowerLimitIndex])) && parseFloat(rowData) < parseFloat(record[lowerLimitIndex])) {
return <Button type="link" block onClick={() => {
window.parent.postMessage(
{
type: "turn",
payload: { id: "PIVOTCHART", params: { record } }
},
"*"
);
}}>
<Text type="success">{rowData}</Text>
1 year ago
</Button>;
}
if (!isNaN(parseFloat(record[upperLimitIndex])) && parseFloat(rowData) > parseFloat(record[upperLimitIndex])) {
return <Button type="link" block onClick={() => {
window.parent.postMessage(
{
type: "turn",
payload: { id: "PIVOTCHART", params: { record } }
},
"*"
);
}}>
<Text type="danger">{rowData}</Text>
1 year ago
</Button>;
} else {
1 year ago
return `${rowData}`;
}
}
2 years ago
return <Button type="link" block onClick={() => {
window.parent.postMessage(
{
type: "turn",
payload: { id: "PIVOTCHART", params: { record } }
},
"*"
);
}}>{text}</Button>;
}
};
})
};
}));
}
};
12 months ago
const onChange = (current: number, pageSize: number) => {
setPageInfo((prevState) => {
const { pageSize: size } = prevState;
window.parent.postMessage(
{
type: "turn",
payload: { id: "PAGEINFO_REPORT", params: { ...pageInfo, current: size === pageSize ? current : 1, pageSize } }
},
"*"
);
return { ...pageInfo, current: size === pageSize ? current : 1, pageSize };
});
};
return <Table
rowKey="id"
className={styles.tableWrapper}
columns={columns}
dataSource={dataSource}
bordered
size="small"
12 months ago
scroll={{ x: 1200, y: `calc(100vh - 174px)` }}
1 year ago
title={() => <Paragraph ellipsis={true}>{SSHeaderInfo}</Paragraph>}
12 months ago
pagination={!_.isNil(pageInfo) ? {
...paginationAction(pageInfo, i18n, onChange),
size: "default"
} : false}
summary={() => {
if (!showSumrow) return;
let totalColumns: any[] = [];
_.forEach(columns, it => {
if (_.isEmpty(it.children)) {
totalColumns.push(it);
} else {
totalColumns = [...totalColumns, ...it.children];
}
});
return (
<Table.Summary fixed>
<Table.Summary.Row>
{
_.map(totalColumns, (item, index) => {
return <Table.Summary.Cell index={index} key={index} align="center">
<Text>{!_.isNil(sumRow[item.dataIndex]) ? sumRow[item.dataIndex] : "-"}</Text>
</Table.Summary.Cell>;
})
}
</Table.Summary.Row>
</Table.Summary>
);
}}
/>;
};
export default ReportTable;