분기별 교차표: 지역·제품 합계Quarterly cross table: totals by region and product
분기마다 컬럼을 두고 지역(또는 제품)별로 묶어서, 묶음 줄과 맨 아래 전체 줄에 분기별 합계와 연 합계를 보여 줘요.One column per quarter grouped by region (or product), with quarterly and yearly totals on group and total rows.
코드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="by">
<option value="region" selected>지역</option>
<option value="product">제품</option>
<option value="none">묶지 않기</option>
</select>
<button type="button" id="expandAll">모두 펼치기</button>
<button type="button" id="collapseAll">모두 접기</button>
</div>
<div id="grid" style="height: 300px"></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('by').addEventListener('change', (event) => actions.by(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));const products = ['노트북', '모니터', '독'];
const regions = ['서울', '부산', '대전'];
const sum = (row) => row.q1 + row.q2 + row.q3 + row.q4;
// 피벗 기능 없이 만든 교차표예요: 분기마다 컬럼을 하나씩 두고, 모든 컬럼이 그룹 줄과 총계 줄에서 합쳐져요.
// "연 합계"는 줄마다 네 분기를 더한 계산 컬럼이에요.
export const options = {
columns: [
{ id: 'region', title: '지역', width: 100 },
{ id: 'product', title: '제품', width: 100 },
...['q1', 'q2', 'q3', 'q4'].map((id, i) => ({
id,
title: `${i + 1}분기`,
kind: 'number',
width: 80,
align: 'end',
aggregate: 'sum',
})),
{
id: 'year',
title: '연 합계',
kind: 'number',
width: 100,
align: 'end',
getValue: sum,
aggregate: 'sum',
},
],
rows: regions.flatMap((region, r) => products.map((product, p) => ({
id: r * 3 + p + 1,
region,
product,
q1: 40 + r * 7 + p * 5,
q2: 55 + r * 3 + p * 9,
q3: 48 + r * 11 + p * 4,
q4: 70 + r * 5 + p * 6,
}))),
rowKey: 'id',
grouping: { by: ['region'], column: 'region', expandDepth: 0 },
summary: { label: '전체' },
};
export function setup(_api, log) {
log({
ko: '지역별 합계가 한눈에 보여요. 지역을 눌러 제품별 줄을 펼쳐 보세요.',
en: 'Totals by region at a glance. Click a region to expand the product rows.',
});
}
export const actions = {
// 그룹 이름이 보이는 칸(column)도 함께 바꿔요. 안 그러면 제품 이름이 "지역" 칸에 나와요.
by: (api, value) => {
if (value === 'none')
return api.setGroupBy([]);
const id = value === 'product' ? 'product' : 'region';
api.setOptions({ grouping: { by: [id], column: id, expandDepth: 0 } });
},
expandAll: (api) => api.expandAll(),
collapseAll: (api) => api.collapseAll(),
};<!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="by">
<option value="region" selected>지역</option>
<option value="product">제품</option>
<option value="none">묶지 않기</option>
</select>
<button type="button" id="expandAll">모두 펼치기</button>
<button type="button" id="collapseAll">모두 접기</button>
</div>
<div id="grid" style="height: 300px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const products = ['노트북', '모니터', '독'];
const regions = ['서울', '부산', '대전'];
const sum = (row) => row.q1 + row.q2 + row.q3 + row.q4;
// 피벗 기능 없이 만든 교차표예요: 분기마다 컬럼을 하나씩 두고, 모든 컬럼이 그룹 줄과 총계 줄에서 합쳐져요.
// "연 합계"는 줄마다 네 분기를 더한 계산 컬럼이에요.
const options = {
columns: [
{ id: 'region', title: '지역', width: 100 },
{ id: 'product', title: '제품', width: 100 },
...['q1', 'q2', 'q3', 'q4'].map((id, i) => ({
id,
title: `${i + 1}분기`,
kind: 'number',
width: 80,
align: 'end',
aggregate: 'sum',
})),
{
id: 'year',
title: '연 합계',
kind: 'number',
width: 100,
align: 'end',
getValue: sum,
aggregate: 'sum',
},
],
rows: regions.flatMap((region, r) => products.map((product, p) => ({
id: r * 3 + p + 1,
region,
product,
q1: 40 + r * 7 + p * 5,
q2: 55 + r * 3 + p * 9,
q3: 48 + r * 11 + p * 4,
q4: 70 + r * 5 + p * 6,
}))),
rowKey: 'id',
grouping: { by: ['region'], column: 'region', expandDepth: 0 },
summary: { label: '전체' },
};
function setup(_api, log) {
log({
ko: '지역별 합계가 한눈에 보여요. 지역을 눌러 제품별 줄을 펼쳐 보세요.',
en: 'Totals by region at a glance. Click a region to expand the product rows.',
});
}
const actions = {
// 그룹 이름이 보이는 칸(column)도 함께 바꿔요. 안 그러면 제품 이름이 "지역" 칸에 나와요.
by: (api, value) => {
if (value === 'none')
return api.setGroupBy([]);
const id = value === 'product' ? 'product' : 'region';
api.setOptions({ grouping: { by: [id], column: id, expandDepth: 0 } });
},
expandAll: (api) => api.expandAll(),
collapseAll: (api) => api.collapseAll(),
};
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('by').addEventListener('change', (event) => actions.by(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(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.by(api, event.target.value)}
>
<option value="region">지역</option>
<option value="product">제품</option>
<option value="none">묶지 않기</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.expandAll(api)}
>모두 펼치기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.collapseAll(api)}
>모두 접기</button>
</div>
<Grid style={{ height: 300 }} 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;
product: string;
q1: number;
q2: number;
q3: number;
q4: number;
}
const products = ['노트북', '모니터', '독'];
const regions = ['서울', '부산', '대전'];
const sum = (row: Row): number => row.q1 + row.q2 + row.q3 + row.q4;
// 피벗 기능 없이 만든 교차표예요: 분기마다 컬럼을 하나씩 두고, 모든 컬럼이 그룹 줄과 총계 줄에서 합쳐져요.
// "연 합계"는 줄마다 네 분기를 더한 계산 컬럼이에요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'region', title: '지역', width: 100 },
{ id: 'product', title: '제품', width: 100 },
...(['q1', 'q2', 'q3', 'q4'] as const).map((id, i) => ({
id,
title: `${i + 1}분기`,
kind: 'number' as const,
width: 80,
align: 'end' as const,
aggregate: 'sum' as const,
})),
{
id: 'year',
title: '연 합계',
kind: 'number',
width: 100,
align: 'end',
getValue: sum,
aggregate: 'sum',
},
],
rows: regions.flatMap((region, r) =>
products.map((product, p) => ({
id: r * 3 + p + 1,
region,
product,
q1: 40 + r * 7 + p * 5,
q2: 55 + r * 3 + p * 9,
q3: 48 + r * 11 + p * 4,
q4: 70 + r * 5 + p * 6,
})),
),
rowKey: 'id',
grouping: { by: ['region'], column: 'region', expandDepth: 0 },
summary: { label: '전체' },
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: '지역별 합계가 한눈에 보여요. 지역을 눌러 제품별 줄을 펼쳐 보세요.',
en: 'Totals by region at a glance. Click a region to expand the product rows.',
});
}
export const actions = {
// 그룹 이름이 보이는 칸(column)도 함께 바꿔요. 안 그러면 제품 이름이 "지역" 칸에 나와요.
by: (api: GridApi<Row>, value?: string) => {
if (value === 'none') return api.setGroupBy([]);
const id = value === 'product' ? 'product' : 'region';
api.setOptions({ grouping: { by: [id], column: id, expandDepth: 0 } });
},
expandAll: (api: GridApi<Row>) => api.expandAll(),
collapseAll: (api: GridApi<Row>) => api.collapseAll(),
};버튼을 누르면 이 예제의 코드가 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.by(api, ($event.target as HTMLSelectElement).value)"
>
<option value="region" selected>지역</option>
<option value="product">제품</option>
<option value="none">묶지 않기</option>
</select>
<button
type="button"
:disabled="!api"
@click="api && actions.expandAll(api)"
>모두 펼치기</button>
<button
type="button"
:disabled="!api"
@click="api && actions.collapseAll(api)"
>모두 접기</button>
</div>
<Grid style="height: 300px" :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;
product: string;
q1: number;
q2: number;
q3: number;
q4: number;
}
const products = ['노트북', '모니터', '독'];
const regions = ['서울', '부산', '대전'];
const sum = (row: Row): number => row.q1 + row.q2 + row.q3 + row.q4;
// 피벗 기능 없이 만든 교차표예요: 분기마다 컬럼을 하나씩 두고, 모든 컬럼이 그룹 줄과 총계 줄에서 합쳐져요.
// "연 합계"는 줄마다 네 분기를 더한 계산 컬럼이에요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'region', title: '지역', width: 100 },
{ id: 'product', title: '제품', width: 100 },
...(['q1', 'q2', 'q3', 'q4'] as const).map((id, i) => ({
id,
title: `${i + 1}분기`,
kind: 'number' as const,
width: 80,
align: 'end' as const,
aggregate: 'sum' as const,
})),
{
id: 'year',
title: '연 합계',
kind: 'number',
width: 100,
align: 'end',
getValue: sum,
aggregate: 'sum',
},
],
rows: regions.flatMap((region, r) =>
products.map((product, p) => ({
id: r * 3 + p + 1,
region,
product,
q1: 40 + r * 7 + p * 5,
q2: 55 + r * 3 + p * 9,
q3: 48 + r * 11 + p * 4,
q4: 70 + r * 5 + p * 6,
})),
),
rowKey: 'id',
grouping: { by: ['region'], column: 'region', expandDepth: 0 },
summary: { label: '전체' },
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: '지역별 합계가 한눈에 보여요. 지역을 눌러 제품별 줄을 펼쳐 보세요.',
en: 'Totals by region at a glance. Click a region to expand the product rows.',
});
}
export const actions = {
// 그룹 이름이 보이는 칸(column)도 함께 바꿔요. 안 그러면 제품 이름이 "지역" 칸에 나와요.
by: (api: GridApi<Row>, value?: string) => {
if (value === 'none') return api.setGroupBy([]);
const id = value === 'product' ? 'product' : 'region';
api.setOptions({ grouping: { by: [id], column: id, expandDepth: 0 } });
},
expandAll: (api: GridApi<Row>) => api.expandAll(),
collapseAll: (api: GridApi<Row>) => api.collapseAll(),
};버튼을 누르면 이 예제의 코드가 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.by(api, $any($event.target).value)"
>
<option value="region" selected>지역</option>
<option value="product">제품</option>
<option value="none">묶지 않기</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.expandAll(api)"
>모두 펼치기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.collapseAll(api)"
>모두 접기</button>
</div>
<mx-grid style="height: 300px" [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;
product: string;
q1: number;
q2: number;
q3: number;
q4: number;
}
const products = ['노트북', '모니터', '독'];
const regions = ['서울', '부산', '대전'];
const sum = (row: Row): number => row.q1 + row.q2 + row.q3 + row.q4;
// 피벗 기능 없이 만든 교차표예요: 분기마다 컬럼을 하나씩 두고, 모든 컬럼이 그룹 줄과 총계 줄에서 합쳐져요.
// "연 합계"는 줄마다 네 분기를 더한 계산 컬럼이에요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'region', title: '지역', width: 100 },
{ id: 'product', title: '제품', width: 100 },
...(['q1', 'q2', 'q3', 'q4'] as const).map((id, i) => ({
id,
title: `${i + 1}분기`,
kind: 'number' as const,
width: 80,
align: 'end' as const,
aggregate: 'sum' as const,
})),
{
id: 'year',
title: '연 합계',
kind: 'number',
width: 100,
align: 'end',
getValue: sum,
aggregate: 'sum',
},
],
rows: regions.flatMap((region, r) =>
products.map((product, p) => ({
id: r * 3 + p + 1,
region,
product,
q1: 40 + r * 7 + p * 5,
q2: 55 + r * 3 + p * 9,
q3: 48 + r * 11 + p * 4,
q4: 70 + r * 5 + p * 6,
})),
),
rowKey: 'id',
grouping: { by: ['region'], column: 'region', expandDepth: 0 },
summary: { label: '전체' },
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: '지역별 합계가 한눈에 보여요. 지역을 눌러 제품별 줄을 펼쳐 보세요.',
en: 'Totals by region at a glance. Click a region to expand the product rows.',
});
}
export const actions = {
// 그룹 이름이 보이는 칸(column)도 함께 바꿔요. 안 그러면 제품 이름이 "지역" 칸에 나와요.
by: (api: GridApi<Row>, value?: string) => {
if (value === 'none') return api.setGroupBy([]);
const id = value === 'product' ? 'product' : 'region';
api.setOptions({ grouping: { by: [id], column: id, expandDepth: 0 } });
},
expandAll: (api: GridApi<Row>) => api.expandAll(),
collapseAll: (api: GridApi<Row>) => api.collapseAll(),
};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 by" id="by">
<option value="region" selected>Region</option>
<option value="product">Product</option>
<option value="none">No grouping</option>
</select>
<button type="button" id="expandAll">Expand all</button>
<button type="button" id="collapseAll">Collapse all</button>
</div>
<div id="grid" style="height: 300px"></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('by').addEventListener('change', (event) => actions.by(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));const products = ['Laptop', 'Monitor', 'Dock'];
const regions = ['New York', 'Boston', 'Austin'];
const sum = (row) => row.q1 + row.q2 + row.q3 + row.q4;
// A cross-tab built without a pivot feature: one column per quarter, and every column is summed on group rows and the total row.
// "Year total" is a computed column that adds the four quarters of each row.
export const options = {
columns: [
{ id: 'region', title: 'Region', width: 100 },
{ id: 'product', title: 'Product', width: 100 },
...['q1', 'q2', 'q3', 'q4'].map((id, i) => ({
id,
title: `Q${i + 1}`,
kind: 'number',
width: 80,
align: 'end',
aggregate: 'sum',
})),
{
id: 'year',
title: 'Year total',
kind: 'number',
width: 130,
align: 'end',
getValue: sum,
aggregate: 'sum',
},
],
rows: regions.flatMap((region, r) => products.map((product, p) => ({
id: r * 3 + p + 1,
region,
product,
q1: 40 + r * 7 + p * 5,
q2: 55 + r * 3 + p * 9,
q3: 48 + r * 11 + p * 4,
q4: 70 + r * 5 + p * 6,
}))),
rowKey: 'id',
grouping: { by: ['region'], column: 'region', expandDepth: 0 },
summary: { label: 'Total' },
};
export function setup(_api, log) {
log('Totals by region at a glance. Click a region to expand the product rows.');
}
export const actions = {
// Also switch the column that shows the group name. Otherwise product names would appear in the "Region" column.
by: (api, value) => {
if (value === 'none')
return api.setGroupBy([]);
const id = value === 'product' ? 'product' : 'region';
api.setOptions({ grouping: { by: [id], column: id, expandDepth: 0 } });
},
expandAll: (api) => api.expandAll(),
collapseAll: (api) => api.collapseAll(),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Quarterly cross table: totals by region and product</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Group by" id="by">
<option value="region" selected>Region</option>
<option value="product">Product</option>
<option value="none">No grouping</option>
</select>
<button type="button" id="expandAll">Expand all</button>
<button type="button" id="collapseAll">Collapse all</button>
</div>
<div id="grid" style="height: 300px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const products = ['Laptop', 'Monitor', 'Dock'];
const regions = ['New York', 'Boston', 'Austin'];
const sum = (row) => row.q1 + row.q2 + row.q3 + row.q4;
// A cross-tab built without a pivot feature: one column per quarter, and every column is summed on group rows and the total row.
// "Year total" is a computed column that adds the four quarters of each row.
const options = {
columns: [
{ id: 'region', title: 'Region', width: 100 },
{ id: 'product', title: 'Product', width: 100 },
...['q1', 'q2', 'q3', 'q4'].map((id, i) => ({
id,
title: `Q${i + 1}`,
kind: 'number',
width: 80,
align: 'end',
aggregate: 'sum',
})),
{
id: 'year',
title: 'Year total',
kind: 'number',
width: 130,
align: 'end',
getValue: sum,
aggregate: 'sum',
},
],
rows: regions.flatMap((region, r) => products.map((product, p) => ({
id: r * 3 + p + 1,
region,
product,
q1: 40 + r * 7 + p * 5,
q2: 55 + r * 3 + p * 9,
q3: 48 + r * 11 + p * 4,
q4: 70 + r * 5 + p * 6,
}))),
rowKey: 'id',
grouping: { by: ['region'], column: 'region', expandDepth: 0 },
summary: { label: 'Total' },
};
function setup(_api, log) {
log('Totals by region at a glance. Click a region to expand the product rows.');
}
const actions = {
// Also switch the column that shows the group name. Otherwise product names would appear in the "Region" column.
by: (api, value) => {
if (value === 'none')
return api.setGroupBy([]);
const id = value === 'product' ? 'product' : 'region';
api.setOptions({ grouping: { by: [id], column: id, expandDepth: 0 } });
},
expandAll: (api) => api.expandAll(),
collapseAll: (api) => api.collapseAll(),
};
options.ariaLabel = "Quarterly cross table: totals by region and product";
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('by').addEventListener('change', (event) => actions.by(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(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 by"
disabled={!api}
onChange={(event) => api && actions.by(api, event.target.value)}
>
<option value="region">Region</option>
<option value="product">Product</option>
<option value="none">No grouping</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.expandAll(api)}
>Expand all</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.collapseAll(api)}
>Collapse all</button>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
region: string;
product: string;
q1: number;
q2: number;
q3: number;
q4: number;
}
const products = ['Laptop', 'Monitor', 'Dock'];
const regions = ['New York', 'Boston', 'Austin'];
const sum = (row: Row): number => row.q1 + row.q2 + row.q3 + row.q4;
// A cross-tab built without a pivot feature: one column per quarter, and every column is summed on group rows and the total row.
// "Year total" is a computed column that adds the four quarters of each row.
export const options: GridOptions<Row> = {
columns: [
{ id: 'region', title: 'Region', width: 100 },
{ id: 'product', title: 'Product', width: 100 },
...(['q1', 'q2', 'q3', 'q4'] as const).map((id, i) => ({
id,
title: `Q${i + 1}`,
kind: 'number' as const,
width: 80,
align: 'end' as const,
aggregate: 'sum' as const,
})),
{
id: 'year',
title: 'Year total',
kind: 'number',
width: 130,
align: 'end',
getValue: sum,
aggregate: 'sum',
},
],
rows: regions.flatMap((region, r) =>
products.map((product, p) => ({
id: r * 3 + p + 1,
region,
product,
q1: 40 + r * 7 + p * 5,
q2: 55 + r * 3 + p * 9,
q3: 48 + r * 11 + p * 4,
q4: 70 + r * 5 + p * 6,
})),
),
rowKey: 'id',
grouping: { by: ['region'], column: 'region', expandDepth: 0 },
summary: { label: 'Total' },
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log('Totals by region at a glance. Click a region to expand the product rows.');
}
export const actions = {
// Also switch the column that shows the group name. Otherwise product names would appear in the "Region" column.
by: (api: GridApi<Row>, value?: string) => {
if (value === 'none') return api.setGroupBy([]);
const id = value === 'product' ? 'product' : 'region';
api.setOptions({ grouping: { by: [id], column: id, expandDepth: 0 } });
},
expandAll: (api: GridApi<Row>) => api.expandAll(),
collapseAll: (api: GridApi<Row>) => api.collapseAll(),
};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 by"
:disabled="!api"
@change="api && actions.by(api, ($event.target as HTMLSelectElement).value)"
>
<option value="region" selected>Region</option>
<option value="product">Product</option>
<option value="none">No grouping</option>
</select>
<button
type="button"
:disabled="!api"
@click="api && actions.expandAll(api)"
>Expand all</button>
<button
type="button"
:disabled="!api"
@click="api && actions.collapseAll(api)"
>Collapse all</button>
</div>
<Grid style="height: 300px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
region: string;
product: string;
q1: number;
q2: number;
q3: number;
q4: number;
}
const products = ['Laptop', 'Monitor', 'Dock'];
const regions = ['New York', 'Boston', 'Austin'];
const sum = (row: Row): number => row.q1 + row.q2 + row.q3 + row.q4;
// A cross-tab built without a pivot feature: one column per quarter, and every column is summed on group rows and the total row.
// "Year total" is a computed column that adds the four quarters of each row.
export const options: GridOptions<Row> = {
columns: [
{ id: 'region', title: 'Region', width: 100 },
{ id: 'product', title: 'Product', width: 100 },
...(['q1', 'q2', 'q3', 'q4'] as const).map((id, i) => ({
id,
title: `Q${i + 1}`,
kind: 'number' as const,
width: 80,
align: 'end' as const,
aggregate: 'sum' as const,
})),
{
id: 'year',
title: 'Year total',
kind: 'number',
width: 130,
align: 'end',
getValue: sum,
aggregate: 'sum',
},
],
rows: regions.flatMap((region, r) =>
products.map((product, p) => ({
id: r * 3 + p + 1,
region,
product,
q1: 40 + r * 7 + p * 5,
q2: 55 + r * 3 + p * 9,
q3: 48 + r * 11 + p * 4,
q4: 70 + r * 5 + p * 6,
})),
),
rowKey: 'id',
grouping: { by: ['region'], column: 'region', expandDepth: 0 },
summary: { label: 'Total' },
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log('Totals by region at a glance. Click a region to expand the product rows.');
}
export const actions = {
// Also switch the column that shows the group name. Otherwise product names would appear in the "Region" column.
by: (api: GridApi<Row>, value?: string) => {
if (value === 'none') return api.setGroupBy([]);
const id = value === 'product' ? 'product' : 'region';
api.setOptions({ grouping: { by: [id], column: id, expandDepth: 0 } });
},
expandAll: (api: GridApi<Row>) => api.expandAll(),
collapseAll: (api: GridApi<Row>) => api.collapseAll(),
};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 by"
[disabled]="!api"
(change)="api && actions.by(api, $any($event.target).value)"
>
<option value="region" selected>Region</option>
<option value="product">Product</option>
<option value="none">No grouping</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.expandAll(api)"
>Expand all</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.collapseAll(api)"
>Collapse all</button>
</div>
<mx-grid style="height: 300px" [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;
product: string;
q1: number;
q2: number;
q3: number;
q4: number;
}
const products = ['Laptop', 'Monitor', 'Dock'];
const regions = ['New York', 'Boston', 'Austin'];
const sum = (row: Row): number => row.q1 + row.q2 + row.q3 + row.q4;
// A cross-tab built without a pivot feature: one column per quarter, and every column is summed on group rows and the total row.
// "Year total" is a computed column that adds the four quarters of each row.
export const options: GridOptions<Row> = {
columns: [
{ id: 'region', title: 'Region', width: 100 },
{ id: 'product', title: 'Product', width: 100 },
...(['q1', 'q2', 'q3', 'q4'] as const).map((id, i) => ({
id,
title: `Q${i + 1}`,
kind: 'number' as const,
width: 80,
align: 'end' as const,
aggregate: 'sum' as const,
})),
{
id: 'year',
title: 'Year total',
kind: 'number',
width: 130,
align: 'end',
getValue: sum,
aggregate: 'sum',
},
],
rows: regions.flatMap((region, r) =>
products.map((product, p) => ({
id: r * 3 + p + 1,
region,
product,
q1: 40 + r * 7 + p * 5,
q2: 55 + r * 3 + p * 9,
q3: 48 + r * 11 + p * 4,
q4: 70 + r * 5 + p * 6,
})),
),
rowKey: 'id',
grouping: { by: ['region'], column: 'region', expandDepth: 0 },
summary: { label: 'Total' },
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log('Totals by region at a glance. Click a region to expand the product rows.');
}
export const actions = {
// Also switch the column that shows the group name. Otherwise product names would appear in the "Region" column.
by: (api: GridApi<Row>, value?: string) => {
if (value === 'none') return api.setGroupBy([]);
const id = value === 'product' ? 'product' : 'region';
api.setOptions({ grouping: { by: [id], column: id, expandDepth: 0 } });
},
expandAll: (api: GridApi<Row>) => api.expandAll(),
collapseAll: (api: GridApi<Row>) => api.collapseAll(),
};