그룹이 끝나는 곳에 소계 줄 붙이기Add a subtotal row where each group ends
지역 아래 담당자로 두 단계 묶은 발주 내역에서, 그룹이 끝나는 곳마다 수량과 금액의 소계 줄이 붙고 맨 아래에는 전체 합계가 나와요. 그룹을 접으면 소계 줄도 같이 접혀서 표가 깔끔해져요.In a two-level grouping of orders (region, then manager) each group ends with a subtotal row of quantity and amount, and a grand total sits at the bottom. Folding a group folds its subtotal.
코드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="depth">
<option value="all" selected>모두 펼치기</option>
<option value="region">지역만 펼치기</option>
<option value="none">모두 접기</option>
</select>
<button type="button" id="swap">묶는 순서 바꾸기</button>
</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('depth').addEventListener('change', (event) => actions.depth(api, event.target.value));
document.getElementById('swap').addEventListener('click', () => actions.swap(api));// 지역 → 담당자 순서로 묶은 발주 내역이에요. 담당자마다, 지역마다 소계 줄이 맨 아래에 붙어요.
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),
});
}
});
}
// grouping.summary: 'footer'면 그룹이 끝나는 곳에 소계 줄을 따로 붙여요. 접으면 소계 줄도 함께 접혀요.
// 숫자 컬럼에 aggregate를 적어 두면 그 컬럼의 소계가 계산돼요. summary: true는 맨 아래 전체 합계 줄이에요.
export const options = {
columns: [
{ id: 'region', title: '지역', width: 170, aggregate: 'rowCount' },
{ id: 'manager', title: '담당자', width: 110 },
{ id: 'item', title: '품목', width: 140 },
{ 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'),
},
],
rows,
rowKey: 'id',
grouping: { by: ['region', 'manager'], expandDepth: 'all', summary: 'footer' },
summary: { label: '전체 합계' },
};
export const actions = {
// 접힌 정도를 바꿔 봐요. 접어도 소계 줄은 그 그룹과 함께 접혀요.
depth: (api, value) => {
if (value === 'none')
api.collapseAll();
else if (value === 'region')
api.expandToDepth(1);
else
api.expandAll();
},
// 묶는 순서를 서로 바꿔요.
swap: (api) => api.setGroupBy([...api.getGroupBy()].reverse()),
};<!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="depth">
<option value="all" selected>모두 펼치기</option>
<option value="region">지역만 펼치기</option>
<option value="none">모두 접기</option>
</select>
<button type="button" id="swap">묶는 순서 바꾸기</button>
</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),
});
}
});
}
// grouping.summary: 'footer'면 그룹이 끝나는 곳에 소계 줄을 따로 붙여요. 접으면 소계 줄도 함께 접혀요.
// 숫자 컬럼에 aggregate를 적어 두면 그 컬럼의 소계가 계산돼요. summary: true는 맨 아래 전체 합계 줄이에요.
const options = {
columns: [
{ id: 'region', title: '지역', width: 170, aggregate: 'rowCount' },
{ id: 'manager', title: '담당자', width: 110 },
{ id: 'item', title: '품목', width: 140 },
{ 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'),
},
],
rows,
rowKey: 'id',
grouping: { by: ['region', 'manager'], expandDepth: 'all', summary: 'footer' },
summary: { label: '전체 합계' },
};
const actions = {
// 접힌 정도를 바꿔 봐요. 접어도 소계 줄은 그 그룹과 함께 접혀요.
depth: (api, value) => {
if (value === 'none')
api.collapseAll();
else if (value === 'region')
api.expandToDepth(1);
else
api.expandAll();
},
// 묶는 순서를 서로 바꿔요.
swap: (api) => api.setGroupBy([...api.getGroupBy()].reverse()),
};
options.ariaLabel = "그룹이 끝나는 곳에 소계 줄 붙이기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('depth').addEventListener('change', (event) => actions.depth(api, event.target.value));
document.getElementById('swap').addEventListener('click', () => actions.swap(api));
</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.depth(api, event.target.value)}
>
<option value="all">모두 펼치기</option>
<option value="region">지역만 펼치기</option>
<option value="none">모두 접기</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.swap(api)}
>묶는 순서 바꾸기</button>
</div>
<Grid style={{ height: 740 }} ref={gridRef} options={options} />
</>
);
}import type { 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),
});
}
});
}
// grouping.summary: 'footer'면 그룹이 끝나는 곳에 소계 줄을 따로 붙여요. 접으면 소계 줄도 함께 접혀요.
// 숫자 컬럼에 aggregate를 적어 두면 그 컬럼의 소계가 계산돼요. summary: true는 맨 아래 전체 합계 줄이에요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'region', title: '지역', width: 170, aggregate: 'rowCount' },
{ id: 'manager', title: '담당자', width: 110 },
{ id: 'item', title: '품목', width: 140 },
{ 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'),
},
],
rows,
rowKey: 'id',
grouping: { by: ['region', 'manager'], expandDepth: 'all', summary: 'footer' },
summary: { label: '전체 합계' },
};
export const actions = {
// 접힌 정도를 바꿔 봐요. 접어도 소계 줄은 그 그룹과 함께 접혀요.
depth: (api: GridApi<Row>, value?: string) => {
if (value === 'none') api.collapseAll();
else if (value === 'region') api.expandToDepth(1);
else api.expandAll();
},
// 묶는 순서를 서로 바꿔요.
swap: (api: GridApi<Row>) => api.setGroupBy([...api.getGroupBy()].reverse()),
};버튼을 누르면 이 예제의 코드가 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.depth(api, ($event.target as HTMLSelectElement).value)"
>
<option value="all" selected>모두 펼치기</option>
<option value="region">지역만 펼치기</option>
<option value="none">모두 접기</option>
</select>
<button type="button" :disabled="!api" @click="api && actions.swap(api)">묶는 순서 바꾸기</button>
</div>
<Grid style="height: 740px" :options="options" @ready="onReady" />
</template>import type { 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),
});
}
});
}
// grouping.summary: 'footer'면 그룹이 끝나는 곳에 소계 줄을 따로 붙여요. 접으면 소계 줄도 함께 접혀요.
// 숫자 컬럼에 aggregate를 적어 두면 그 컬럼의 소계가 계산돼요. summary: true는 맨 아래 전체 합계 줄이에요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'region', title: '지역', width: 170, aggregate: 'rowCount' },
{ id: 'manager', title: '담당자', width: 110 },
{ id: 'item', title: '품목', width: 140 },
{ 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'),
},
],
rows,
rowKey: 'id',
grouping: { by: ['region', 'manager'], expandDepth: 'all', summary: 'footer' },
summary: { label: '전체 합계' },
};
export const actions = {
// 접힌 정도를 바꿔 봐요. 접어도 소계 줄은 그 그룹과 함께 접혀요.
depth: (api: GridApi<Row>, value?: string) => {
if (value === 'none') api.collapseAll();
else if (value === 'region') api.expandToDepth(1);
else api.expandAll();
},
// 묶는 순서를 서로 바꿔요.
swap: (api: GridApi<Row>) => api.setGroupBy([...api.getGroupBy()].reverse()),
};버튼을 누르면 이 예제의 코드가 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.depth(api, $any($event.target).value)"
>
<option value="all" selected>모두 펼치기</option>
<option value="region">지역만 펼치기</option>
<option value="none">모두 접기</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.swap(api)"
>묶는 순서 바꾸기</button>
</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 { 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),
});
}
});
}
// grouping.summary: 'footer'면 그룹이 끝나는 곳에 소계 줄을 따로 붙여요. 접으면 소계 줄도 함께 접혀요.
// 숫자 컬럼에 aggregate를 적어 두면 그 컬럼의 소계가 계산돼요. summary: true는 맨 아래 전체 합계 줄이에요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'region', title: '지역', width: 170, aggregate: 'rowCount' },
{ id: 'manager', title: '담당자', width: 110 },
{ id: 'item', title: '품목', width: 140 },
{ 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'),
},
],
rows,
rowKey: 'id',
grouping: { by: ['region', 'manager'], expandDepth: 'all', summary: 'footer' },
summary: { label: '전체 합계' },
};
export const actions = {
// 접힌 정도를 바꿔 봐요. 접어도 소계 줄은 그 그룹과 함께 접혀요.
depth: (api: GridApi<Row>, value?: string) => {
if (value === 'none') api.collapseAll();
else if (value === 'region') api.expandToDepth(1);
else api.expandAll();
},
// 묶는 순서를 서로 바꿔요.
swap: (api: GridApi<Row>) => api.setGroupBy([...api.getGroupBy()].reverse()),
};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="Expansion" id="depth">
<option value="all" selected>Expand all</option>
<option value="region">Regions only</option>
<option value="none">Collapse all</option>
</select>
<button type="button" id="swap">Swap group order</button>
</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('depth').addEventListener('change', (event) => actions.depth(api, event.target.value));
document.getElementById('swap').addEventListener('click', () => actions.swap(api));// A purchase list grouped by region, then manager. A subtotal row is added at the bottom of each manager and each region.
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),
});
}
});
}
// With grouping.summary: 'footer', a separate subtotal row is added where the group ends. Collapsing a group collapses its subtotal row too.
// Set aggregate on a numeric column to get that column's subtotal. summary: true is the grand total row at the bottom.
export const options = {
columns: [
{ id: 'region', title: 'Region', width: 170, aggregate: 'rowCount' },
{ id: 'manager', title: 'Manager', width: 110 },
{ id: 'item', title: 'Item', width: 140 },
{ 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'),
},
],
rows,
rowKey: 'id',
grouping: { by: ['region', 'manager'], expandDepth: 'all', summary: 'footer' },
summary: { label: 'Grand total' },
};
export const actions = {
// Change how far the tree is collapsed. A subtotal row collapses together with its group.
depth: (api, value) => {
if (value === 'none')
api.collapseAll();
else if (value === 'region')
api.expandToDepth(1);
else
api.expandAll();
},
// Reverse the grouping order.
swap: (api) => api.setGroupBy([...api.getGroupBy()].reverse()),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Add a subtotal row where each group ends</title>
</head>
<body>
<div class="toolbar">
<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>
<button type="button" id="swap">Swap group order</button>
</div>
<div id="grid" style="height: 740px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// A purchase list grouped by region, then manager. A subtotal row is added at the bottom of each manager and each region.
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),
});
}
});
}
// With grouping.summary: 'footer', a separate subtotal row is added where the group ends. Collapsing a group collapses its subtotal row too.
// Set aggregate on a numeric column to get that column's subtotal. summary: true is the grand total row at the bottom.
const options = {
columns: [
{ id: 'region', title: 'Region', width: 170, aggregate: 'rowCount' },
{ id: 'manager', title: 'Manager', width: 110 },
{ id: 'item', title: 'Item', width: 140 },
{ 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'),
},
],
rows,
rowKey: 'id',
grouping: { by: ['region', 'manager'], expandDepth: 'all', summary: 'footer' },
summary: { label: 'Grand total' },
};
const actions = {
// Change how far the tree is collapsed. A subtotal row collapses together with its group.
depth: (api, value) => {
if (value === 'none')
api.collapseAll();
else if (value === 'region')
api.expandToDepth(1);
else
api.expandAll();
},
// Reverse the grouping order.
swap: (api) => api.setGroupBy([...api.getGroupBy()].reverse()),
};
options.ariaLabel = "Add a subtotal row where each group ends";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('depth').addEventListener('change', (event) => actions.depth(api, event.target.value));
document.getElementById('swap').addEventListener('click', () => actions.swap(api));
</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="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>
<button
type="button"
disabled={!api}
onClick={() => api && actions.swap(api)}
>Swap group order</button>
</div>
<Grid style={{ height: 740 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
region: string;
manager: string;
item: string;
quantity: number;
amount: number;
}
// A purchase list grouped by region, then manager. A subtotal row is added at the bottom of each manager and each region.
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),
});
}
});
}
// With grouping.summary: 'footer', a separate subtotal row is added where the group ends. Collapsing a group collapses its subtotal row too.
// Set aggregate on a numeric column to get that column's subtotal. summary: true is the grand total row at the bottom.
export const options: GridOptions<Row> = {
columns: [
{ id: 'region', title: 'Region', width: 170, aggregate: 'rowCount' },
{ id: 'manager', title: 'Manager', width: 110 },
{ id: 'item', title: 'Item', width: 140 },
{ 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'),
},
],
rows,
rowKey: 'id',
grouping: { by: ['region', 'manager'], expandDepth: 'all', summary: 'footer' },
summary: { label: 'Grand total' },
};
export const actions = {
// Change how far the tree is collapsed. A subtotal row collapses together with its group.
depth: (api: GridApi<Row>, value?: string) => {
if (value === 'none') api.collapseAll();
else if (value === 'region') api.expandToDepth(1);
else api.expandAll();
},
// Reverse the grouping order.
swap: (api: GridApi<Row>) => api.setGroupBy([...api.getGroupBy()].reverse()),
};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="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>
<button type="button" :disabled="!api" @click="api && actions.swap(api)">Swap group order</button>
</div>
<Grid style="height: 740px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
region: string;
manager: string;
item: string;
quantity: number;
amount: number;
}
// A purchase list grouped by region, then manager. A subtotal row is added at the bottom of each manager and each region.
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),
});
}
});
}
// With grouping.summary: 'footer', a separate subtotal row is added where the group ends. Collapsing a group collapses its subtotal row too.
// Set aggregate on a numeric column to get that column's subtotal. summary: true is the grand total row at the bottom.
export const options: GridOptions<Row> = {
columns: [
{ id: 'region', title: 'Region', width: 170, aggregate: 'rowCount' },
{ id: 'manager', title: 'Manager', width: 110 },
{ id: 'item', title: 'Item', width: 140 },
{ 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'),
},
],
rows,
rowKey: 'id',
grouping: { by: ['region', 'manager'], expandDepth: 'all', summary: 'footer' },
summary: { label: 'Grand total' },
};
export const actions = {
// Change how far the tree is collapsed. A subtotal row collapses together with its group.
depth: (api: GridApi<Row>, value?: string) => {
if (value === 'none') api.collapseAll();
else if (value === 'region') api.expandToDepth(1);
else api.expandAll();
},
// Reverse the grouping order.
swap: (api: GridApi<Row>) => api.setGroupBy([...api.getGroupBy()].reverse()),
};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="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>
<button
type="button"
[disabled]="!api"
(click)="api && actions.swap(api)"
>Swap group order</button>
</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 { GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
region: string;
manager: string;
item: string;
quantity: number;
amount: number;
}
// A purchase list grouped by region, then manager. A subtotal row is added at the bottom of each manager and each region.
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),
});
}
});
}
// With grouping.summary: 'footer', a separate subtotal row is added where the group ends. Collapsing a group collapses its subtotal row too.
// Set aggregate on a numeric column to get that column's subtotal. summary: true is the grand total row at the bottom.
export const options: GridOptions<Row> = {
columns: [
{ id: 'region', title: 'Region', width: 170, aggregate: 'rowCount' },
{ id: 'manager', title: 'Manager', width: 110 },
{ id: 'item', title: 'Item', width: 140 },
{ 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'),
},
],
rows,
rowKey: 'id',
grouping: { by: ['region', 'manager'], expandDepth: 'all', summary: 'footer' },
summary: { label: 'Grand total' },
};
export const actions = {
// Change how far the tree is collapsed. A subtotal row collapses together with its group.
depth: (api: GridApi<Row>, value?: string) => {
if (value === 'none') api.collapseAll();
else if (value === 'region') api.expandToDepth(1);
else api.expandAll();
},
// Reverse the grouping order.
swap: (api: GridApi<Row>) => api.setGroupBy([...api.getGroupBy()].reverse()),
};