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

헤더를 년·분기·월로 묶어서 보여 주기Group headers as year, quarter and month

여러 컬럼을 하나의 제목 아래 묶고, 그 묶음을 또 묶어서 3단 헤더를 만들어요. 분기 옆 버튼으로 접고 펼 수도 있어요. 지역 컬럼은 고정이라 가로로 스크롤해도 남아 있어요.Put columns under a shared title and nest groups into a three-level header. Quarters fold with a button, and the pinned Region column stays while you scroll.

입문Beginner
직접 써 보기Live preview헤더를 년·분기·월로 묶어서 보여 주기Group headers as year, quarter and month

코드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="fold">
      <option value="expanded" selected>모두 펼치기</option>
      <option value="collapsed">모두 접기</option>
    </select>
    <button type="button" id="foldQ2">2분기만 접기</button>
  </div>
  <div id="grid" style="height: 470px"></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('fold').addEventListener('change', (event) => actions.fold(api, event.target.value));
document.getElementById('foldQ2').addEventListener('click', () => actions.foldQ2(api));
example.js
// 2022년 월별 매출(만 원). 년 → 분기 → 월, 3단으로 겹친 헤더예요.
const regions = [
  '서울',
  '경기·인천',
  '강원',
  '충북',
  '충남',
  '대전',
  '전북',
  '전남',
  '광주',
  '경북',
];
const MONTHS = Array.from({ length: 12 }, (_, i) => `m${i + 1}`);
// headerGroups는 "어느 컬럼들을 묶어 어떤 제목을 달지"만 적어요. 컬럼 정의는 그대로 평평하게 두고요.
// collapsible: true를 주면 그룹 제목 옆에 접기 버튼이 생겨요.
export const options = {
  columns: [
    { id: 'region', title: '지역', width: 110, pinned: 'start' },
    ...MONTHS.map((id, i) => ({
      id,
      title: `${i + 1}월`,
      kind: 'number',
      width: 90,
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('ko-KR'),
    })),
  ],
  headerGroups: [
    {
      id: 'y2022',
      title: '2022년',
      children: [1, 2, 3, 4].map((q) => ({
        id: `q${q}`,
        title: `${q}분기`,
        collapsible: true,
        children: [0, 1, 2].map((k) => `m${(q - 1) * 3 + k + 1}`),
      })),
    },
  ],
  rows: regions.map((region, r) => ({
    id: r + 1,
    region,
    ...Object.fromEntries(MONTHS.map((id, i) => [id, 3000 + ((r * 6113 + i * 2297 + r * i * 311) % 9000)])),
  })),
  rowKey: 'id',
  showRowNumbers: true,
};
const QUARTERS = ['q1', 'q2', 'q3', 'q4'];
export function setup(api, log) {
  const off = api.on('headerGroupToggled', ({ groupId, collapsed }) => log({
    ko: `${groupId.replace('q', '')}분기를 ${collapsed ? '접었어요' : '펼쳤어요'}`,
    en: `Quarter ${groupId.replace('q', '')} ${collapsed ? 'collapsed' : 'expanded'}`,
  }));
  return () => off();
}
export const actions = {
  // 분기를 모두 접거나 펼쳐요. 접으면 그 분기의 첫 번째 월만 남아요.
  fold: (api, value) => QUARTERS.forEach((q) => api.setHeaderGroupCollapsed(q, value === 'collapsed')),
  // 2분기 하나만 접어 봐요.
  foldQ2: (api) => api.setHeaderGroupCollapsed('q2', true),
};

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="Quarters" id="fold">
      <option value="expanded" selected>All expanded</option>
      <option value="collapsed">All collapsed</option>
    </select>
    <button type="button" id="foldQ2">Fold Q2 only</button>
  </div>
  <div id="grid" style="height: 470px"></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('fold').addEventListener('change', (event) => actions.fold(api, event.target.value));
document.getElementById('foldQ2').addEventListener('click', () => actions.foldQ2(api));
example.js
// Monthly sales for 2022 ($). A header nested in 3 levels: year > quarter > month.
const regions = [
  'Seoul',
  'Gyeonggi-Incheon',
  'Gangwon',
  'Chungbuk',
  'Chungnam',
  'Daejeon',
  'Jeonbuk',
  'Jeonnam',
  'Gwangju',
  'Gyeongbuk',
];
const MONTHS = Array.from({ length: 12 }, (_, i) => `m${i + 1}`);
// headerGroups only says which columns to group under which title. The column definitions stay flat.
// With collapsible: true, a collapse button appears next to the group title.
export const options = {
  columns: [
    { id: 'region', title: 'Region', width: 170, pinned: 'start' },
    ...MONTHS.map((id, i) => ({
      id,
      title: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][i],
      kind: 'number',
      width: 90,
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('en-US'),
    })),
  ],
  headerGroups: [
    {
      id: 'y2022',
      title: '2022',
      children: [1, 2, 3, 4].map((q) => ({
        id: `q${q}`,
        title: `Q${q}`,
        collapsible: true,
        children: [0, 1, 2].map((k) => `m${(q - 1) * 3 + k + 1}`),
      })),
    },
  ],
  rows: regions.map((region, r) => ({
    id: r + 1,
    region,
    ...Object.fromEntries(MONTHS.map((id, i) => [id, 10 * (3000 + ((r * 6113 + i * 2297 + r * i * 311) % 9000))])),
  })),
  rowKey: 'id',
  showRowNumbers: true,
};
const QUARTERS = ['q1', 'q2', 'q3', 'q4'];
export function setup(api, log) {
  const off = api.on('headerGroupToggled', ({ groupId, collapsed }) => log(`Quarter ${groupId.replace('q', '')} ${collapsed ? 'collapsed' : 'expanded'}`));
  return () => off();
}
export const actions = {
  // Collapse or expand all quarters. A collapsed quarter keeps only its first month.
  fold: (api, value) => QUARTERS.forEach((q) => api.setHeaderGroupCollapsed(q, value === 'collapsed')),
  // Collapse only Q2.
  foldQ2: (api) => api.setHeaderGroupCollapsed('q2', true),
};