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

묶음 헤더가 있는 예산 트리A budget tree with grouped headers

컬럼 위에 "상반기 > 1분기·2분기" 같은 묶음 헤더를 달고, 본부·팀 줄에는 아래 항목의 합계를 보여 줘요. 분기 묶음은 헤더에서 접을 수 있어요.Put grouped headers such as "H1 > Q1, Q2" above the columns, and let department rows show the sum of their items. Quarter groups fold from the header.

활용Intermediate
직접 써 보기Live preview묶음 헤더가 있는 예산 트리A budget tree with grouped headers

코드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="quarter">
      <option value="open" selected>펼치기</option>
      <option value="closed">접기</option>
    </select>
    <button type="button" id="expandAll">모두 열기</button>
    <button type="button" id="collapseAll">모두 닫기</button>
  </div>
  <div id="grid" style="height: 340px"></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('quarter').addEventListener('change', (event) => actions.quarter(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));
example.js
// 본부 > 팀 > 항목 세 단계의 월별 예산(백만원)이에요. 맨 아래 항목만 숫자를 갖고, 본부·팀 줄은 아래 줄의 합계를 보여 줘요.
const leaves = [
  ['B111', 'B11', '인건비'],
  ['B112', 'B11', '광고비'],
  ['B121', 'B12', '출장비'],
  ['B122', 'B12', '수수료'],
  ['B211', 'B21', '서버 비용'],
  ['B212', 'B21', '도구 구독'],
  ['B221', 'B22', '외주비'],
  ['B222', 'B22', '교육비'],
];
const month = (i, k) => 20 + ((i * 17 + k * 9) % 60);
const empty = { m1: 0, m2: 0, m3: 0, m4: 0, m5: 0, m6: 0 };
const rows = [
  { id: 'B1', parentId: null, name: '영업본부', ...empty },
  { id: 'B11', parentId: 'B1', name: '국내영업', ...empty },
  { id: 'B12', parentId: 'B1', name: '해외영업', ...empty },
  { id: 'B2', parentId: null, name: '개발본부', ...empty },
  { id: 'B21', parentId: 'B2', name: '플랫폼팀', ...empty },
  { id: 'B22', parentId: 'B2', name: '앱개발팀', ...empty },
  ...leaves.map(([id, parentId, name], i) => ({
    id,
    parentId,
    name,
    m1: month(i, 1),
    m2: month(i, 2),
    m3: month(i, 3),
    m4: month(i, 4),
    m5: month(i, 5),
    m6: month(i, 6),
  })),
];
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'];
const money = (value) => Number(value).toLocaleString('ko-KR');
export const options = {
  columns: [
    { id: 'name', title: '항목', width: 160 },
    ...months.map((id, i) => ({
      id,
      title: `${i + 1}월`,
      kind: 'number',
      width: 90,
      align: 'end',
      aggregate: 'sum',
      formatValue: money,
      aggregateFormat: money,
    })),
    {
      id: 'total',
      title: '합계',
      kind: 'number',
      width: 110,
      align: 'end',
      getValue: (row) => months.reduce((sum, key) => sum + row[key], 0),
      aggregate: 'sum',
      formatValue: money,
      aggregateFormat: money,
    },
  ],
  // 컬럼을 묶는 헤더예요. 묶음 안에 묶음을 넣을 수 있고, collapsible이면 헤더의 토글로 접을 수 있어요.
  headerGroups: [
    {
      id: 'h1',
      title: '2026 상반기',
      children: [
        { id: 'q1', title: '1분기', collapsible: true, children: ['m1', 'm2', 'm3'] },
        { id: 'q2', title: '2분기', collapsible: true, children: ['m4', 'm5', 'm6'] },
      ],
    },
  ],
  rows,
  rowKey: 'id',
  // parentAggregates: 부모 줄은 자기 값 대신 아래 줄들의 합계(aggregate: 'sum')를 보여 줘요.
  tree: { parentKey: 'parentId', column: 'name', expandDepth: 1, parentAggregates: true },
};
export const actions = {
  quarter: (api, value) => {
    api.setHeaderGroupCollapsed('q1', value === 'closed');
    api.setHeaderGroupCollapsed('q2', value === 'closed');
  },
  expandAll: (api) => api.expandAll(),
  collapseAll: (api) => api.collapseAll(),
};

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="Quarter groups" id="quarter">
      <option value="open" selected>Open</option>
      <option value="closed">Fold</option>
    </select>
    <button type="button" id="expandAll">Expand all</button>
    <button type="button" id="collapseAll">Collapse all</button>
  </div>
  <div id="grid" style="height: 340px"></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('quarter').addEventListener('change', (event) => actions.quarter(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));
