그룹을 트리 마디로 보기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'.
코드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)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<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>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));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(),
};<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>그룹을 트리 마디로 보기</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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'은 그룹 행을 따로 그려요. 집계 칸은 그대로 컬럼마다 나와요.
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',
};
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;
}
const actions = {
mode: (api, value) => api.setOptions({ groupMode: value === 'flat' ? 'flat' : 'tree' }),
expandAll: (api) => api.expandAll(),
};
options.ariaLabel = "그룹을 트리 마디로 보기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="그룹 보기"
disabled={!api}
onChange={(event) => api && actions.mode(api, event.target.value)}
>
<option value="tree">트리 마디</option>
<option value="flat">기본(flat)</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.expandAll(api)}
>모두 펼치기</button>
</div>
<Grid style={{ height: 320 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
region: string;
channel: string;
product: string;
amount: number;
}
const regions = ['서울', '부산', '대구'];
const channels = ['온라인', '매장'];
const products = ['무선 이어폰', '노트북', '스마트워치', '태블릿'];
const rows: Row[] = Array.from({ length: 36 }, (_, i) => ({
id: i + 1,
region: regions[i % 3] as string,
channel: channels[Math.floor(i / 3) % 2] as string,
product: products[(i * 5) % 4] as string,
amount: 60 + ((i * 47) % 300),
}));
// groupMode: 'tree'는 그룹을 첫 컬럼(라벨·펼침 단추·행 수)의 트리 마디로 그려요. 기본 'flat'은 그룹 행을 따로 그려요. 집계 칸은 그대로 컬럼마다 나와요.
export const options: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>, value?: string) =>
api.setOptions({ groupMode: value === 'flat' ? 'flat' : 'tree' }),
expandAll: (api: GridApi<Row>) => api.expandAll(),
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="그룹 보기"
:disabled="!api"
@change="api && actions.mode(api, ($event.target as HTMLSelectElement).value)"
>
<option value="tree" selected>트리 마디</option>
<option value="flat">기본(flat)</option>
</select>
<button
type="button"
:disabled="!api"
@click="api && actions.expandAll(api)"
>모두 펼치기</button>
</div>
<Grid style="height: 320px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
region: string;
channel: string;
product: string;
amount: number;
}
const regions = ['서울', '부산', '대구'];
const channels = ['온라인', '매장'];
const products = ['무선 이어폰', '노트북', '스마트워치', '태블릿'];
const rows: Row[] = Array.from({ length: 36 }, (_, i) => ({
id: i + 1,
region: regions[i % 3] as string,
channel: channels[Math.floor(i / 3) % 2] as string,
product: products[(i * 5) % 4] as string,
amount: 60 + ((i * 47) % 300),
}));
// groupMode: 'tree'는 그룹을 첫 컬럼(라벨·펼침 단추·행 수)의 트리 마디로 그려요. 기본 'flat'은 그룹 행을 따로 그려요. 집계 칸은 그대로 컬럼마다 나와요.
export const options: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>, value?: string) =>
api.setOptions({ groupMode: value === 'flat' ? 'flat' : 'tree' }),
expandAll: (api: GridApi<Row>) => api.expandAll(),
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="그룹 보기"
[disabled]="!api"
(change)="api && actions.mode(api, $any($event.target).value)"
>
<option value="tree" selected>트리 마디</option>
<option value="flat">기본(flat)</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.expandAll(api)"
>모두 펼치기</button>
</div>
<mx-grid style="height: 320px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
region: string;
channel: string;
product: string;
amount: number;
}
const regions = ['서울', '부산', '대구'];
const channels = ['온라인', '매장'];
const products = ['무선 이어폰', '노트북', '스마트워치', '태블릿'];
const rows: Row[] = Array.from({ length: 36 }, (_, i) => ({
id: i + 1,
region: regions[i % 3] as string,
channel: channels[Math.floor(i / 3) % 2] as string,
product: products[(i * 5) % 4] as string,
amount: 60 + ((i * 47) % 300),
}));
// groupMode: 'tree'는 그룹을 첫 컬럼(라벨·펼침 단추·행 수)의 트리 마디로 그려요. 기본 'flat'은 그룹 행을 따로 그려요. 집계 칸은 그대로 컬럼마다 나와요.
export const options: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>, value?: string) =>
api.setOptions({ groupMode: value === 'flat' ? 'flat' : 'tree' }),
expandAll: (api: GridApi<Row>) => api.expandAll(),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<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>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));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(),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Show groups as tree nodes</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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.
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',
};
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;
}
const actions = {
mode: (api, value) => api.setOptions({ groupMode: value === 'flat' ? 'flat' : 'tree' }),
expandAll: (api) => api.expandAll(),
};
options.ariaLabel = "Show groups as tree nodes";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="Group view"
disabled={!api}
onChange={(event) => api && actions.mode(api, event.target.value)}
>
<option value="tree">Tree nodes</option>
<option value="flat">Flat</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.expandAll(api)}
>Expand all</button>
</div>
<Grid style={{ height: 320 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
region: string;
channel: string;
product: string;
amount: number;
}
const regions = ['New York', 'Boston', 'Austin'];
const channels = ['Online', 'In store'];
const products = ['Earbuds', 'Laptop', 'Smartwatch', 'Tablet'];
const rows: Row[] = Array.from({ length: 36 }, (_, i) => ({
id: i + 1,
region: regions[i % 3] as string,
channel: channels[Math.floor(i / 3) % 2] as string,
product: products[(i * 5) % 4] as string,
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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>, value?: string) =>
api.setOptions({ groupMode: value === 'flat' ? 'flat' : 'tree' }),
expandAll: (api: GridApi<Row>) => api.expandAll(),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="Group view"
:disabled="!api"
@change="api && actions.mode(api, ($event.target as HTMLSelectElement).value)"
>
<option value="tree" selected>Tree nodes</option>
<option value="flat">Flat</option>
</select>
<button
type="button"
:disabled="!api"
@click="api && actions.expandAll(api)"
>Expand all</button>
</div>
<Grid style="height: 320px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
region: string;
channel: string;
product: string;
amount: number;
}
const regions = ['New York', 'Boston', 'Austin'];
const channels = ['Online', 'In store'];
const products = ['Earbuds', 'Laptop', 'Smartwatch', 'Tablet'];
const rows: Row[] = Array.from({ length: 36 }, (_, i) => ({
id: i + 1,
region: regions[i % 3] as string,
channel: channels[Math.floor(i / 3) % 2] as string,
product: products[(i * 5) % 4] as string,
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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>, value?: string) =>
api.setOptions({ groupMode: value === 'flat' ? 'flat' : 'tree' }),
expandAll: (api: GridApi<Row>) => api.expandAll(),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="Group view"
[disabled]="!api"
(change)="api && actions.mode(api, $any($event.target).value)"
>
<option value="tree" selected>Tree nodes</option>
<option value="flat">Flat</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.expandAll(api)"
>Expand all</button>
</div>
<mx-grid style="height: 320px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
region: string;
channel: string;
product: string;
amount: number;
}
const regions = ['New York', 'Boston', 'Austin'];
const channels = ['Online', 'In store'];
const products = ['Earbuds', 'Laptop', 'Smartwatch', 'Tablet'];
const rows: Row[] = Array.from({ length: 36 }, (_, i) => ({
id: i + 1,
region: regions[i % 3] as string,
channel: channels[Math.floor(i / 3) % 2] as string,
product: products[(i * 5) % 4] as string,
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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>, value?: string) =>
api.setOptions({ groupMode: value === 'flat' ? 'flat' : 'tree' }),
expandAll: (api: GridApi<Row>) => api.expandAll(),
};