import React, { useEffect, useState } from 'react' import "./index.css" import { ReportData, ReportTableProps } from "../tmpl/interface"; import HcserviceV3 from "../tmpl/hcservice-v3"; import { useLocale } from "../locale/useLocale"; import ColumnBuilder from "./column/column-builder"; import ResizableTable from "./resizable-table"; const ReportTable = (props: ReportTableProps) => { const { showEmpty = true, tableLayout } = props; const [loading, setLoading] = useState(false); const [dataSource, setDataSource] = useState(undefined); const [reportData, setReportData] = useState(undefined); const { getServiceLangStr, translate } = useLocale(); const { tableProps, columns, queryKey, serverKey, leftFixedCols, hiddenColIds, onChangeResize } = props useEffect(() => { const queryData = async () => { if (queryKey) { await loadData(queryKey.key, serverKey); } }; queryData() }, [queryKey?.key]); const loadData = async ( queryKey: string, serverKey: string ) => { setLoading(true); let reportData: ReportData = await HcserviceV3.requestQueryReportData( serverKey, queryKey, getServiceLangStr() ); if (reportData?.key != queryKey) { return; } else { let dataSource: object[] = []; if (reportData.entities) { reportData.entities.forEach((item, index) => { if (item) { let row = { key: item.code, code: item.code, //增加code,为了删除操作 ...item.fieldMap, index, }; dataSource.push(row); } }); } setDataSource(dataSource); setReportData(reportData); setLoading(false); } } const addColumnOnCell = (current_cols, hiddenColIds: string[]) => { if (!reportData) { return; } const { rowGrids } = reportData current_cols.forEach((c) => { debugger if (rowGrids) { c.onCell = (record, index) => { let rowGrid = rowGrids[record.code]; if (rowGrid && rowGrid.cellMap) { if (rowGrid.cellMap[c.dataIndex]) { let a = { ...rowGrid.cellMap[c.dataIndex] } if (a.colSpan > 1) { let hiddenDim = 0; if (hiddenColIds) { debugger hiddenColIds.forEach(id => { if (rowGrid.cellMap[id]) { hiddenDim++; } }) } return { colSpan: a.colSpan - hiddenDim, rowSpan: 1 } } else { return a; } } else { return { colSpan: 1, rowSpan: 1 } } } } } }); } let current_cols = reportData?.columnConfigs ? ColumnBuilder.buildReportTableColumns({ columnConfigs: [...reportData.columnConfigs], translate, leftFixedCols, hiddenColIds, tableLayout }) : columns; let table_x = 20; current_cols.forEach((c) => { table_x = table_x + (c.width ? c.width : 140); }); addColumnOnCell(current_cols, hiddenColIds); return } export default ReportTable;