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

한 줄짜리 기본 헤더와 넓은 표A plain header on a wide table

가장 기본적인 한 줄 헤더예요. 2년치 월별 매출처럼 컬럼이 25개나 되어도, 왼쪽 지역 컬럼은 고정하고 나머지는 가로로 스크롤해서 볼 수 있어요.The simplest one-line header. Even with 25 columns of monthly sales, the Region column stays pinned while you scroll the rest sideways.

입문Beginner
직접 써 보기Live preview한 줄짜리 기본 헤더와 넓은 표A plain header on a wide table

코드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">
    <select aria-label="보이는 기간" id="period">
      <option value="all" selected>2022~2023 전체</option>
      <option value="2022">2022년만</option>
      <option value="2023">2023년만</option>
    </select>
    <select aria-label="지역 컬럼" id="pin">
      <option value="on" selected>왼쪽에 고정</option>
      <option value="off">고정 안 함</option>
    </select>
  </div>
  <div id="grid" style="height: 560px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('period').addEventListener('change', (event) => actions.period(api, event.target.value));
document.getElementById('pin').addEventListener('change', (event) => actions.pin(api, event.target.value));
example.js
// 지역별 월 매출(만 원) 2년치. 컬럼이 25개라서 한 화면에 다 안 들어가는 넓은 표예요.
const regions = [
  '서울',
  '경기·인천',
  '강원',
  '충북',
  '충남',
  '대전',
  '전북',
  '전남',
  '광주',
  '경북',
  '대구',
  '경남',
  '부산',
  '울산',
  '제주',
];
const MONTHS = Array.from({ length: 24 }, (_, i) => ({
  id: `m${i + 1}`,
  year: i < 12 ? 2022 : 2023,
  month: (i % 12) + 1,
}));
export const options = {
  columns: [
    // 지역 컬럼은 왼쪽에 고정해서, 가로로 스크롤해도 어느 지역인지 계속 보여요.
    { id: 'region', title: '지역', width: 110, pinned: 'start' },
    ...MONTHS.map(({ id, year, month }) => ({
      id,
      title: `${year}년 ${month}월`,
      kind: 'number',
      width: 110,
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('ko-KR'),
    })),
  ],
  rows: regions.map((region, r) => ({
    id: r + 1,
    region,
    ...Object.fromEntries(MONTHS.map(({ id }, i) => [
      id,
      4000 + ((r * 7919 + i * 2711 + (r + 1) * (i + 3) * 137) % 9000),
    ])),
  })),
  rowKey: 'id',
};
export const actions = {
  // 보이는 기간을 바꿔요. 숨긴 컬럼은 폭을 기억하고 있다가 다시 보이면 그대로 돌아와요.
  period: (api, value) => {
    for (const { id, year } of MONTHS) {
      api.setColumnVisible(id, value === 'all' || value === undefined || String(year) === value);
    }
  },
  // 지역 컬럼 고정을 켜고 꺼요.
  pin: (api, value) => api.pinColumn('region', value === 'off' ? null : 'start'),
};

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">
    <select aria-label="Period" id="period">
      <option value="all" selected>2022-2023</option>
      <option value="2022">2022 only</option>
      <option value="2023">2023 only</option>
    </select>
    <select aria-label="Region column" id="pin">
      <option value="on" selected>Pinned left</option>
      <option value="off">Not pinned</option>
    </select>
  </div>
  <div id="grid" style="height: 560px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('period').addEventListener('change', (event) => actions.period(api, event.target.value));
document.getElementById('pin').addEventListener('change', (event) => actions.pin(api, event.target.value));
example.js
// Two years of monthly sales by region ($). With 25 columns it is a wide table that does not fit on one screen.
const regions = [
  'Seoul',
  'Gyeonggi-Incheon',
  'Gangwon',
  'Chungbuk',
  'Chungnam',
  'Daejeon',
  'Jeonbuk',
  'Jeonnam',
  'Gwangju',
  'Gyeongbuk',
  'Daegu',
  'Gyeongnam',
  'Busan',
  'Ulsan',
  'Jeju',
];
const MONTHS = Array.from({ length: 24 }, (_, i) => ({
  id: `m${i + 1}`,
  year: i < 12 ? 2022 : 2023,
  month: (i % 12) + 1,
}));
const MONTH_NAMES = [
  'Jan',
  'Feb',
  'Mar',
  'Apr',
  'May',
  'Jun',
  'Jul',
  'Aug',
  'Sep',
  'Oct',
  'Nov',
  'Dec',
];
export const options = {
  columns: [
    // The Region column is pinned on the left, so you can always see which region a row is while scrolling sideways.
    { id: 'region', title: 'Region', width: 150, pinned: 'start' },
    ...MONTHS.map(({ id, year, month }) => ({
      id,
      title: `${MONTH_NAMES[month - 1]} ${year}`,
      kind: 'number',
      width: 110,
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('en-US'),
    })),
  ],
  rows: regions.map((region, r) => ({
    id: r + 1,
    region,
    ...Object.fromEntries(MONTHS.map(({ id }, i) => [
      id,
      10 * (4000 + ((r * 7919 + i * 2711 + (r + 1) * (i + 3) * 137) % 9000)),
    ])),
  })),
  rowKey: 'id',
};
export const actions = {
  // Change the visible period. Hidden columns remember their width and come back unchanged when shown again.
  period: (api, value) => {
    for (const { id, year } of MONTHS) {
      api.setColumnVisible(id, value === 'all' || value === undefined || String(year) === value);
    }
  },
  // Turn pinning of the Region column on or off.
  pin: (api, value) => api.pinColumn('region', value === 'off' ? null : 'start'),
};