example.js
// Monthly budget (in US dollars) in three levels: division > team > item. Only the bottom items hold numbers; division and team rows show the sum of the rows below.
const leaves = [
  ['B111', 'B11', 'Payroll'],
  ['B112', 'B11', 'Advertising'],
  ['B121', 'B12', 'Travel'],
  ['B122', 'B12', 'Fees'],
  ['B211', 'B21', 'Server costs'],
  ['B212', 'B21', 'Tool subscriptions'],
  ['B221', 'B22', 'Outsourcing'],
  ['B222', 'B22', 'Training'],
];
const month = (i, k) => (20 + ((i * 17 + k * 9) % 60)) * 1000;
const empty = { m1: 0, m2: 0, m3: 0, m4: 0, m5: 0, m6: 0 };
const rows = [
  { id: 'B1', parentId: null, name: 'Sales Division', ...empty },
  { id: 'B11', parentId: 'B1', name: 'Domestic Sales', ...empty },
  { id: 'B12', parentId: 'B1', name: 'Overseas Sales', ...empty },
  { id: 'B2', parentId: null, name: 'Dev Division', ...empty },
  { id: 'B21', parentId: 'B2', name: 'Platform Team', ...empty },
  { id: 'B22', parentId: 'B2', name: 'App Dev Team', ...empty },
  ...leaves.map(([id, parentId, name], i) => ({
    id,
    parentId,
    name,
    m1: month(i, 1),
    m2: month(i, 2),
    m3: month(i, 3),
    m4: month(i, 4),
    m5: month(i, 5),
    m6: month(i, 6),
  })),
];
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'];
const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
const money = (value) => Number(value).toLocaleString('en-US');
export const options = {
  columns: [
    { id: 'name', title: 'Item', width: 200 },
    ...months.map((id, i) => ({
      id,
      title: monthNames[i],
      kind: 'number',
      width: 110,
      align: 'end',
      aggregate: 'sum',
      formatValue: money,
      aggregateFormat: money,
    })),
    {
      id: 'total',
      title: 'Total',
      kind: 'number',
      width: 130,
      align: 'end',
      getValue: (row) => months.reduce((sum, key) => sum + row[key], 0),
      aggregate: 'sum',
      formatValue: money,
      aggregateFormat: money,
    },
  ],
  // Headers that group columns. Groups can nest, and a collapsible group folds with the toggle in its header.
  headerGroups: [
    {
      id: 'h1',
      title: '2026 H1',
      children: [
        { id: 'q1', title: 'Q1', collapsible: true, children: ['m1', 'm2', 'm3'] },
        { id: 'q2', title: 'Q2', collapsible: true, children: ['m4', 'm5', 'm6'] },
      ],
    },
  ],
  rows,
  rowKey: 'id',
  // parentAggregates: a parent row shows the sum (aggregate: 'sum') of the rows below instead of its own value.
  tree: { parentKey: 'parentId', column: 'name', expandDepth: 1, parentAggregates: true },
};
export const actions = {
  quarter: (api, value) => {
    api.setHeaderGroupCollapsed('q1', value === 'closed');
    api.setHeaderGroupCollapsed('q2', value === 'closed');
  },
  expandAll: (api) => api.expandAll(),
  collapseAll: (api) => api.collapseAll(),
};