10만 행도 가볍게 쓰는 요령Keeping 100,000 rows light
표에 10만 행을 넣어도 화면에는 보이는 줄만 그려져서 스크롤과 정렬이 가벼워요. 여기에 프레임워크 쪽에서 지킬 요령을 더했어요. React는 행을 한 번만 만들고 스크롤 이벤트를 받지 않기, Vue는 감시를 가볍게 하는 상자에 담기, Angular는 받을 이벤트를 좁히기예요. React, Vue, Angular 탭에는 각 프레임워크의 방식으로 쓴 같은 예제가 들어 있어요.Even with 100,000 rows only the visible lines are drawn, so scrolling and sorting stay light. The framework tabs add what to watch: in React build the rows once and skip the scroll event, in Vue keep them in a light-weight reactive box, in Angular narrow the events you receive. The React, Vue and Angular tabs hold the same example written the way each framework does it.
실행 화면은 JavaScript 버전이에요. 아래 탭의 React·Vue·Angular 코드는 같은 예제를 각 프레임워크로 직접 쓴 코드예요.The preview runs the JavaScript version. The React, Vue, Angular tabs below hold the same example written for each framework.
코드Code
탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.
이 화면은 새 모양 옵션을 켠 모양이에요: density: 'spacious', iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: density: 'spacious', iconStyle: 'soft', motion: true (without them in your code you get the default look)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<div id="app">
<div class="toolbar">
<button type="button" id="middle">가운데로 가기</button>
<button type="button" id="last">맨 끝으로 가기</button>
<button type="button" id="sortBySalary">월급 높은 순으로</button>
<button type="button" id="clear">정렬 풀기</button>
</div>
<div id="grid" style="height: 440px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('middle').addEventListener('click', () => actions.middle(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
document.getElementById('sortBySalary').addEventListener('click', () => actions.sortBySalary(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));export const TOTAL = 100_000;
const DEPTS = ['개발팀', '디자인팀', '영업팀', '인사팀'];
/** 10만 행을 한 번에 만들어요. 화면에는 보이는 줄만 그려져서 행이 많아도 가벼워요. */
export function makeRows(count = TOTAL) {
return Array.from({ length: count }, (_, i) => ({
id: i + 1,
name: `직원 ${i + 1}`,
dept: DEPTS[i % DEPTS.length],
salary: 300 + ((i * 37) % 500),
}));
}
export const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 90, align: 'end' },
{ id: 'name', title: '이름', width: 140 },
{ id: 'dept', title: '부서', width: 120 },
{ id: 'salary', title: '월급(만원)', kind: 'number', width: 130, align: 'end' },
],
rows: makeRows(),
rowKey: 'id',
};
export function setup(api, log) {
log({
ko: `${TOTAL.toLocaleString('ko-KR')}행을 한 번에 넣었어요`,
en: `Put in ${TOTAL.toLocaleString('en-US')} rows at once`,
});
return api.on('sortChanged', ({ sortModel }) => {
log({
ko: sortModel.length ? '정렬했어요 (10만 행도 바로 끝나요)' : '정렬을 풀었어요',
en: sortModel.length ? 'Sorted (even 100,000 rows finish at once)' : 'Sort cleared',
});
});
}
export const actions = {
middle: (api) => api.scrollToIndex(Math.floor(TOTAL / 2)),
last: (api) => api.scrollToIndex(TOTAL - 1),
sortBySalary: (api) => api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
clear: (api) => api.setSortModel([]),
};<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>10만 행도 가볍게 쓰는 요령</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="middle">가운데로 가기</button>
<button type="button" id="last">맨 끝으로 가기</button>
<button type="button" id="sortBySalary">월급 높은 순으로</button>
<button type="button" id="clear">정렬 풀기</button>
</div>
<div id="grid" style="height: 440px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const TOTAL = 100_000;
const DEPTS = ['개발팀', '디자인팀', '영업팀', '인사팀'];
/** 10만 행을 한 번에 만들어요. 화면에는 보이는 줄만 그려져서 행이 많아도 가벼워요. */
function makeRows(count = TOTAL) {
return Array.from({ length: count }, (_, i) => ({
id: i + 1,
name: `직원 ${i + 1}`,
dept: DEPTS[i % DEPTS.length],
salary: 300 + ((i * 37) % 500),
}));
}
const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 90, align: 'end' },
{ id: 'name', title: '이름', width: 140 },
{ id: 'dept', title: '부서', width: 120 },
{ id: 'salary', title: '월급(만원)', kind: 'number', width: 130, align: 'end' },
],
rows: makeRows(),
rowKey: 'id',
};
function setup(api, log) {
log({
ko: `${TOTAL.toLocaleString('ko-KR')}행을 한 번에 넣었어요`,
en: `Put in ${TOTAL.toLocaleString('en-US')} rows at once`,
});
return api.on('sortChanged', ({ sortModel }) => {
log({
ko: sortModel.length ? '정렬했어요 (10만 행도 바로 끝나요)' : '정렬을 풀었어요',
en: sortModel.length ? 'Sorted (even 100,000 rows finish at once)' : 'Sort cleared',
});
});
}
const actions = {
middle: (api) => api.scrollToIndex(Math.floor(TOTAL / 2)),
last: (api) => api.scrollToIndex(TOTAL - 1),
sortBySalary: (api) => api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
clear: (api) => api.setSortModel([]),
};
options.ariaLabel = "10만 행도 가볍게 쓰는 요령";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('middle').addEventListener('click', () => actions.middle(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
document.getElementById('sortBySalary').addEventListener('click', () => actions.sortBySalary(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Grid } from '@maxgrid/react';
import { useState } from 'react';
import { options, type Row } from './example';
// 행이 많을 때 React에서 지킬 점은 두 가지예요.
// 1) options를 컴포넌트 밖(또는 한 번만 만드는 곳)에 둬서 렌더마다 새 10만 행을 만들지 않기
// 2) 잦은 이벤트(스크롤)에는 onScrolled를 달지 않기 — 달지 않은 이벤트는 구독하지 않아요
export function ExampleGrid() {
const [sortText, setSortText] = useState('정렬 없음');
return (
<>
<p>{sortText}</p>
<div style={{ height: 320 }}>
<Grid<Row>
options={options}
onSortChanged={({ sortModel }) =>
setSortText(sortModel[0] ? `${sortModel[0].columnId} 기준 정렬` : '정렬 없음')
}
/>
</div>
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
dept: string;
salary: number;
}
export const TOTAL = 100_000;
const DEPTS = ['개발팀', '디자인팀', '영업팀', '인사팀'];
/** 10만 행을 한 번에 만들어요. 화면에는 보이는 줄만 그려져서 행이 많아도 가벼워요. */
export function makeRows(count: number = TOTAL): Row[] {
return Array.from({ length: count }, (_, i) => ({
id: i + 1,
name: `직원 ${i + 1}`,
dept: DEPTS[i % DEPTS.length] as string,
salary: 300 + ((i * 37) % 500),
}));
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 90, align: 'end' },
{ id: 'name', title: '이름', width: 140 },
{ id: 'dept', title: '부서', width: 120 },
{ id: 'salary', title: '월급(만원)', kind: 'number', width: 130, align: 'end' },
],
rows: makeRows(),
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: `${TOTAL.toLocaleString('ko-KR')}행을 한 번에 넣었어요`,
en: `Put in ${TOTAL.toLocaleString('en-US')} rows at once`,
});
return api.on('sortChanged', ({ sortModel }) => {
log({
ko: sortModel.length ? '정렬했어요 (10만 행도 바로 끝나요)' : '정렬을 풀었어요',
en: sortModel.length ? 'Sorted (even 100,000 rows finish at once)' : 'Sort cleared',
});
});
}
export const actions = {
middle: (api: GridApi<Row>) => api.scrollToIndex(Math.floor(TOTAL / 2)),
last: (api: GridApi<Row>) => api.scrollToIndex(TOTAL - 1),
sortBySalary: (api: GridApi<Row>) =>
api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
clear: (api: GridApi<Row>) => api.setSortModel([]),
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { Grid } from '@maxgrid/vue';
import { ref, shallowRef } from 'vue';
import { options as baseOptions, type Row } from './example';
// 행이 많을 때는 shallowRef에 담아요. ref는 10만 행을 하나하나 감시용으로 감싸지만,
// shallowRef는 맨 바깥 값만 감시해서 가벼워요. (표는 받은 값을 원본으로 풀어서 쓰고요)
const options = shallowRef(baseOptions);
const sortText = ref('정렬 없음');
</script>
<template>
<p>{{ sortText }}</p>
<Grid
style="height: 320px"
:options="options"
@sort-changed="
({ sortModel }) =>
(sortText = sortModel[0] ? `${sortModel[0].columnId} 기준 정렬` : '정렬 없음')
"
/>
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
dept: string;
salary: number;
}
export const TOTAL = 100_000;
const DEPTS = ['개발팀', '디자인팀', '영업팀', '인사팀'];
/** 10만 행을 한 번에 만들어요. 화면에는 보이는 줄만 그려져서 행이 많아도 가벼워요. */
export function makeRows(count: number = TOTAL): Row[] {
return Array.from({ length: count }, (_, i) => ({
id: i + 1,
name: `직원 ${i + 1}`,
dept: DEPTS[i % DEPTS.length] as string,
salary: 300 + ((i * 37) % 500),
}));
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 90, align: 'end' },
{ id: 'name', title: '이름', width: 140 },
{ id: 'dept', title: '부서', width: 120 },
{ id: 'salary', title: '월급(만원)', kind: 'number', width: 130, align: 'end' },
],
rows: makeRows(),
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: `${TOTAL.toLocaleString('ko-KR')}행을 한 번에 넣었어요`,
en: `Put in ${TOTAL.toLocaleString('en-US')} rows at once`,
});
return api.on('sortChanged', ({ sortModel }) => {
log({
ko: sortModel.length ? '정렬했어요 (10만 행도 바로 끝나요)' : '정렬을 풀었어요',
en: sortModel.length ? 'Sorted (even 100,000 rows finish at once)' : 'Sort cleared',
});
});
}
export const actions = {
middle: (api: GridApi<Row>) => api.scrollToIndex(Math.floor(TOTAL / 2)),
last: (api: GridApi<Row>) => api.scrollToIndex(TOTAL - 1),
sortBySalary: (api: GridApi<Row>) =>
api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
clear: (api: GridApi<Row>) => api.setSortModel([]),
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import {
GridComponent,
isGridEvent,
type GridEventMessage,
type GridEventName,
} from '@maxgrid/angular';
import { options, type Row } from './example';
// 행이 많을 때는 [events]로 받을 이벤트를 좁혀요.
// 쓰지 않는 scrolled를 빼 두면, 스크롤할 때마다 화면 갱신이 돌지 않아요.
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<p>{{ sortText() }}</p>
<mx-grid
style="height: 320px"
[options]="options"
[events]="events"
(gridEvent)="onEvent($event)"
/>
`,
})
export class ExampleGridComponent {
readonly options = options;
readonly events: GridEventName[] = ['sortChanged'];
readonly sortText = signal('정렬 없음');
onEvent(message: GridEventMessage<Row>): void {
if (!isGridEvent(message, 'sortChanged')) return;
const first = message.payload.sortModel[0];
this.sortText.set(first ? `${first.columnId} 기준 정렬` : '정렬 없음');
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
dept: string;
salary: number;
}
export const TOTAL = 100_000;
const DEPTS = ['개발팀', '디자인팀', '영업팀', '인사팀'];
/** 10만 행을 한 번에 만들어요. 화면에는 보이는 줄만 그려져서 행이 많아도 가벼워요. */
export function makeRows(count: number = TOTAL): Row[] {
return Array.from({ length: count }, (_, i) => ({
id: i + 1,
name: `직원 ${i + 1}`,
dept: DEPTS[i % DEPTS.length] as string,
salary: 300 + ((i * 37) % 500),
}));
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 90, align: 'end' },
{ id: 'name', title: '이름', width: 140 },
{ id: 'dept', title: '부서', width: 120 },
{ id: 'salary', title: '월급(만원)', kind: 'number', width: 130, align: 'end' },
],
rows: makeRows(),
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: `${TOTAL.toLocaleString('ko-KR')}행을 한 번에 넣었어요`,
en: `Put in ${TOTAL.toLocaleString('en-US')} rows at once`,
});
return api.on('sortChanged', ({ sortModel }) => {
log({
ko: sortModel.length ? '정렬했어요 (10만 행도 바로 끝나요)' : '정렬을 풀었어요',
en: sortModel.length ? 'Sorted (even 100,000 rows finish at once)' : 'Sort cleared',
});
});
}
export const actions = {
middle: (api: GridApi<Row>) => api.scrollToIndex(Math.floor(TOTAL / 2)),
last: (api: GridApi<Row>) => api.scrollToIndex(TOTAL - 1),
sortBySalary: (api: GridApi<Row>) =>
api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
clear: (api: GridApi<Row>) => api.setSortModel([]),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div class="toolbar">
<button type="button" id="middle">Jump to the middle</button>
<button type="button" id="last">Jump to the end</button>
<button type="button" id="sortBySalary">Sort by salary, high first</button>
<button type="button" id="clear">Clear sort</button>
</div>
<div id="grid" style="height: 440px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('middle').addEventListener('click', () => actions.middle(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
document.getElementById('sortBySalary').addEventListener('click', () => actions.sortBySalary(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));export const TOTAL = 100_000;
const DEPTS = ['Dev', 'Design', 'Sales', 'HR'];
/** Creates 100,000 rows at once. Only the visible lines are drawn, so it stays light even with many rows. */
export function makeRows(count = TOTAL) {
return Array.from({ length: count }, (_, i) => ({
id: i + 1,
name: `Employee ${i + 1}`,
dept: DEPTS[i % DEPTS.length],
salary: 300 + ((i * 37) % 500),
}));
}
export const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 90, align: 'end' },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'dept', title: 'Department', width: 120 },
{ id: 'salary', title: 'Monthly pay ($100)', kind: 'number', width: 160, align: 'end' },
],
rows: makeRows(),
rowKey: 'id',
};
export function setup(api, log) {
log(`Put in ${TOTAL.toLocaleString('en-US')} rows at once`);
return api.on('sortChanged', ({ sortModel }) => {
log(sortModel.length ? 'Sorted (even 100,000 rows finish at once)' : 'Sort cleared');
});
}
export const actions = {
middle: (api) => api.scrollToIndex(Math.floor(TOTAL / 2)),
last: (api) => api.scrollToIndex(TOTAL - 1),
sortBySalary: (api) => api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
clear: (api) => api.setSortModel([]),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Keeping 100,000 rows light</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="middle">Jump to the middle</button>
<button type="button" id="last">Jump to the end</button>
<button type="button" id="sortBySalary">Sort by salary, high first</button>
<button type="button" id="clear">Clear sort</button>
</div>
<div id="grid" style="height: 440px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const TOTAL = 100_000;
const DEPTS = ['Dev', 'Design', 'Sales', 'HR'];
/** Creates 100,000 rows at once. Only the visible lines are drawn, so it stays light even with many rows. */
function makeRows(count = TOTAL) {
return Array.from({ length: count }, (_, i) => ({
id: i + 1,
name: `Employee ${i + 1}`,
dept: DEPTS[i % DEPTS.length],
salary: 300 + ((i * 37) % 500),
}));
}
const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 90, align: 'end' },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'dept', title: 'Department', width: 120 },
{ id: 'salary', title: 'Monthly pay ($100)', kind: 'number', width: 160, align: 'end' },
],
rows: makeRows(),
rowKey: 'id',
};
function setup(api, log) {
log(`Put in ${TOTAL.toLocaleString('en-US')} rows at once`);
return api.on('sortChanged', ({ sortModel }) => {
log(sortModel.length ? 'Sorted (even 100,000 rows finish at once)' : 'Sort cleared');
});
}
const actions = {
middle: (api) => api.scrollToIndex(Math.floor(TOTAL / 2)),
last: (api) => api.scrollToIndex(TOTAL - 1),
sortBySalary: (api) => api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
clear: (api) => api.setSortModel([]),
};
options.ariaLabel = "Keeping 100,000 rows light";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('middle').addEventListener('click', () => actions.middle(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
document.getElementById('sortBySalary').addEventListener('click', () => actions.sortBySalary(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Grid } from '@maxgrid/react';
import { useState } from 'react';
import { options, type Row } from './example';
// 행이 많을 때 React에서 지킬 점은 두 가지예요.
// 1) options를 컴포넌트 밖(또는 한 번만 만드는 곳)에 둬서 렌더마다 새 10만 행을 만들지 않기
// 2) 잦은 이벤트(스크롤)에는 onScrolled를 달지 않기 — 달지 않은 이벤트는 구독하지 않아요
export function ExampleGrid() {
const [sortText, setSortText] = useState('정렬 없음');
return (
<>
<p>{sortText}</p>
<div style={{ height: 320 }}>
<Grid<Row>
options={options}
onSortChanged={({ sortModel }) =>
setSortText(sortModel[0] ? `${sortModel[0].columnId} 기준 정렬` : '정렬 없음')
}
/>
</div>
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string;
salary: number;
}
export const TOTAL = 100_000;
const DEPTS = ['Dev', 'Design', 'Sales', 'HR'];
/** Creates 100,000 rows at once. Only the visible lines are drawn, so it stays light even with many rows. */
export function makeRows(count: number = TOTAL): Row[] {
return Array.from({ length: count }, (_, i) => ({
id: i + 1,
name: `Employee ${i + 1}`,
dept: DEPTS[i % DEPTS.length] as string,
salary: 300 + ((i * 37) % 500),
}));
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 90, align: 'end' },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'dept', title: 'Department', width: 120 },
{ id: 'salary', title: 'Monthly pay ($100)', kind: 'number', width: 160, align: 'end' },
],
rows: makeRows(),
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log(`Put in ${TOTAL.toLocaleString('en-US')} rows at once`);
return api.on('sortChanged', ({ sortModel }) => {
log(sortModel.length ? 'Sorted (even 100,000 rows finish at once)' : 'Sort cleared');
});
}
export const actions = {
middle: (api: GridApi<Row>) => api.scrollToIndex(Math.floor(TOTAL / 2)),
last: (api: GridApi<Row>) => api.scrollToIndex(TOTAL - 1),
sortBySalary: (api: GridApi<Row>) =>
api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
clear: (api: GridApi<Row>) => api.setSortModel([]),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { Grid } from '@maxgrid/vue';
import { ref, shallowRef } from 'vue';
import { options as baseOptions, type Row } from './example';
// 행이 많을 때는 shallowRef에 담아요. ref는 10만 행을 하나하나 감시용으로 감싸지만,
// shallowRef는 맨 바깥 값만 감시해서 가벼워요. (표는 받은 값을 원본으로 풀어서 쓰고요)
const options = shallowRef(baseOptions);
const sortText = ref('정렬 없음');
</script>
<template>
<p>{{ sortText }}</p>
<Grid
style="height: 320px"
:options="options"
@sort-changed="
({ sortModel }) =>
(sortText = sortModel[0] ? `${sortModel[0].columnId} 기준 정렬` : '정렬 없음')
"
/>
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string;
salary: number;
}
export const TOTAL = 100_000;
const DEPTS = ['Dev', 'Design', 'Sales', 'HR'];
/** Creates 100,000 rows at once. Only the visible lines are drawn, so it stays light even with many rows. */
export function makeRows(count: number = TOTAL): Row[] {
return Array.from({ length: count }, (_, i) => ({
id: i + 1,
name: `Employee ${i + 1}`,
dept: DEPTS[i % DEPTS.length] as string,
salary: 300 + ((i * 37) % 500),
}));
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 90, align: 'end' },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'dept', title: 'Department', width: 120 },
{ id: 'salary', title: 'Monthly pay ($100)', kind: 'number', width: 160, align: 'end' },
],
rows: makeRows(),
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log(`Put in ${TOTAL.toLocaleString('en-US')} rows at once`);
return api.on('sortChanged', ({ sortModel }) => {
log(sortModel.length ? 'Sorted (even 100,000 rows finish at once)' : 'Sort cleared');
});
}
export const actions = {
middle: (api: GridApi<Row>) => api.scrollToIndex(Math.floor(TOTAL / 2)),
last: (api: GridApi<Row>) => api.scrollToIndex(TOTAL - 1),
sortBySalary: (api: GridApi<Row>) =>
api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
clear: (api: GridApi<Row>) => api.setSortModel([]),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import {
GridComponent,
isGridEvent,
type GridEventMessage,
type GridEventName,
} from '@maxgrid/angular';
import { options, type Row } from './example';
// 행이 많을 때는 [events]로 받을 이벤트를 좁혀요.
// 쓰지 않는 scrolled를 빼 두면, 스크롤할 때마다 화면 갱신이 돌지 않아요.
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<p>{{ sortText() }}</p>
<mx-grid
style="height: 320px"
[options]="options"
[events]="events"
(gridEvent)="onEvent($event)"
/>
`,
})
export class ExampleGridComponent {
readonly options = options;
readonly events: GridEventName[] = ['sortChanged'];
readonly sortText = signal('정렬 없음');
onEvent(message: GridEventMessage<Row>): void {
if (!isGridEvent(message, 'sortChanged')) return;
const first = message.payload.sortModel[0];
this.sortText.set(first ? `${first.columnId} 기준 정렬` : '정렬 없음');
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string;
salary: number;
}
export const TOTAL = 100_000;
const DEPTS = ['Dev', 'Design', 'Sales', 'HR'];
/** Creates 100,000 rows at once. Only the visible lines are drawn, so it stays light even with many rows. */
export function makeRows(count: number = TOTAL): Row[] {
return Array.from({ length: count }, (_, i) => ({
id: i + 1,
name: `Employee ${i + 1}`,
dept: DEPTS[i % DEPTS.length] as string,
salary: 300 + ((i * 37) % 500),
}));
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 90, align: 'end' },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'dept', title: 'Department', width: 120 },
{ id: 'salary', title: 'Monthly pay ($100)', kind: 'number', width: 160, align: 'end' },
],
rows: makeRows(),
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log(`Put in ${TOTAL.toLocaleString('en-US')} rows at once`);
return api.on('sortChanged', ({ sortModel }) => {
log(sortModel.length ? 'Sorted (even 100,000 rows finish at once)' : 'Sort cleared');
});
}
export const actions = {
middle: (api: GridApi<Row>) => api.scrollToIndex(Math.floor(TOTAL / 2)),
last: (api: GridApi<Row>) => api.scrollToIndex(TOTAL - 1),
sortBySalary: (api: GridApi<Row>) =>
api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
clear: (api: GridApi<Row>) => api.setSortModel([]),
};