본문으로 건너뛰기Skip to content
예제 첫화면Examples home

컬럼이 1,000개여도 부드럽게A thousand columns, still smooth

지표가 1,000개나 되는 아주 넓은 표도 화면에 보이는 컬럼만 그려서 빠르게 움직여요. 번호는 왼쪽, 합계는 오른쪽에 고정해 두면 가로로 멀리 가도 어느 줄인지 알 수 있어요.Even a table with 1,000 metric columns stays fast because only the visible columns are drawn. Pinning the No. left and the total right keeps rows readable while you scroll far.

활용Intermediate
직접 써 보기Live preview컬럼이 1,000개여도 부드럽게A thousand columns, still smooth

코드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)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<div id="app">
  <div class="toolbar">
    <button type="button" id="first">첫 컬럼으로</button>
    <button type="button" id="middle">500번째로</button>
    <button type="button" id="last">마지막 컬럼으로</button>
  </div>
  <div id="grid" style="height: 420px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('first').addEventListener('click', () => actions.first(api));
document.getElementById('middle').addEventListener('click', () => actions.middle(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
example.js
const COLUMNS = 1000;
// 컬럼이 1,000개여도 화면에 보이는 컬럼만 그려서 빨라요(가로 가상화).
// 맨 왼쪽 '번호'는 왼쪽에, '합계'는 오른쪽에 고정해서 가로로 스크롤해도 늘 보여요.
export const options = {
  columns: [
    { id: 'no', title: '번호', width: 70, pinned: 'start', align: 'end' },
    ...Array.from({ length: COLUMNS }, (_, c) => ({
      id: `c${c}`,
      title: `지표 ${c + 1}`,
      kind: 'number',
      width: 96,
      align: 'end',
    })),
    { id: 'sum', title: '합계', kind: 'number', width: 110, pinned: 'end', align: 'end' },
  ],
  rows: Array.from({ length: 200 }, (_, r) => {
    const row = { id: r, no: r + 1, sum: 0 };
    let sum = 0;
    for (let c = 0; c < COLUMNS; c += 1) {
      const value = ((r + 1) * (c + 3)) % 997;
      row[`c${c}`] = value;
      sum += value;
    }
    row.sum = sum;
    return row;
  }),
  rowKey: 'id',
};
export function setup(api, log) {
  // 스크롤이 멈춘 뒤(그리기가 끝난 뒤) 한 줄에 몇 칸이 그려져 있는지 센다.
  const count = () => document.querySelector('[role="row"].mxg-row')?.querySelectorAll('[role="gridcell"]').length ??
    0;
  let timer;
  const off = api.on('scrolled', () => {
    clearTimeout(timer);
    timer = setTimeout(() => log({
      ko: `지금 그려진 칸: 한 줄에 ${count()}개 (전체 ${COLUMNS + 2}컬럼)`,
      en: `Cells drawn now: ${count()} per row (${COLUMNS + 2} columns in all)`,
    }), 150);
  });
  return () => {
    clearTimeout(timer);
    off();
  };
}
export const actions = {
  middle: (api) => api.focusCell(1, 'c500'),
  last: (api) => api.focusCell(1, `c${COLUMNS - 1}`),
  first: (api) => api.focusCell(1, 'c0'),
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<div id="app">
  <div class="toolbar">
    <button type="button" id="first">To the first</button>
    <button type="button" id="middle">To column 500</button>
    <button type="button" id="last">To the last</button>
  </div>
  <div id="grid" style="height: 420px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('first').addEventListener('click', () => actions.first(api));
document.getElementById('middle').addEventListener('click', () => actions.middle(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
example.js
const COLUMNS = 1000;
// Even with 1,000 columns it stays fast because only the visible columns are drawn (horizontal virtualization).
// 'No.' is pinned on the left and 'Total' on the right, so both stay visible while scrolling sideways.
export const options = {
  columns: [
    { id: 'no', title: 'No.', width: 70, pinned: 'start', align: 'end' },
    ...Array.from({ length: COLUMNS }, (_, c) => ({
      id: `c${c}`,
      title: `Metric ${c + 1}`,
      kind: 'number',
      width: 112,
      align: 'end',
    })),
    { id: 'sum', title: 'Total', kind: 'number', width: 110, pinned: 'end', align: 'end' },
  ],
  rows: Array.from({ length: 200 }, (_, r) => {
    const row = { id: r, no: r + 1, sum: 0 };
    let sum = 0;
    for (let c = 0; c < COLUMNS; c += 1) {
      const value = ((r + 1) * (c + 3)) % 997;
      row[`c${c}`] = value;
      sum += value;
    }
    row.sum = sum;
    return row;
  }),
  rowKey: 'id',
};
export function setup(api, log) {
  // After scrolling stops (drawing is done), count how many cells are drawn in one row.
  const count = () => document.querySelector('[role="row"].mxg-row')?.querySelectorAll('[role="gridcell"]').length ??
    0;
  let timer;
  const off = api.on('scrolled', () => {
    clearTimeout(timer);
    timer = setTimeout(() => log(`Cells drawn now: ${count()} per row (${COLUMNS + 2} columns in all)`), 150);
  });
  return () => {
    clearTimeout(timer);
    off();
  };
}
export const actions = {
  middle: (api) => api.focusCell(1, 'c500'),
  last: (api) => api.focusCell(1, `c${COLUMNS - 1}`),
  first: (api) => api.focusCell(1, 'c0'),
};