그룹 이름을 한 칸에 겹쳐 보여 주기Show nested group names in a single column
지역 → 담당자 그룹 이름을 '품목 / 그룹' 한 칸에 들여쓰기로 겹쳐 보여 줘서, 같은 값이 칸마다 반복되지 않아요. 지역·담당자 컬럼은 숨기고, 그룹이 끝나는 곳의 소계 줄과 전체 합계는 그대로 나와요.Region and manager group names nest by indentation in one Item / Group column, so values do not repeat in every cell. The region and manager columns are hidden while subtotal rows and the grand total stay.
코드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="columns">
<option value="hide" selected>숨기기</option>
<option value="show">보이기</option>
</select>
<select aria-label="펼침" id="depth">
<option value="all" selected>모두 펼치기</option>
<option value="region">지역만 펼치기</option>
<option value="none">모두 접기</option>
</select>
</div>
<div id="grid" style="height: 740px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('columns').addEventListener('change', (event) => actions.columns(api, event.target.value));
document.getElementById('depth').addEventListener('change', (event) => actions.depth(api, event.target.value));const PEOPLE = {
서울: ['김민준', '이서연'],
부산: ['박지호', '최유나'],
};
const items = ['A4 용지', '복합기 토너', '사무용 의자', '모니터 받침대', '화이트보드'];
const rows = [];
for (const [region, managers] of Object.entries(PEOPLE)) {
managers.forEach((manager, m) => {
for (let k = 0; k < 2; k += 1) {
const id = rows.length + 1;
rows.push({
id,
region,
manager,
item: items[(id * 3 + m) % 5],
quantity: 2 + ((id * 7) % 18),
amount: 18000 + ((id * 41000) % 260000),
});
}
});
}
// 그룹 이름(지역 → 담당자)을 한 칸('품목' 컬럼)에 들여쓰기로 겹쳐 보여 주고,
// 값이 반복되는 지역·담당자 컬럼은 숨겨요. 컬럼 순서는 그대로예요. 소계 줄은 그룹 끝에 붙어요.
const columns = (showGroupColumns) => [
{ id: 'region', title: '지역', width: 90, hidden: !showGroupColumns },
{ id: 'manager', title: '담당자', width: 110, hidden: !showGroupColumns },
{ id: 'item', title: '품목 / 그룹', width: 220, aggregate: 'rowCount' },
{ id: 'quantity', title: '수량', kind: 'number', width: 90, align: 'end', aggregate: 'sum' },
{
id: 'amount',
title: '금액(원)',
kind: 'number',
width: 130,
align: 'end',
aggregate: 'sum',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
];
export const options = {
columns: columns(false),
rows,
rowKey: 'id',
// column: 그룹 이름과 펼침 버튼을 그릴 컬럼이에요.
grouping: { by: ['region', 'manager'], column: 'item', expandDepth: 'all', summary: 'footer' },
summary: { label: '전체 합계' },
};
export const actions = {
// 숨긴 지역·담당자 컬럼을 다시 보이게 하거나 숨겨요(그룹은 그대로예요).
columns: (api, value) => {
const show = value === 'show';
api.setColumnVisible('region', show);
api.setColumnVisible('manager', show);
},
depth: (api, value) => {
if (value === 'none')
api.collapseAll();
else if (value === 'region')
api.expandToDepth(1);
else
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="columns">
<option value="hide" selected>숨기기</option>
<option value="show">보이기</option>
</select>
<select aria-label="펼침" id="depth">
<option value="all" selected>모두 펼치기</option>
<option value="region">지역만 펼치기</option>
<option value="none">모두 접기</option>
</select>
</div>
<div id="grid" style="height: 740px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const PEOPLE = {
서울: ['김민준', '이서연'],
부산: ['박지호', '최유나'],
};
const items = ['A4 용지', '복합기 토너', '사무용 의자', '모니터 받침대', '화이트보드'];
const rows = [];
for (const [region, managers] of Object.entries(PEOPLE)) {
managers.forEach((manager, m) => {
for (let k = 0; k < 2; k += 1) {
const id = rows.length + 1;
rows.push({
id,
region,
manager,
item: items[(id * 3 + m) % 5],
quantity: 2 + ((id * 7) % 18),
amount: 18000 + ((id * 41000) % 260000),
});
}
});
}
// 그룹 이름(지역 → 담당자)을 한 칸('품목' 컬럼)에 들여쓰기로 겹쳐 보여 주고,
// 값이 반복되는 지역·담당자 컬럼은 숨겨요. 컬럼 순서는 그대로예요. 소계 줄은 그룹 끝에 붙어요.
const columns = (showGroupColumns) => [
{ id: 'region', title: '지역', width: 90, hidden: !showGroupColumns },
{ id: 'manager', title: '담당자', width: 110, hidden: !showGroupColumns },
{ id: 'item', title: '품목 / 그룹', width: 220, aggregate: 'rowCount' },
{ id: 'quantity', title: '수량', kind: 'number', width: 90, align: 'end', aggregate: 'sum' },
{
id: 'amount',
title: '금액(원)',
kind: 'number',
width: 130,
align: 'end',
aggregate: 'sum',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
];
const options = {
columns: columns(false),
rows,
rowKey: 'id',
// column: 그룹 이름과 펼침 버튼을 그릴 컬럼이에요.
grouping: { by: ['region', 'manager'], column: 'item', expandDepth: 'all', summary: 'footer' },
summary: { label: '전체 합계' },
};
const actions = {
// 숨긴 지역·담당자 컬럼을 다시 보이게 하거나 숨겨요(그룹은 그대로예요).
columns: (api, value) => {
const show = value === 'show';
api.setColumnVisible('region', show);
api.setColumnVisible('manager', show);
},
depth: (api, value) => {
if (value === 'none')
api.collapseAll();
else if (value === 'region')
api.expandToDepth(1);
else
api.expandAll();
},
};
options.ariaLabel = "그룹 이름을 한 칸에 겹쳐 보여 주기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('columns').addEventListener('change', (event) => actions.columns(api, event.target.value));
document.getElementById('depth').addEventListener('change', (event) => actions.depth(api, event.target.value));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
return (
<>
<div className="toolbar">
<select
aria-label="지역·담당자 컬럼"
disabled={!api}
onChange={(event) => api && actions.columns(api, event.target.value)}
>
<option value="hide">숨기기</option>
<option value="show">보이기</option>
</select>
<select defaultValue="hide"
aria-label="펼침"
disabled={!api}
onChange={(event) => api && actions.depth(api, event.target.value)}
>
<option value="all">모두 펼치기</option>
<option value="region">지역만 펼치기</option>
<option value="none">모두 접기</option>
</select>
</div>
<Grid style={{ height: 740 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
region: string;
manager: string;
item: string;
quantity: number;
amount: number;
}
const PEOPLE: Record<string, string[]> = {
서울: ['김민준', '이서연'],
부산: ['박지호', '최유나'],
};
const items = ['A4 용지', '복합기 토너', '사무용 의자', '모니터 받침대', '화이트보드'];
const rows: Row[] = [];
for (const [region, managers] of Object.entries(PEOPLE)) {
managers.forEach((manager, m) => {
for (let k = 0; k < 2; k += 1) {
const id = rows.length + 1;
rows.push({
id,
region,
manager,
item: items[(id * 3 + m) % 5] as string,
quantity: 2 + ((id * 7) % 18),
amount: 18000 + ((id * 41000) % 260000),
});
}
});
}
// 그룹 이름(지역 → 담당자)을 한 칸('품목' 컬럼)에 들여쓰기로 겹쳐 보여 주고,
// 값이 반복되는 지역·담당자 컬럼은 숨겨요. 컬럼 순서는 그대로예요. 소계 줄은 그룹 끝에 붙어요.
const columns = (showGroupColumns: boolean): ColumnDef<Row>[] => [
{ id: 'region', title: '지역', width: 90, hidden: !showGroupColumns },
{ id: 'manager', title: '담당자', width: 110, hidden: !showGroupColumns },
{ id: 'item', title: '품목 / 그룹', width: 220, aggregate: 'rowCount' },
{ id: 'quantity', title: '수량', kind: 'number', width: 90, align: 'end', aggregate: 'sum' },
{
id: 'amount',
title: '금액(원)',
kind: 'number',
width: 130,
align: 'end',
aggregate: 'sum',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
];
export const options: GridOptions<Row> = {
columns: columns(false),
rows,
rowKey: 'id',
// column: 그룹 이름과 펼침 버튼을 그릴 컬럼이에요.
grouping: { by: ['region', 'manager'], column: 'item', expandDepth: 'all', summary: 'footer' },
summary: { label: '전체 합계' },
};
export const actions = {
// 숨긴 지역·담당자 컬럼을 다시 보이게 하거나 숨겨요(그룹은 그대로예요).
columns: (api: GridApi<Row>, value?: string) => {
const show = value === 'show';
api.setColumnVisible('region', show);
api.setColumnVisible('manager', show);
},
depth: (api: GridApi<Row>, value?: string) => {
if (value === 'none') api.collapseAll();
else if (value === 'region') api.expandToDepth(1);
else api.expandAll();
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
}
</script>
<template>
<div class="toolbar">
<select
aria-label="지역·담당자 컬럼"
:disabled="!api"
@change="api && actions.columns(api, ($event.target as HTMLSelectElement).value)"
>
<option value="hide" selected>숨기기</option>
<option value="show">보이기</option>
</select>
<select
aria-label="펼침"
:disabled="!api"
@change="api && actions.depth(api, ($event.target as HTMLSelectElement).value)"
>
<option value="all" selected>모두 펼치기</option>
<option value="region">지역만 펼치기</option>
<option value="none">모두 접기</option>
</select>
</div>
<Grid style="height: 740px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
region: string;
manager: string;
item: string;
quantity: number;
amount: number;
}
const PEOPLE: Record<string, string[]> = {
서울: ['김민준', '이서연'],
부산: ['박지호', '최유나'],
};
const items = ['A4 용지', '복합기 토너', '사무용 의자', '모니터 받침대', '화이트보드'];
const rows: Row[] = [];
for (const [region, managers] of Object.entries(PEOPLE)) {
managers.forEach((manager, m) => {
for (let k = 0; k < 2; k += 1) {
const id = rows.length + 1;
rows.push({
id,
region,
manager,
item: items[(id * 3 + m) % 5] as string,
quantity: 2 + ((id * 7) % 18),
amount: 18000 + ((id * 41000) % 260000),
});
}
});
}
// 그룹 이름(지역 → 담당자)을 한 칸('품목' 컬럼)에 들여쓰기로 겹쳐 보여 주고,
// 값이 반복되는 지역·담당자 컬럼은 숨겨요. 컬럼 순서는 그대로예요. 소계 줄은 그룹 끝에 붙어요.
const columns = (showGroupColumns: boolean): ColumnDef<Row>[] => [
{ id: 'region', title: '지역', width: 90, hidden: !showGroupColumns },
{ id: 'manager', title: '담당자', width: 110, hidden: !showGroupColumns },
{ id: 'item', title: '품목 / 그룹', width: 220, aggregate: 'rowCount' },
{ id: 'quantity', title: '수량', kind: 'number', width: 90, align: 'end', aggregate: 'sum' },
{
id: 'amount',
title: '금액(원)',
kind: 'number',
width: 130,
align: 'end',
aggregate: 'sum',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
];
export const options: GridOptions<Row> = {
columns: columns(false),
rows,
rowKey: 'id',
// column: 그룹 이름과 펼침 버튼을 그릴 컬럼이에요.
grouping: { by: ['region', 'manager'], column: 'item', expandDepth: 'all', summary: 'footer' },
summary: { label: '전체 합계' },
};
export const actions = {
// 숨긴 지역·담당자 컬럼을 다시 보이게 하거나 숨겨요(그룹은 그대로예요).
columns: (api: GridApi<Row>, value?: string) => {
const show = value === 'show';
api.setColumnVisible('region', show);
api.setColumnVisible('manager', show);
},
depth: (api: GridApi<Row>, value?: string) => {
if (value === 'none') api.collapseAll();
else if (value === 'region') api.expandToDepth(1);
else api.expandAll();
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, 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.columns(api, $any($event.target).value)"
>
<option value="hide" selected>숨기기</option>
<option value="show">보이기</option>
</select>
<select
aria-label="펼침"
[disabled]="!api"
(change)="api && actions.depth(api, $any($event.target).value)"
>
<option value="all" selected>모두 펼치기</option>
<option value="region">지역만 펼치기</option>
<option value="none">모두 접기</option>
</select>
</div>
<mx-grid style="height: 740px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
}
}
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
region: string;
manager: string;
item: string;
quantity: number;
amount: number;
}
const PEOPLE: Record<string, string[]> = {
서울: ['김민준', '이서연'],
부산: ['박지호', '최유나'],
};
const items = ['A4 용지', '복합기 토너', '사무용 의자', '모니터 받침대', '화이트보드'];
const rows: Row[] = [];
for (const [region, managers] of Object.entries(PEOPLE)) {
managers.forEach((manager, m) => {
for (let k = 0; k < 2; k += 1) {
const id = rows.length + 1;
rows.push({
id,
region,
manager,
item: items[(id * 3 + m) % 5] as string,
quantity: 2 + ((id * 7) % 18),
amount: 18000 + ((id * 41000) % 260000),
});
}
});
}
// 그룹 이름(지역 → 담당자)을 한 칸('품목' 컬럼)에 들여쓰기로 겹쳐 보여 주고,
// 값이 반복되는 지역·담당자 컬럼은 숨겨요. 컬럼 순서는 그대로예요. 소계 줄은 그룹 끝에 붙어요.
const columns = (showGroupColumns: boolean): ColumnDef<Row>[] => [
{ id: 'region', title: '지역', width: 90, hidden: !showGroupColumns },
{ id: 'manager', title: '담당자', width: 110, hidden: !showGroupColumns },
{ id: 'item', title: '품목 / 그룹', width: 220, aggregate: 'rowCount' },
{ id: 'quantity', title: '수량', kind: 'number', width: 90, align: 'end', aggregate: 'sum' },
{
id: 'amount',
title: '금액(원)',
kind: 'number',
width: 130,
align: 'end',
aggregate: 'sum',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
];
export const options: GridOptions<Row> = {
columns: columns(false),
rows,
rowKey: 'id',
// column: 그룹 이름과 펼침 버튼을 그릴 컬럼이에요.
grouping: { by: ['region', 'manager'], column: 'item', expandDepth: 'all', summary: 'footer' },
summary: { label: '전체 합계' },
};
export const actions = {
// 숨긴 지역·담당자 컬럼을 다시 보이게 하거나 숨겨요(그룹은 그대로예요).
columns: (api: GridApi<Row>, value?: string) => {
const show = value === 'show';
api.setColumnVisible('region', show);
api.setColumnVisible('manager', show);
},
depth: (api: GridApi<Row>, value?: string) => {
if (value === 'none') api.collapseAll();
else if (value === 'region') api.expandToDepth(1);
else 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="Region and manager columns" id="columns">
<option value="hide" selected>Hidden</option>
<option value="show">Show</option>
</select>
<select aria-label="Expansion" id="depth">
<option value="all" selected>Expand all</option>
<option value="region">Regions only</option>
<option value="none">Collapse all</option>
</select>
</div>
<div id="grid" style="height: 740px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('columns').addEventListener('change', (event) => actions.columns(api, event.target.value));
document.getElementById('depth').addEventListener('change', (event) => actions.depth(api, event.target.value));const PEOPLE = {
'New York': ['Liam Carter', 'Emma Stone'],
Boston: ['Noah Kim', 'Olivia Park'],
};
const items = ['Letter paper', 'Printer toner', 'Office chair', 'Monitor stand', 'Whiteboard'];
const rows = [];
for (const [region, managers] of Object.entries(PEOPLE)) {
managers.forEach((manager, m) => {
for (let k = 0; k < 2; k += 1) {
const id = rows.length + 1;
rows.push({
id,
region,
manager,
item: items[(id * 3 + m) % 5],
quantity: 2 + ((id * 7) % 18),
amount: 18 + ((id * 41) % 260),
});
}
});
}
// Show the group names (region → manager) overlapped with indentation in a single cell (the 'Item' column),
// and hide the region and manager columns whose values repeat. The column order stays the same. Subtotal rows go at the end of each group.
const columns = (showGroupColumns) => [
{ id: 'region', title: 'Region', width: 90, hidden: !showGroupColumns },
{ id: 'manager', title: 'Manager', width: 110, hidden: !showGroupColumns },
{ id: 'item', title: 'Item / Group', width: 220, aggregate: 'rowCount' },
{ id: 'quantity', title: 'Qty', kind: 'number', width: 90, align: 'end', aggregate: 'sum' },
{
id: 'amount',
title: 'Amount ($)',
kind: 'number',
width: 130,
align: 'end',
aggregate: 'sum',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
];
export const options = {
columns: columns(false),
rows,
rowKey: 'id',
// column: the column that draws the group name and the expand button.
grouping: { by: ['region', 'manager'], column: 'item', expandDepth: 'all', summary: 'footer' },
summary: { label: 'Grand total' },
};
export const actions = {
// Show or hide the hidden region and manager columns again (the grouping stays as it is).
columns: (api, value) => {
const show = value === 'show';
api.setColumnVisible('region', show);
api.setColumnVisible('manager', show);
},
depth: (api, value) => {
if (value === 'none')
api.collapseAll();
else if (value === 'region')
api.expandToDepth(1);
else
api.expandAll();
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Show nested group names in a single column</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Region and manager columns" id="columns">
<option value="hide" selected>Hidden</option>
<option value="show">Show</option>
</select>
<select aria-label="Expansion" id="depth">
<option value="all" selected>Expand all</option>
<option value="region">Regions only</option>
<option value="none">Collapse all</option>
</select>
</div>
<div id="grid" style="height: 740px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const PEOPLE = {
'New York': ['Liam Carter', 'Emma Stone'],
Boston: ['Noah Kim', 'Olivia Park'],
};
const items = ['Letter paper', 'Printer toner', 'Office chair', 'Monitor stand', 'Whiteboard'];
const rows = [];
for (const [region, managers] of Object.entries(PEOPLE)) {
managers.forEach((manager, m) => {
for (let k = 0; k < 2; k += 1) {
const id = rows.length + 1;
rows.push({
id,
region,
manager,
item: items[(id * 3 + m) % 5],
quantity: 2 + ((id * 7) % 18),
amount: 18 + ((id * 41) % 260),
});
}
});
}
// Show the group names (region → manager) overlapped with indentation in a single cell (the 'Item' column),
// and hide the region and manager columns whose values repeat. The column order stays the same. Subtotal rows go at the end of each group.
const columns = (showGroupColumns) => [
{ id: 'region', title: 'Region', width: 90, hidden: !showGroupColumns },
{ id: 'manager', title: 'Manager', width: 110, hidden: !showGroupColumns },
{ id: 'item', title: 'Item / Group', width: 220, aggregate: 'rowCount' },
{ id: 'quantity', title: 'Qty', kind: 'number', width: 90, align: 'end', aggregate: 'sum' },
{
id: 'amount',
title: 'Amount ($)',
kind: 'number',
width: 130,
align: 'end',
aggregate: 'sum',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
];
const options = {
columns: columns(false),
rows,
rowKey: 'id',
// column: the column that draws the group name and the expand button.
grouping: { by: ['region', 'manager'], column: 'item', expandDepth: 'all', summary: 'footer' },
summary: { label: 'Grand total' },
};
const actions = {
// Show or hide the hidden region and manager columns again (the grouping stays as it is).
columns: (api, value) => {
const show = value === 'show';
api.setColumnVisible('region', show);
api.setColumnVisible('manager', show);
},
depth: (api, value) => {
if (value === 'none')
api.collapseAll();
else if (value === 'region')
api.expandToDepth(1);
else
api.expandAll();
},
};
options.ariaLabel = "Show nested group names in a single column";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('columns').addEventListener('change', (event) => actions.columns(api, event.target.value));
document.getElementById('depth').addEventListener('change', (event) => actions.depth(api, event.target.value));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
return (
<>
<div className="toolbar">
<select
aria-label="Region and manager columns"
disabled={!api}
onChange={(event) => api && actions.columns(api, event.target.value)}
>
<option value="hide">Hidden</option>
<option value="show">Show</option>
</select>
<select defaultValue="hide"
aria-label="Expansion"
disabled={!api}
onChange={(event) => api && actions.depth(api, event.target.value)}
>
<option value="all">Expand all</option>
<option value="region">Regions only</option>
<option value="none">Collapse all</option>
</select>
</div>
<Grid style={{ height: 740 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
region: string;
manager: string;
item: string;
quantity: number;
amount: number;
}
const PEOPLE: Record<string, string[]> = {
'New York': ['Liam Carter', 'Emma Stone'],
Boston: ['Noah Kim', 'Olivia Park'],
};
const items = ['Letter paper', 'Printer toner', 'Office chair', 'Monitor stand', 'Whiteboard'];
const rows: Row[] = [];
for (const [region, managers] of Object.entries(PEOPLE)) {
managers.forEach((manager, m) => {
for (let k = 0; k < 2; k += 1) {
const id = rows.length + 1;
rows.push({
id,
region,
manager,
item: items[(id * 3 + m) % 5] as string,
quantity: 2 + ((id * 7) % 18),
amount: 18 + ((id * 41) % 260),
});
}
});
}
// Show the group names (region → manager) overlapped with indentation in a single cell (the 'Item' column),
// and hide the region and manager columns whose values repeat. The column order stays the same. Subtotal rows go at the end of each group.
const columns = (showGroupColumns: boolean): ColumnDef<Row>[] => [
{ id: 'region', title: 'Region', width: 90, hidden: !showGroupColumns },
{ id: 'manager', title: 'Manager', width: 110, hidden: !showGroupColumns },
{ id: 'item', title: 'Item / Group', width: 220, aggregate: 'rowCount' },
{ id: 'quantity', title: 'Qty', kind: 'number', width: 90, align: 'end', aggregate: 'sum' },
{
id: 'amount',
title: 'Amount ($)',
kind: 'number',
width: 130,
align: 'end',
aggregate: 'sum',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
];
export const options: GridOptions<Row> = {
columns: columns(false),
rows,
rowKey: 'id',
// column: the column that draws the group name and the expand button.
grouping: { by: ['region', 'manager'], column: 'item', expandDepth: 'all', summary: 'footer' },
summary: { label: 'Grand total' },
};
export const actions = {
// Show or hide the hidden region and manager columns again (the grouping stays as it is).
columns: (api: GridApi<Row>, value?: string) => {
const show = value === 'show';
api.setColumnVisible('region', show);
api.setColumnVisible('manager', show);
},
depth: (api: GridApi<Row>, value?: string) => {
if (value === 'none') api.collapseAll();
else if (value === 'region') api.expandToDepth(1);
else 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 { shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
}
</script>
<template>
<div class="toolbar">
<select
aria-label="Region and manager columns"
:disabled="!api"
@change="api && actions.columns(api, ($event.target as HTMLSelectElement).value)"
>
<option value="hide" selected>Hidden</option>
<option value="show">Show</option>
</select>
<select
aria-label="Expansion"
:disabled="!api"
@change="api && actions.depth(api, ($event.target as HTMLSelectElement).value)"
>
<option value="all" selected>Expand all</option>
<option value="region">Regions only</option>
<option value="none">Collapse all</option>
</select>
</div>
<Grid style="height: 740px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
region: string;
manager: string;
item: string;
quantity: number;
amount: number;
}
const PEOPLE: Record<string, string[]> = {
'New York': ['Liam Carter', 'Emma Stone'],
Boston: ['Noah Kim', 'Olivia Park'],
};
const items = ['Letter paper', 'Printer toner', 'Office chair', 'Monitor stand', 'Whiteboard'];
const rows: Row[] = [];
for (const [region, managers] of Object.entries(PEOPLE)) {
managers.forEach((manager, m) => {
for (let k = 0; k < 2; k += 1) {
const id = rows.length + 1;
rows.push({
id,
region,
manager,
item: items[(id * 3 + m) % 5] as string,
quantity: 2 + ((id * 7) % 18),
amount: 18 + ((id * 41) % 260),
});
}
});
}
// Show the group names (region → manager) overlapped with indentation in a single cell (the 'Item' column),
// and hide the region and manager columns whose values repeat. The column order stays the same. Subtotal rows go at the end of each group.
const columns = (showGroupColumns: boolean): ColumnDef<Row>[] => [
{ id: 'region', title: 'Region', width: 90, hidden: !showGroupColumns },
{ id: 'manager', title: 'Manager', width: 110, hidden: !showGroupColumns },
{ id: 'item', title: 'Item / Group', width: 220, aggregate: 'rowCount' },
{ id: 'quantity', title: 'Qty', kind: 'number', width: 90, align: 'end', aggregate: 'sum' },
{
id: 'amount',
title: 'Amount ($)',
kind: 'number',
width: 130,
align: 'end',
aggregate: 'sum',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
];
export const options: GridOptions<Row> = {
columns: columns(false),
rows,
rowKey: 'id',
// column: the column that draws the group name and the expand button.
grouping: { by: ['region', 'manager'], column: 'item', expandDepth: 'all', summary: 'footer' },
summary: { label: 'Grand total' },
};
export const actions = {
// Show or hide the hidden region and manager columns again (the grouping stays as it is).
columns: (api: GridApi<Row>, value?: string) => {
const show = value === 'show';
api.setColumnVisible('region', show);
api.setColumnVisible('manager', show);
},
depth: (api: GridApi<Row>, value?: string) => {
if (value === 'none') api.collapseAll();
else if (value === 'region') api.expandToDepth(1);
else api.expandAll();
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="Region and manager columns"
[disabled]="!api"
(change)="api && actions.columns(api, $any($event.target).value)"
>
<option value="hide" selected>Hidden</option>
<option value="show">Show</option>
</select>
<select
aria-label="Expansion"
[disabled]="!api"
(change)="api && actions.depth(api, $any($event.target).value)"
>
<option value="all" selected>Expand all</option>
<option value="region">Regions only</option>
<option value="none">Collapse all</option>
</select>
</div>
<mx-grid style="height: 740px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
}
}
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
region: string;
manager: string;
item: string;
quantity: number;
amount: number;
}
const PEOPLE: Record<string, string[]> = {
'New York': ['Liam Carter', 'Emma Stone'],
Boston: ['Noah Kim', 'Olivia Park'],
};
const items = ['Letter paper', 'Printer toner', 'Office chair', 'Monitor stand', 'Whiteboard'];
const rows: Row[] = [];
for (const [region, managers] of Object.entries(PEOPLE)) {
managers.forEach((manager, m) => {
for (let k = 0; k < 2; k += 1) {
const id = rows.length + 1;
rows.push({
id,
region,
manager,
item: items[(id * 3 + m) % 5] as string,
quantity: 2 + ((id * 7) % 18),
amount: 18 + ((id * 41) % 260),
});
}
});
}
// Show the group names (region → manager) overlapped with indentation in a single cell (the 'Item' column),
// and hide the region and manager columns whose values repeat. The column order stays the same. Subtotal rows go at the end of each group.
const columns = (showGroupColumns: boolean): ColumnDef<Row>[] => [
{ id: 'region', title: 'Region', width: 90, hidden: !showGroupColumns },
{ id: 'manager', title: 'Manager', width: 110, hidden: !showGroupColumns },
{ id: 'item', title: 'Item / Group', width: 220, aggregate: 'rowCount' },
{ id: 'quantity', title: 'Qty', kind: 'number', width: 90, align: 'end', aggregate: 'sum' },
{
id: 'amount',
title: 'Amount ($)',
kind: 'number',
width: 130,
align: 'end',
aggregate: 'sum',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
];
export const options: GridOptions<Row> = {
columns: columns(false),
rows,
rowKey: 'id',
// column: the column that draws the group name and the expand button.
grouping: { by: ['region', 'manager'], column: 'item', expandDepth: 'all', summary: 'footer' },
summary: { label: 'Grand total' },
};
export const actions = {
// Show or hide the hidden region and manager columns again (the grouping stays as it is).
columns: (api: GridApi<Row>, value?: string) => {
const show = value === 'show';
api.setColumnVisible('region', show);
api.setColumnVisible('manager', show);
},
depth: (api: GridApi<Row>, value?: string) => {
if (value === 'none') api.collapseAll();
else if (value === 'region') api.expandToDepth(1);
else api.expandAll();
},
};