헤더를 년·분기·월로 묶어서 보여 주기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.
코드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="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>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));// 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),
};<!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="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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// 2022년 월별 매출(만 원). 년 → 분기 → 월, 3단으로 겹친 헤더예요.
const regions = [
'서울',
'경기·인천',
'강원',
'충북',
'충남',
'대전',
'전북',
'전남',
'광주',
'경북',
];
const MONTHS = Array.from({ length: 12 }, (_, i) => `m${i + 1}`);
// headerGroups는 "어느 컬럼들을 묶어 어떤 제목을 달지"만 적어요. 컬럼 정의는 그대로 평평하게 두고요.
// collapsible: true를 주면 그룹 제목 옆에 접기 버튼이 생겨요.
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'];
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();
}
const actions = {
// 분기를 모두 접거나 펼쳐요. 접으면 그 분기의 첫 번째 월만 남아요.
fold: (api, value) => QUARTERS.forEach((q) => api.setHeaderGroupCollapsed(q, value === 'collapsed')),
// 2분기 하나만 접어 봐요.
foldQ2: (api) => api.setHeaderGroupCollapsed('q2', true),
};
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('fold').addEventListener('change', (event) => actions.fold(api, event.target.value));
document.getElementById('foldQ2').addEventListener('click', () => actions.foldQ2(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.fold(api, event.target.value)}
>
<option value="expanded">모두 펼치기</option>
<option value="collapsed">모두 접기</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.foldQ2(api)}
>2분기만 접기</button>
</div>
<Grid style={{ height: 470 }} 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;
[month: string]: string | number;
}
// 2022년 월별 매출(만 원). 년 → 분기 → 월, 3단으로 겹친 헤더예요.
const regions = [
'서울',
'경기·인천',
'강원',
'충북',
'충남',
'대전',
'전북',
'전남',
'광주',
'경북',
];
const MONTHS = Array.from({ length: 12 }, (_, i) => `m${i + 1}`);
// headerGroups는 "어느 컬럼들을 묶어 어떤 제목을 달지"만 적어요. 컬럼 정의는 그대로 평평하게 두고요.
// collapsible: true를 주면 그룹 제목 옆에 접기 버튼이 생겨요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'region', title: '지역', width: 110, pinned: 'start' },
...MONTHS.map((id, i) => ({
id,
title: `${i + 1}월`,
kind: 'number' as const,
width: 90,
align: 'end' as const,
formatValue: (value: unknown) => 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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>, value?: string) =>
QUARTERS.forEach((q) => api.setHeaderGroupCollapsed(q, value === 'collapsed')),
// 2분기 하나만 접어 봐요.
foldQ2: (api: GridApi<Row>) => api.setHeaderGroupCollapsed('q2', true),
};버튼을 누르면 이 예제의 코드가 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.fold(api, ($event.target as HTMLSelectElement).value)"
>
<option value="expanded" selected>모두 펼치기</option>
<option value="collapsed">모두 접기</option>
</select>
<button type="button" :disabled="!api" @click="api && actions.foldQ2(api)">2분기만 접기</button>
</div>
<Grid style="height: 470px" :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;
[month: string]: string | number;
}
// 2022년 월별 매출(만 원). 년 → 분기 → 월, 3단으로 겹친 헤더예요.
const regions = [
'서울',
'경기·인천',
'강원',
'충북',
'충남',
'대전',
'전북',
'전남',
'광주',
'경북',
];
const MONTHS = Array.from({ length: 12 }, (_, i) => `m${i + 1}`);
// headerGroups는 "어느 컬럼들을 묶어 어떤 제목을 달지"만 적어요. 컬럼 정의는 그대로 평평하게 두고요.
// collapsible: true를 주면 그룹 제목 옆에 접기 버튼이 생겨요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'region', title: '지역', width: 110, pinned: 'start' },
...MONTHS.map((id, i) => ({
id,
title: `${i + 1}월`,
kind: 'number' as const,
width: 90,
align: 'end' as const,
formatValue: (value: unknown) => 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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>, value?: string) =>
QUARTERS.forEach((q) => api.setHeaderGroupCollapsed(q, value === 'collapsed')),
// 2분기 하나만 접어 봐요.
foldQ2: (api: GridApi<Row>) => api.setHeaderGroupCollapsed('q2', true),
};버튼을 누르면 이 예제의 코드가 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.fold(api, $any($event.target).value)"
>
<option value="expanded" selected>모두 펼치기</option>
<option value="collapsed">모두 접기</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.foldQ2(api)"
>2분기만 접기</button>
</div>
<mx-grid style="height: 470px" [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;
[month: string]: string | number;
}
// 2022년 월별 매출(만 원). 년 → 분기 → 월, 3단으로 겹친 헤더예요.
const regions = [
'서울',
'경기·인천',
'강원',
'충북',
'충남',
'대전',
'전북',
'전남',
'광주',
'경북',
];
const MONTHS = Array.from({ length: 12 }, (_, i) => `m${i + 1}`);
// headerGroups는 "어느 컬럼들을 묶어 어떤 제목을 달지"만 적어요. 컬럼 정의는 그대로 평평하게 두고요.
// collapsible: true를 주면 그룹 제목 옆에 접기 버튼이 생겨요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'region', title: '지역', width: 110, pinned: 'start' },
...MONTHS.map((id, i) => ({
id,
title: `${i + 1}월`,
kind: 'number' as const,
width: 90,
align: 'end' as const,
formatValue: (value: unknown) => 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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>, value?: string) =>
QUARTERS.forEach((q) => api.setHeaderGroupCollapsed(q, value === 'collapsed')),
// 2분기 하나만 접어 봐요.
foldQ2: (api: GridApi<Row>) => api.setHeaderGroupCollapsed('q2', true),
};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="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>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));// 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),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Group headers as year, quarter and month</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// 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.
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'];
function setup(api, log) {
const off = api.on('headerGroupToggled', ({ groupId, collapsed }) => log(`Quarter ${groupId.replace('q', '')} ${collapsed ? 'collapsed' : 'expanded'}`));
return () => off();
}
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),
};
options.ariaLabel = "Group headers as year, quarter and month";
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('fold').addEventListener('change', (event) => actions.fold(api, event.target.value));
document.getElementById('foldQ2').addEventListener('click', () => actions.foldQ2(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="Quarters"
disabled={!api}
onChange={(event) => api && actions.fold(api, event.target.value)}
>
<option value="expanded">All expanded</option>
<option value="collapsed">All collapsed</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.foldQ2(api)}
>Fold Q2 only</button>
</div>
<Grid style={{ height: 470 }} 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;
[month: string]: string | number;
}
// 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: GridOptions<Row> = {
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
] as string,
kind: 'number' as const,
width: 90,
align: 'end' as const,
formatValue: (value: unknown) => 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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>, value?: string) =>
QUARTERS.forEach((q) => api.setHeaderGroupCollapsed(q, value === 'collapsed')),
// Collapse only Q2.
foldQ2: (api: GridApi<Row>) => api.setHeaderGroupCollapsed('q2', true),
};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="Quarters"
:disabled="!api"
@change="api && actions.fold(api, ($event.target as HTMLSelectElement).value)"
>
<option value="expanded" selected>All expanded</option>
<option value="collapsed">All collapsed</option>
</select>
<button type="button" :disabled="!api" @click="api && actions.foldQ2(api)">Fold Q2 only</button>
</div>
<Grid style="height: 470px" :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;
[month: string]: string | number;
}
// 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: GridOptions<Row> = {
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
] as string,
kind: 'number' as const,
width: 90,
align: 'end' as const,
formatValue: (value: unknown) => 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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>, value?: string) =>
QUARTERS.forEach((q) => api.setHeaderGroupCollapsed(q, value === 'collapsed')),
// Collapse only Q2.
foldQ2: (api: GridApi<Row>) => api.setHeaderGroupCollapsed('q2', true),
};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="Quarters"
[disabled]="!api"
(change)="api && actions.fold(api, $any($event.target).value)"
>
<option value="expanded" selected>All expanded</option>
<option value="collapsed">All collapsed</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.foldQ2(api)"
>Fold Q2 only</button>
</div>
<mx-grid style="height: 470px" [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;
[month: string]: string | number;
}
// 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: GridOptions<Row> = {
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
] as string,
kind: 'number' as const,
width: 90,
align: 'end' as const,
formatValue: (value: unknown) => 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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>, value?: string) =>
QUARTERS.forEach((q) => api.setHeaderGroupCollapsed(q, value === 'collapsed')),
// Collapse only Q2.
foldQ2: (api: GridApi<Row>) => api.setHeaderGroupCollapsed('q2', true),
};