합계 및 요약행 (Summary Row)
BGridProps의 summary 설정으로 상단 또는 하단에 사용자 정의 요약 셀을 표시하는 방법을 학습합니다.
import { Radio } from 'antd';
import * as React from 'react';
import { BGrid, BGridColumn, BGridDataItem, BGridProps } from 'beautiful-grid';
import { useContainerSize } from '../hooks/useContainerSize';
import DataGridContainer from '../components/DataGridContainer';
interface SalesOrder {
channel: string;
orderNo: string;
customer: string;
quantity: number;
supplyAmount: number;
taxAmount: number;
totalAmount: number;
}
const customers = ['에이원 리테일', '한빛상사', '모노마켓', '오로라스토어'];
const channels = ['자사몰', '스마트스토어', 'B2B'];
const data: BGridDataItem<SalesOrder>[] = Array.from({ length: 30 }, (_, index) => {
const quantity = (index % 8) + 1;
const supplyAmount = quantity * (48_000 + (index % 5) * 12_000);
const taxAmount = Math.round(supplyAmount * 0.1);
return {
values: {
channel: channels[index % channels.length],
orderNo: `ORD-2026-${String(index + 1).padStart(5, '0')}`,
customer: customers[index % customers.length],
quantity,
supplyAmount,
taxAmount,
totalAmount: supplyAmount + taxAmount,
},
};
});
const formatWon = (value: number) => `${value.toLocaleString()}원`;
const sumBy = (rows: BGridDataItem<SalesOrder>[], key: 'quantity' | 'supplyAmount' | 'taxAmount' | 'totalAmount') =>
rows.reduce((total, item) => total + item.values[key], 0);
const createSummary = (position: 'top' | 'bottom'): BGridProps<SalesOrder>['summary'] => ({
position,
columns: [
{
columnIndex: 0,
colSpan: 3,
align: 'center',
itemRender: ({ data }) => <strong>{position === 'top' ? '상단' : '하단'} 매출 합계 · {data.length}건</strong>,
},
{ columnIndex: 3, align: 'right', itemRender: ({ data }) => <strong>{sumBy(data, 'quantity')}개</strong> },
{ columnIndex: 4, align: 'right', itemRender: ({ data }) => <strong>{formatWon(sumBy(data, 'supplyAmount'))}</strong> },
{ columnIndex: 5, align: 'right', itemRender: ({ data }) => <strong>{formatWon(sumBy(data, 'taxAmount'))}</strong> },
{ columnIndex: 6, align: 'right', itemRender: ({ data }) => <strong>{formatWon(sumBy(data, 'totalAmount'))}</strong> },
],
});
function SummaryExample() {
const [position, setPosition] = React.useState<'top' | 'bottom'>('top');
const [columns, setColumns] = React.useState<BGridColumn<SalesOrder>[]>([
{ key: 'channel', label: '주문채널', width: 100, align: 'center' },
{ key: 'orderNo', label: '주문번호', width: 140 },
{ key: 'customer', label: '고객사', width: 150 },
{ key: 'quantity', label: '수량', width: 80, align: 'right' },
{ key: 'supplyAmount', label: '공급가액', width: 130, align: 'right', itemRender: ({ values }) => <>{formatWon(values.supplyAmount)}</> },
{ key: 'taxAmount', label: '부가세', width: 110, align: 'right', itemRender: ({ values }) => <>{formatWon(values.taxAmount)}</> },
{ key: 'totalAmount', label: '결제금액', width: 140, align: 'right', itemRender: ({ values }) => <strong>{formatWon(values.totalAmount)}</strong> },
]);
const containerRef = React.useRef<HTMLDivElement>(null);
const { width, height } = useContainerSize(containerRef);
return (
<>
<Radio.Group
style={{ marginBottom: 10 }}
options={[{ label: '상단 요약', value: 'top' }, { label: '하단 요약', value: 'bottom' }]}
value={position}
onChange={event => setPosition(event.target.value)}
/>
<DataGridContainer ref={containerRef}>
<BGrid<SalesOrder>
showLineNumber
frozenColumnIndex={1}
width={width}
height={height}
data={data}
columns={columns}
rowKey='orderNo'
onChangeColumns={(_columnIndex, { columns }) => setColumns(columns)}
variant='vertical-bordered'
summary={createSummary(position)}
/>
</DataGridContainer>
</>
);
}
export default SummaryExample;import * as React from 'react';
import './DataGridContainer.css';
interface DataGridContainerProps extends React.HTMLAttributes<HTMLDivElement> {
children?: React.ReactNode;
}
/**
* Keeps a DataGrid in a measured, fixed layout box.
*
* BGrid's rendered root is absolutely positioned within this relative
* container. This makes a ResizeObserver measurement authoritative when a
* surrounding flex or grid layout shrinks as well as when it expands.
*/
const DataGridContainer = React.forwardRef<HTMLDivElement, DataGridContainerProps>(
({ className, ...rest }, ref) => (
<div ref={ref} className={`data-grid-container ${className ?? ''}`.trim()} {...rest} />
),
);
DataGridContainer.displayName = 'DataGridContainer';
export default DataGridContainer;.data-grid-container {
position: relative;
width: 100%;
height: 400px;
overflow: hidden;
font-size: 13px;
}
.data-grid-container > .bgrid-root {
position: absolute;
inset: 0;
}import * as React from 'react';
export function useContainerSize(ref: React.MutableRefObject<HTMLElement | null>, additionalDeps: unknown[] = []) {
const [width, setWidth] = React.useState(0);
const [height, setHeight] = React.useState(0);
const resizeObserver = React.useRef(
new ResizeObserver(entries => {
if (entries.length !== 1) {
throw new Error('Invalid Container length');
}
const [entry] = entries;
const { width, height } = entry.contentRect;
setWidth(width);
setHeight(height);
}),
);
React.useEffect(() => {
if (!ref.current) return;
const observer = resizeObserver.current;
const element = ref.current;
setWidth(element.clientWidth);
setHeight(element.clientHeight);
observer.observe(element);
return () => {
observer.unobserve(element);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [...additionalDeps, ref]);
return {
width,
height,
};
}Summary 설정
summary는 일반 <table>이나 <tr>을 직접 반환하는 렌더 prop이 아닙니다. 표시 위치와 요약할 컬럼 정의 배열을 전달합니다.
const summary: BGridProps<Row>['summary'] = {
position: 'bottom',
columns: [
{
columnIndex: 0,
colSpan: 2,
align: 'center',
itemRender: () => <>합계</>,
},
{
columnIndex: 2,
align: 'right',
itemRender: ({ data }) => (
<>{data.reduce((sum, item) => sum + item.values.amount, 0).toLocaleString()}</>
),
},
],
};
<BGrid summary={summary} summaryHeight={32} {...props} />
멤버 의미
| 멤버 | 설명 |
|---|---|
position |
요약 행을 데이터 위(top) 또는 아래(bottom)에 배치합니다. |
columnIndex |
요약 셀이 시작될 컬럼의 0 기반 인덱스입니다. |
colSpan |
요약 셀이 차지할 컬럼 수입니다. |
itemRender |
{ column, columnIndex, data }를 받아 요약 콘텐츠를 반환합니다. |
summaryHeight |
요약 영역의 높이를 조정하는 최상위 prop입니다. |
data는 BGridDataItem<T>[]이므로 실제 값은 item.values에서 읽습니다. 페이지 단위 합계인지 전체 데이터 합계인지는 부모가 그리드에 어떤 데이터를 전달했는지에 따라 결정됩니다.