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

그룹을 트리 마디로 보기Show groups as tree nodes

groupMode: 'tree'면 그룹이 첫 컬럼의 트리 마디로 보여요: 들여쓰기·펼침 단추·라벨과 행 수. ←→로 접고 펴요. 기본 'flat'과 바꿔 가며 비교해 보세요.With groupMode: 'tree' groups are tree nodes in the first column: indent, toggle, label and row count, and the arrow keys collapse and expand. Compare with the default 'flat'.

입문Beginner
직접 써 보기Live preview그룹을 트리 마디로 보기Show groups as tree nodes

코드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="mode">
      <option value="tree" selected>트리 마디</option>
      <option value="flat">기본(flat)</option>
    </select>
    <button type="button" id="expandAll">모두 펼치기</button>
  </div>
  <div id="grid" style="height: 320px"></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('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
example.js
const regions = ['서울', '부산', '대구'];
const channels = ['온라인', '매장'];
const products = ['무선 이어폰', '노트북', '스마트워치', '태블릿'];
const rows = Array.from({ length: 36 }, (_, i) => ({
  id: i + 1,
  region: regions[i % 3],
  channel: channels[Math.floor(i / 3) % 2],
  product: products[(i * 5) % 4],
  amount: 60 + ((i * 47) % 300),
}));
// groupMode: 'tree'는 그룹을 첫 컬럼(라벨·펼침 단추·행 수)의 트리 마디로 그려요. 기본 'flat'은 그룹 행을 따로 그려요. 집계 칸은 그대로 컬럼마다 나와요.
export const options = {
  columns: [
    { id: 'product', title: '상품', width: 190 },
    { id: 'region', title: '지역', width: 100 },
    { id: 'channel', title: '채널', width: 100 },
    { id: 'amount', title: '금액 ($)', kind: 'number', width: 120, align: 'end', aggregate: 'sum' },
  ],
  rows,
  rowKey: 'id',
  grouping: { by: ['region', 'channel'], expandDepth: 1 },
  groupMode: 'tree',
};
export function setup(_api, log) {
  log({
    ko: '그룹이 첫 컬럼의 트리 마디로 보여요. 라벨 옆에 행 수가 나오고, ←→로 접고 펴요.',
    en: 'Groups are tree nodes in the first column with the row count beside the label. Collapse and expand with the arrow keys.',
  });
  return () => undefined;
}
export const actions = {
  mode: (api, value) => api.setOptions({ groupMode: value === 'flat' ? 'flat' : 'tree' }),
  expandAll: (api) => api.expandAll(),
};

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="Group view" id="mode">
      <option value="tree" selected>Tree nodes</option>
      <option value="flat">Flat</option>
    </select>
    <button type="button" id="expandAll">Expand all</button>
  </div>
  <div id="grid" style="height: 320px"></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('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
example.js
const regions = ['New York', 'Boston', 'Austin'];
const channels = ['Online', 'In store'];
const products = ['Earbuds', 'Laptop', 'Smartwatch', 'Tablet'];
const rows = Array.from({ length: 36 }, (_, i) => ({
  id: i + 1,
  region: regions[i % 3],
  channel: channels[Math.floor(i / 3) % 2],
  product: products[(i * 5) % 4],
  amount: 60 + ((i * 47) % 300),
}));
// groupMode: 'tree' draws groups as tree nodes in the first column (label, toggle, row count). The default 'flat' draws separate group rows. Aggregates stay in their own columns.
export const options = {
  columns: [
    { id: 'product', title: 'Product', width: 190 },
    { id: 'region', title: 'Region', width: 100 },
    { id: 'channel', title: 'Channel', width: 100 },
    {
      id: 'amount',
      title: 'Amount ($)',
      kind: 'number',
      width: 120,
      align: 'end',
      aggregate: 'sum',
    },
  ],
  rows,
  rowKey: 'id',
  grouping: { by: ['region', 'channel'], expandDepth: 1 },
  groupMode: 'tree',
};
export function setup(_api, log) {
  log('Groups are tree nodes in the first column with the row count beside the label. Collapse and expand with the arrow keys.');
  return () => undefined;
}
export const actions = {
  mode: (api, value) => api.setOptions({ groupMode: value === 'flat' ? 'flat' : 'tree' }),
  expandAll: (api) => api.expandAll(),
};