fix: footer(합계) 작업 중

This commit is contained in:
2025-12-29 11:49:07 +09:00
parent ba4062f826
commit 2009c54a00
38 changed files with 1367 additions and 1083 deletions
+65 -5
View File
@@ -10,6 +10,16 @@ import { useAlertStore } from "@/shared/lib";
import { Button } from "@common/components";
import { useTabStore } from "@/store/tab-store"
// RealGrid 타입 확장
declare module "realgrid" {
interface GridView {
setOptions(options: {
footer?: { visible?: boolean };
summaryMode?: 'aggregate' | 'statistical' | 'none';
}): void;
}
}
// Window 인터페이스 확장
declare global {
interface Window {
@@ -32,7 +42,28 @@ const DataGrid = ({ columnInfo, rows = [], rowStartNum, isExcelImport = true, fi
const activeTabId = useTabStore((state) => state.activeTabId);
const menuList = useTabStore((state) => state.menuList);
const [rowData, setRowData] = useState<Record<string, string>[]>([]);
const [rowData, setRowData] = useState<Record<string, string | number>[]>([]);
// footer가 있는 컬럼의 데이터를 숫자로 변환
const convertNumericData = (data: Record<string, string>[]): Record<string, string | number>[] => {
const footerColumns = columns.filter(col => col.footer);
const footerFieldNames = new Set(footerColumns.map(col => col.fieldName));
return data.map(row => {
const convertedRow: Record<string, string | number> = { ...row };
footerFieldNames.forEach(fieldName => {
const value = row[fieldName];
if (value !== undefined && value !== null && value !== '') {
// 쉼표 제거 후 숫자로 변환
const numericValue = Number(String(value).replace(/,/g, ''));
if (!isNaN(numericValue)) {
convertedRow[fieldName] = numericValue;
}
}
});
return convertedRow;
});
};
const exportExcel = () => {
if (rowData.length === 0) {
@@ -152,7 +183,8 @@ const DataGrid = ({ columnInfo, rows = [], rowStartNum, isExcelImport = true, fi
}
}
setRowData(dataRows);
const convertedDataRows = convertNumericData(dataRows);
setRowData(convertedDataRows);
} catch (error) {
console.error('엑셀 파일 파싱 오류:', error);
alert('엑셀 파일 파싱 중 오류가 발생했습니다.');
@@ -187,8 +219,11 @@ const DataGrid = ({ columnInfo, rows = [], rowStartNum, isExcelImport = true, fi
(gridView.displayOptions as DisplayOptions).fitStyle = GridFitStyle.EVEN;
gridView.header.height = 60;
if (rows.length > 0) {
setRowData(rows);
// footer가 있는 컬럼이 있으면 footer 높이 설정
const hasFooter = columns.some(col => col.footer);
if (hasFooter) {
gridView.footer.height = 40;
}
// 고정 컬럼 설정
@@ -197,7 +232,27 @@ const DataGrid = ({ columnInfo, rows = [], rowStartNum, isExcelImport = true, fi
colCount: fixedColumns,
});
}
}, [rows, columns, columnLayout, fixedColumns]);
gridView.setOptions({
summaryMode: "aggregate"
});
// footer의 valueCallback 설정
columns.forEach(col => {
if (col.footer && col.footer.valueCallback) {
const column = gridView.columnByName(col.name);
if (column && column.footer) {
(column.footer as { valueCallback?: typeof col.footer.valueCallback }).valueCallback = col.footer.valueCallback;
}
}
});
if (rows.length > 0) {
const convertedRows = convertNumericData(rows);
setRowData(convertedRows);
}
}, [rows, columns, columnLayout, fixedColumns, rowData]);
return (
<div className="flex flex-col gap-[0.6rem] mx-[0.6rem]">
@@ -233,6 +288,11 @@ const DataGrid = ({ columnInfo, rows = [], rowStartNum, isExcelImport = true, fi
name={column.name}
fieldName={column.fieldName}
header={column.header}
footer={column.footer ? {
text: column.footer.text,
expression: column.footer.expression,
numberFormat: column.footer.numberFormat,
} : undefined}
// autoFilter={true}
/>
))}
@@ -5,17 +5,38 @@ import SaveButton from "@/components/button/SaveButton";
import LastYearDataButton from "@/components/button/LastYearDataButton";
import SubmitExceptionButton from "@/components/button/SubmitExceptionButton";
import type { GridView, DataColumn, ColumnFooter } from "@/components/type/DataGrid.type";
import { getFooterSummary } from "@/components/util/grid/footerSummary";
const SeweragePenetrationRateMng = () => {
const columns = [
{ name: 'sido', fieldName: '시도', header: { text: '시도' } },
{ name: 'sgg', fieldName: '구군', header: { text: '구군' } },
{
name: 'sgg',
fieldName: '구군',
header: { text: '구군' },
footer: {
text: '합계'
},
},
{ name: 'rvbsn', fieldName: '수계', header: { text: '수계' } },
{ name: 'bstrm', fieldName: '지류', header: { text: '지류' } },
{ name: 'mdbsn', fieldName: '세부단위유역', header: { text: '세부단위유역' } },
{ name: 'lgbsn', fieldName: '중권역', header: { text: '중권역' } },
{ name: 'mdmbsn', fieldName: '소권역', header: { text: '소권역' } },
{ name: 'specl_cntrpln_ar', fieldName: '특별대책지역', header: { text: '특별대책지역' } },
{ name: 'total_popl', fieldName: '총인구', header: { text: '총인구' } },
{
name: 'total_popl',
fieldName: '총인구',
header: { text: '총인구' },
footer: {
expression: 'sum' as const,
numberFormat: '#,##0',
valueCallback: (grid: GridView, column: DataColumn) => {
return getFooterSummary(grid, column);
},
},
},
{ name: 'popl_sm', fieldName: '내국인', header: { text: '내국인' } },
{ name: 'frgnr_popl_sm', fieldName: '외국인', header: { text: '외국인' } },
{ name: 'area', fieldName: '행정참고자료면적(㎢)', header: { text: '행정참고자료면적(㎢)' } },
@@ -131,7 +152,7 @@ const SeweragePenetrationRateMng = () => {
<LastYearDataButton getLastYearDataFunc={getLastYearDataFunc} />
<SubmitExceptionButton SubmitExceptionFunc={SubmitExceptionFunc} />
</div>
<DataGrid columnInfo={{columns, columnLayout}} rowStartNum={2} isExcelImport={true} fixedColumns={1} />
<DataGrid columnInfo={{ columns, columnLayout }} rowStartNum={3} isExcelImport={true} fixedColumns={1} />
</>
);
};
@@ -1,7 +1,33 @@
// RealGrid 타입 정의
export type GridView = {
getSummary(field: string, summary: 'sum' | 'avg' | 'count' | 'max' | 'min'): number | undefined;
getDataSource(): {
getRowCount(): number;
getValue(rowIndex: number, field: string): number;
};
};
export type DataColumn = {
name: string;
fieldName: string;
};
export type ColumnFooter = {
text?: string;
expression?: string;
numberFormat?: string;
};
export type ColumnConfig = {
name: string;
fieldName: string;
header: { text: string };
footer?: {
text?: string;
expression?: 'sum' | 'avg' | 'count' | 'max' | 'min';
numberFormat?: string;
valueCallback?: (grid: GridView, column: DataColumn, footerIndex: number, columnFooter: ColumnFooter, value: number | string | undefined) => number | string;
},
};
export type ColumnLayoutItem = string | {
@@ -0,0 +1,22 @@
import type { GridView, DataColumn } from "@/components/type/DataGrid.type";
export const getFooterSummary = (grid: GridView, column: DataColumn) => {
// 데이터 소스에서 직접 합계 계산
const dataSource = grid.getDataSource();
const rowCount = dataSource.getRowCount();
let sum = 0;
for (let i = 0; i < rowCount; i++) {
const cellValue = dataSource.getValue(i, column.fieldName);
// 문자열인 경우 숫자로 변환
const numericValue = typeof cellValue === 'string'
? Number(String(cellValue).replace(/,/g, ''))
: Number(cellValue);
if (!isNaN(numericValue) && numericValue !== null && numericValue !== undefined) {
sum += numericValue;
}
}
return sum;
};