예제 — @topgrid/grid 사용 패턴
자주 쓰는 Grid 패턴 3가지를 실제 동작하는 코드로 정리했다. 그대로 복사해
React 프로젝트(@topgrid/grid-core 설치)에 붙여 실행할 수 있다.
pnpm add @topgrid/grid-core @tanstack/react-table
# 변경 추적 예제는 추가로:
pnpm add @topgrid/grid-pro-tracking @topgrid/grid-license
예제 1: 기본 Grid
클라이언트 페이지네이션 + 다중 행 선택 + 정렬.
import { Grid } from '@topgrid/grid-core';
import { type ColumnDef } from '@tanstack/react-table';
type Employee = {
id: string;
name: string;
department: string;
salary: number;
};
const data: Employee[] = [
{ id: 'E001', name: '김철수', department: '개발팀', salary: 5500000 },
{ id: 'E002', name: '이영희', department: '인사팀', salary: 4800000 },
{ id: 'E003', name: '박민준', department: '회계팀', salary: 5200000 },
{ id: 'E004', name: '정수연', department: '개발팀', salary: 6000000 },
{ id: 'E005', name: '최동현', department: '영업팀', salary: 4500000 },
];
const columns: ColumnDef<Employee>[] = [
{ accessorKey: 'id', header: '사원번호', size: 100 },
{ accessorKey: 'name', header: '성명', size: 120 },
{ accessorKey: 'department', header: '부서', size: 120 },
{
accessorKey: 'salary',
header: '급여',
size: 150,
cell: ({ getValue }) =>
new Intl.NumberFormat('ko-KR', { style: 'currency', currency: 'KRW' })
.format(getValue() as number),
},
];
export default function App() {
return (
<Grid
data={data}
columns={columns}
enableSort
enablePagination
pagination={{ pageSize: 3 }}
rowSelection="multi"
emptyText="조회 결과가 없습니다."
/>
);
}
예제 2: 가상화 Grid (대용량)
enableVirtualization 하나로 대용량 행을 가상화 렌더링한다(보이는 행만 DOM 유지).
import { Grid } from '@topgrid/grid-core';
import { type ColumnDef } from '@tanstack/react-table';
type Row = { id: number; item: string; category: string; value: number };
// 1,000행 생성
const data: Row[] = Array.from({ length: 1000 }, (_, i) => ({
id: i + 1,
item: `항목_${i + 1}`,
category: `분류_${(i % 10) + 1}`,
value: (i * 137) % 100000,
}));
const columns: ColumnDef<Row>[] = [
{ accessorKey: 'id', header: '#', size: 60 },
{ accessorKey: 'item', header: '항목', size: 200 },
{ accessorKey: 'category', header: '분류', size: 150 },
{
accessorKey: 'value',
header: '값',
size: 120,
cell: ({ getValue }) =>
new Intl.NumberFormat('ko-KR').format(getValue() as number),
},
];
export default function App() {
return (
<Grid
data={data}
columns={columns}
enableVirtualization
enableSort
emptyText="데이터가 없습니다."
/>
);
}