맨 아래에 합계 줄 붙이기Add a total row at the bottom
컬럼마다 "무엇을 계산할지"만 적어 두면 표 맨 아래에 합계 줄이 생겨요. 합계 말고도 평균, 최소, 최대, 개수를 고를 수 있어요.Say what to calculate for each column and a total row appears at the bottom. Besides the sum you can pick average, minimum, maximum or count.
코드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="op">
<option value="sum" selected>합계</option>
<option value="avg">평균</option>
<option value="min">최소</option>
<option value="max">최대</option>
<option value="count">개수</option>
</select>
<select aria-label="합계 줄" id="footer">
<option value="on" selected>보이기</option>
<option value="off">숨기기</option>
</select>
</div>
<div id="grid" style="height: 360px"></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('op').addEventListener('change', (event) => actions.op(api, event.target.value));
document.getElementById('footer').addEventListener('change', (event) => actions.footer(api, event.target.value));const branches = [
'서울 본점',
'강남점',
'부산점',
'대구점',
'인천점',
'광주점',
'대전점',
'제주점',
];
const regions = ['수도권', '수도권', '영남', '영남', '수도권', '호남', '충청', '제주'];
// 단위는 백만원이에요. 지점마다 1~6월 매출이 있어요.
const sales = (i, k) => 20 + ((i * 13 + k * 7) % 70);
const rows = branches.map((branch, i) => ({
id: i + 1,
branch,
region: regions[i],
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
m4: sales(i, 4),
m5: sales(i, 5),
m6: sales(i, 6),
}));
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'];
const money = (value) => Number(value).toLocaleString('ko-KR');
const labels = {
sum: '합계',
avg: '평균',
min: '최소',
max: '최대',
count: '개수',
};
let op = 'sum';
// 컬럼에 aggregate를 주면 그 컬럼의 값들을 모아서 맨 아래 합계 줄에 계산해 보여 줘요.
// 쓸 수 있는 계산: sum(합계) avg(평균) min(최소) max(최대) count(개수).
const columns = () => [
{ id: 'branch', title: '지점', width: 120 },
...months.map((id, k) => ({
id,
title: `${k + 1}월`,
kind: 'number',
width: 90,
align: 'end',
aggregate: op,
formatValue: money,
// 평균처럼 소수가 나오는 계산은 한 자리까지만 보여 줘요.
aggregateFormat: (value) => Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 1 }),
})),
];
export const options = {
columns: columns(),
rows,
rowKey: 'id',
summary: { label: labels[op] },
};
export const actions = {
op: (api, value) => {
op = (value ?? 'sum');
api.setColumns(columns());
api.setOptions({ summary: { label: labels[op] } });
},
footer: (api, value) => api.setOptions({ summary: value === 'off' ? false : { label: labels[op] } }),
};<!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="op">
<option value="sum" selected>합계</option>
<option value="avg">평균</option>
<option value="min">최소</option>
<option value="max">최대</option>
<option value="count">개수</option>
</select>
<select aria-label="합계 줄" id="footer">
<option value="on" selected>보이기</option>
<option value="off">숨기기</option>
</select>
</div>
<div id="grid" style="height: 360px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const branches = [
'서울 본점',
'강남점',
'부산점',
'대구점',
'인천점',
'광주점',
'대전점',
'제주점',
];
const regions = ['수도권', '수도권', '영남', '영남', '수도권', '호남', '충청', '제주'];
// 단위는 백만원이에요. 지점마다 1~6월 매출이 있어요.
const sales = (i, k) => 20 + ((i * 13 + k * 7) % 70);
const rows = branches.map((branch, i) => ({
id: i + 1,
branch,
region: regions[i],
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
m4: sales(i, 4),
m5: sales(i, 5),
m6: sales(i, 6),
}));
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'];
const money = (value) => Number(value).toLocaleString('ko-KR');
const labels = {
sum: '합계',
avg: '평균',
min: '최소',
max: '최대',
count: '개수',
};
let op = 'sum';
// 컬럼에 aggregate를 주면 그 컬럼의 값들을 모아서 맨 아래 합계 줄에 계산해 보여 줘요.
// 쓸 수 있는 계산: sum(합계) avg(평균) min(최소) max(최대) count(개수).
const columns = () => [
{ id: 'branch', title: '지점', width: 120 },
...months.map((id, k) => ({
id,
title: `${k + 1}월`,
kind: 'number',
width: 90,
align: 'end',
aggregate: op,
formatValue: money,
// 평균처럼 소수가 나오는 계산은 한 자리까지만 보여 줘요.
aggregateFormat: (value) => Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 1 }),
})),
];
const options = {
columns: columns(),
rows,
rowKey: 'id',
summary: { label: labels[op] },
};
const actions = {
op: (api, value) => {
op = (value ?? 'sum');
api.setColumns(columns());
api.setOptions({ summary: { label: labels[op] } });
},
footer: (api, value) => api.setOptions({ summary: value === 'off' ? false : { label: labels[op] } }),
};
options.ariaLabel = "맨 아래에 합계 줄 붙이기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('op').addEventListener('change', (event) => actions.op(api, event.target.value));
document.getElementById('footer').addEventListener('change', (event) => actions.footer(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.op(api, event.target.value)}
>
<option value="sum">합계</option>
<option value="avg">평균</option>
<option value="min">최소</option>
<option value="max">최대</option>
<option value="count">개수</option>
</select>
<select defaultValue="sum"
aria-label="합계 줄"
disabled={!api}
onChange={(event) => api && actions.footer(api, event.target.value)}
>
<option value="on">보이기</option>
<option value="off">숨기기</option>
</select>
</div>
<Grid style={{ height: 360 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
branch: string;
region: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
}
const branches = [
'서울 본점',
'강남점',
'부산점',
'대구점',
'인천점',
'광주점',
'대전점',
'제주점',
];
const regions = ['수도권', '수도권', '영남', '영남', '수도권', '호남', '충청', '제주'];
// 단위는 백만원이에요. 지점마다 1~6월 매출이 있어요.
const sales = (i: number, k: number) => 20 + ((i * 13 + k * 7) % 70);
const rows: Row[] = branches.map((branch, i) => ({
id: i + 1,
branch,
region: regions[i],
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
m4: sales(i, 4),
m5: sales(i, 5),
m6: sales(i, 6),
}));
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'] as const;
const money = (value: unknown) => Number(value).toLocaleString('ko-KR');
type Op = 'sum' | 'avg' | 'min' | 'max' | 'count';
const labels: Record<Op, string> = {
sum: '합계',
avg: '평균',
min: '최소',
max: '최대',
count: '개수',
};
let op: Op = 'sum';
// 컬럼에 aggregate를 주면 그 컬럼의 값들을 모아서 맨 아래 합계 줄에 계산해 보여 줘요.
// 쓸 수 있는 계산: sum(합계) avg(평균) min(최소) max(최대) count(개수).
const columns = (): ColumnDef<Row>[] => [
{ id: 'branch', title: '지점', width: 120 },
...months.map((id, k) => ({
id,
title: `${k + 1}월`,
kind: 'number' as const,
width: 90,
align: 'end' as const,
aggregate: op,
formatValue: money,
// 평균처럼 소수가 나오는 계산은 한 자리까지만 보여 줘요.
aggregateFormat: (value: unknown) =>
Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 1 }),
})),
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
summary: { label: labels[op] },
};
export const actions = {
op: (api: GridApi<Row>, value?: string) => {
op = (value ?? 'sum') as Op;
api.setColumns(columns());
api.setOptions({ summary: { label: labels[op] } });
},
footer: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: value === 'off' ? false : { label: labels[op] } }),
};버튼을 누르면 이 예제의 코드가 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.op(api, ($event.target as HTMLSelectElement).value)"
>
<option value="sum" selected>합계</option>
<option value="avg">평균</option>
<option value="min">최소</option>
<option value="max">최대</option>
<option value="count">개수</option>
</select>
<select
aria-label="합계 줄"
:disabled="!api"
@change="api && actions.footer(api, ($event.target as HTMLSelectElement).value)"
>
<option value="on" selected>보이기</option>
<option value="off">숨기기</option>
</select>
</div>
<Grid style="height: 360px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
branch: string;
region: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
}
const branches = [
'서울 본점',
'강남점',
'부산점',
'대구점',
'인천점',
'광주점',
'대전점',
'제주점',
];
const regions = ['수도권', '수도권', '영남', '영남', '수도권', '호남', '충청', '제주'];
// 단위는 백만원이에요. 지점마다 1~6월 매출이 있어요.
const sales = (i: number, k: number) => 20 + ((i * 13 + k * 7) % 70);
const rows: Row[] = branches.map((branch, i) => ({
id: i + 1,
branch,
region: regions[i],
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
m4: sales(i, 4),
m5: sales(i, 5),
m6: sales(i, 6),
}));
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'] as const;
const money = (value: unknown) => Number(value).toLocaleString('ko-KR');
type Op = 'sum' | 'avg' | 'min' | 'max' | 'count';
const labels: Record<Op, string> = {
sum: '합계',
avg: '평균',
min: '최소',
max: '최대',
count: '개수',
};
let op: Op = 'sum';
// 컬럼에 aggregate를 주면 그 컬럼의 값들을 모아서 맨 아래 합계 줄에 계산해 보여 줘요.
// 쓸 수 있는 계산: sum(합계) avg(평균) min(최소) max(최대) count(개수).
const columns = (): ColumnDef<Row>[] => [
{ id: 'branch', title: '지점', width: 120 },
...months.map((id, k) => ({
id,
title: `${k + 1}월`,
kind: 'number' as const,
width: 90,
align: 'end' as const,
aggregate: op,
formatValue: money,
// 평균처럼 소수가 나오는 계산은 한 자리까지만 보여 줘요.
aggregateFormat: (value: unknown) =>
Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 1 }),
})),
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
summary: { label: labels[op] },
};
export const actions = {
op: (api: GridApi<Row>, value?: string) => {
op = (value ?? 'sum') as Op;
api.setColumns(columns());
api.setOptions({ summary: { label: labels[op] } });
},
footer: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: value === 'off' ? false : { label: labels[op] } }),
};버튼을 누르면 이 예제의 코드가 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.op(api, $any($event.target).value)"
>
<option value="sum" selected>합계</option>
<option value="avg">평균</option>
<option value="min">최소</option>
<option value="max">최대</option>
<option value="count">개수</option>
</select>
<select
aria-label="합계 줄"
[disabled]="!api"
(change)="api && actions.footer(api, $any($event.target).value)"
>
<option value="on" selected>보이기</option>
<option value="off">숨기기</option>
</select>
</div>
<mx-grid style="height: 360px" [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;
branch: string;
region: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
}
const branches = [
'서울 본점',
'강남점',
'부산점',
'대구점',
'인천점',
'광주점',
'대전점',
'제주점',
];
const regions = ['수도권', '수도권', '영남', '영남', '수도권', '호남', '충청', '제주'];
// 단위는 백만원이에요. 지점마다 1~6월 매출이 있어요.
const sales = (i: number, k: number) => 20 + ((i * 13 + k * 7) % 70);
const rows: Row[] = branches.map((branch, i) => ({
id: i + 1,
branch,
region: regions[i],
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
m4: sales(i, 4),
m5: sales(i, 5),
m6: sales(i, 6),
}));
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'] as const;
const money = (value: unknown) => Number(value).toLocaleString('ko-KR');
type Op = 'sum' | 'avg' | 'min' | 'max' | 'count';
const labels: Record<Op, string> = {
sum: '합계',
avg: '평균',
min: '최소',
max: '최대',
count: '개수',
};
let op: Op = 'sum';
// 컬럼에 aggregate를 주면 그 컬럼의 값들을 모아서 맨 아래 합계 줄에 계산해 보여 줘요.
// 쓸 수 있는 계산: sum(합계) avg(평균) min(최소) max(최대) count(개수).
const columns = (): ColumnDef<Row>[] => [
{ id: 'branch', title: '지점', width: 120 },
...months.map((id, k) => ({
id,
title: `${k + 1}월`,
kind: 'number' as const,
width: 90,
align: 'end' as const,
aggregate: op,
formatValue: money,
// 평균처럼 소수가 나오는 계산은 한 자리까지만 보여 줘요.
aggregateFormat: (value: unknown) =>
Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 1 }),
})),
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
summary: { label: labels[op] },
};
export const actions = {
op: (api: GridApi<Row>, value?: string) => {
op = (value ?? 'sum') as Op;
api.setColumns(columns());
api.setOptions({ summary: { label: labels[op] } });
},
footer: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: value === 'off' ? false : { label: labels[op] } }),
};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="Calculation" id="op">
<option value="sum" selected>Sum</option>
<option value="avg">Average</option>
<option value="min">Minimum</option>
<option value="max">Maximum</option>
<option value="count">Count</option>
</select>
<select aria-label="Total row" id="footer">
<option value="on" selected>Show</option>
<option value="off">Hide</option>
</select>
</div>
<div id="grid" style="height: 360px"></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('op').addEventListener('change', (event) => actions.op(api, event.target.value));
document.getElementById('footer').addEventListener('change', (event) => actions.footer(api, event.target.value));const branches = ['Seoul HQ', 'Gangnam', 'Busan', 'Daegu', 'Incheon', 'Gwangju', 'Daejeon', 'Jeju'];
const regions = [
'Capital',
'Capital',
'Yeongnam',
'Yeongnam',
'Capital',
'Honam',
'Chungcheong',
'Jeju',
];
// Units are $ thousands. Each branch has sales for January to June.
const sales = (i, k) => 20 + ((i * 13 + k * 7) % 70);
const rows = branches.map((branch, i) => ({
id: i + 1,
branch,
region: regions[i],
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
m4: sales(i, 4),
m5: sales(i, 5),
m6: sales(i, 6),
}));
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'];
const money = (value) => Number(value).toLocaleString('en-US');
const labels = {
sum: 'Total',
avg: 'Average',
min: 'Min',
max: 'Max',
count: 'Count',
};
let op = 'sum';
// Give a column an aggregate and its values are collected and calculated in the summary row at the bottom.
// Available calculations: sum (total), avg (average), min, max, count.
const columns = () => [
{ id: 'branch', title: 'Branch', width: 120 },
...months.map((id, k) => ({
id,
title: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'][k],
kind: 'number',
width: 90,
align: 'end',
aggregate: op,
formatValue: money,
// Calculations that produce decimals, such as the average, show one decimal place at most.
aggregateFormat: (value) => Number(value).toLocaleString('en-US', { maximumFractionDigits: 1 }),
})),
];
export const options = {
columns: columns(),
rows,
rowKey: 'id',
summary: { label: labels[op] },
};
export const actions = {
op: (api, value) => {
op = (value ?? 'sum');
api.setColumns(columns());
api.setOptions({ summary: { label: labels[op] } });
},
footer: (api, value) => api.setOptions({ summary: value === 'off' ? false : { label: labels[op] } }),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Add a total row at the bottom</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Calculation" id="op">
<option value="sum" selected>Sum</option>
<option value="avg">Average</option>
<option value="min">Minimum</option>
<option value="max">Maximum</option>
<option value="count">Count</option>
</select>
<select aria-label="Total row" id="footer">
<option value="on" selected>Show</option>
<option value="off">Hide</option>
</select>
</div>
<div id="grid" style="height: 360px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const branches = ['Seoul HQ', 'Gangnam', 'Busan', 'Daegu', 'Incheon', 'Gwangju', 'Daejeon', 'Jeju'];
const regions = [
'Capital',
'Capital',
'Yeongnam',
'Yeongnam',
'Capital',
'Honam',
'Chungcheong',
'Jeju',
];
// Units are $ thousands. Each branch has sales for January to June.
const sales = (i, k) => 20 + ((i * 13 + k * 7) % 70);
const rows = branches.map((branch, i) => ({
id: i + 1,
branch,
region: regions[i],
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
m4: sales(i, 4),
m5: sales(i, 5),
m6: sales(i, 6),
}));
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'];
const money = (value) => Number(value).toLocaleString('en-US');
const labels = {
sum: 'Total',
avg: 'Average',
min: 'Min',
max: 'Max',
count: 'Count',
};
let op = 'sum';
// Give a column an aggregate and its values are collected and calculated in the summary row at the bottom.
// Available calculations: sum (total), avg (average), min, max, count.
const columns = () => [
{ id: 'branch', title: 'Branch', width: 120 },
...months.map((id, k) => ({
id,
title: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'][k],
kind: 'number',
width: 90,
align: 'end',
aggregate: op,
formatValue: money,
// Calculations that produce decimals, such as the average, show one decimal place at most.
aggregateFormat: (value) => Number(value).toLocaleString('en-US', { maximumFractionDigits: 1 }),
})),
];
const options = {
columns: columns(),
rows,
rowKey: 'id',
summary: { label: labels[op] },
};
const actions = {
op: (api, value) => {
op = (value ?? 'sum');
api.setColumns(columns());
api.setOptions({ summary: { label: labels[op] } });
},
footer: (api, value) => api.setOptions({ summary: value === 'off' ? false : { label: labels[op] } }),
};
options.ariaLabel = "Add a total row at the bottom";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('op').addEventListener('change', (event) => actions.op(api, event.target.value));
document.getElementById('footer').addEventListener('change', (event) => actions.footer(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="Calculation"
disabled={!api}
onChange={(event) => api && actions.op(api, event.target.value)}
>
<option value="sum">Sum</option>
<option value="avg">Average</option>
<option value="min">Minimum</option>
<option value="max">Maximum</option>
<option value="count">Count</option>
</select>
<select defaultValue="sum"
aria-label="Total row"
disabled={!api}
onChange={(event) => api && actions.footer(api, event.target.value)}
>
<option value="on">Show</option>
<option value="off">Hide</option>
</select>
</div>
<Grid style={{ height: 360 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
branch: string;
region: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
}
const branches = ['Seoul HQ', 'Gangnam', 'Busan', 'Daegu', 'Incheon', 'Gwangju', 'Daejeon', 'Jeju'];
const regions = [
'Capital',
'Capital',
'Yeongnam',
'Yeongnam',
'Capital',
'Honam',
'Chungcheong',
'Jeju',
];
// Units are $ thousands. Each branch has sales for January to June.
const sales = (i: number, k: number) => 20 + ((i * 13 + k * 7) % 70);
const rows: Row[] = branches.map((branch, i) => ({
id: i + 1,
branch,
region: regions[i],
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
m4: sales(i, 4),
m5: sales(i, 5),
m6: sales(i, 6),
}));
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'] as const;
const money = (value: unknown) => Number(value).toLocaleString('en-US');
type Op = 'sum' | 'avg' | 'min' | 'max' | 'count';
const labels: Record<Op, string> = {
sum: 'Total',
avg: 'Average',
min: 'Min',
max: 'Max',
count: 'Count',
};
let op: Op = 'sum';
// Give a column an aggregate and its values are collected and calculated in the summary row at the bottom.
// Available calculations: sum (total), avg (average), min, max, count.
const columns = (): ColumnDef<Row>[] => [
{ id: 'branch', title: 'Branch', width: 120 },
...months.map((id, k) => ({
id,
title: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'][k],
kind: 'number' as const,
width: 90,
align: 'end' as const,
aggregate: op,
formatValue: money,
// Calculations that produce decimals, such as the average, show one decimal place at most.
aggregateFormat: (value: unknown) =>
Number(value).toLocaleString('en-US', { maximumFractionDigits: 1 }),
})),
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
summary: { label: labels[op] },
};
export const actions = {
op: (api: GridApi<Row>, value?: string) => {
op = (value ?? 'sum') as Op;
api.setColumns(columns());
api.setOptions({ summary: { label: labels[op] } });
},
footer: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: value === 'off' ? false : { label: labels[op] } }),
};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="Calculation"
:disabled="!api"
@change="api && actions.op(api, ($event.target as HTMLSelectElement).value)"
>
<option value="sum" selected>Sum</option>
<option value="avg">Average</option>
<option value="min">Minimum</option>
<option value="max">Maximum</option>
<option value="count">Count</option>
</select>
<select
aria-label="Total row"
:disabled="!api"
@change="api && actions.footer(api, ($event.target as HTMLSelectElement).value)"
>
<option value="on" selected>Show</option>
<option value="off">Hide</option>
</select>
</div>
<Grid style="height: 360px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
branch: string;
region: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
}
const branches = ['Seoul HQ', 'Gangnam', 'Busan', 'Daegu', 'Incheon', 'Gwangju', 'Daejeon', 'Jeju'];
const regions = [
'Capital',
'Capital',
'Yeongnam',
'Yeongnam',
'Capital',
'Honam',
'Chungcheong',
'Jeju',
];
// Units are $ thousands. Each branch has sales for January to June.
const sales = (i: number, k: number) => 20 + ((i * 13 + k * 7) % 70);
const rows: Row[] = branches.map((branch, i) => ({
id: i + 1,
branch,
region: regions[i],
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
m4: sales(i, 4),
m5: sales(i, 5),
m6: sales(i, 6),
}));
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'] as const;
const money = (value: unknown) => Number(value).toLocaleString('en-US');
type Op = 'sum' | 'avg' | 'min' | 'max' | 'count';
const labels: Record<Op, string> = {
sum: 'Total',
avg: 'Average',
min: 'Min',
max: 'Max',
count: 'Count',
};
let op: Op = 'sum';
// Give a column an aggregate and its values are collected and calculated in the summary row at the bottom.
// Available calculations: sum (total), avg (average), min, max, count.
const columns = (): ColumnDef<Row>[] => [
{ id: 'branch', title: 'Branch', width: 120 },
...months.map((id, k) => ({
id,
title: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'][k],
kind: 'number' as const,
width: 90,
align: 'end' as const,
aggregate: op,
formatValue: money,
// Calculations that produce decimals, such as the average, show one decimal place at most.
aggregateFormat: (value: unknown) =>
Number(value).toLocaleString('en-US', { maximumFractionDigits: 1 }),
})),
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
summary: { label: labels[op] },
};
export const actions = {
op: (api: GridApi<Row>, value?: string) => {
op = (value ?? 'sum') as Op;
api.setColumns(columns());
api.setOptions({ summary: { label: labels[op] } });
},
footer: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: value === 'off' ? false : { label: labels[op] } }),
};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="Calculation"
[disabled]="!api"
(change)="api && actions.op(api, $any($event.target).value)"
>
<option value="sum" selected>Sum</option>
<option value="avg">Average</option>
<option value="min">Minimum</option>
<option value="max">Maximum</option>
<option value="count">Count</option>
</select>
<select
aria-label="Total row"
[disabled]="!api"
(change)="api && actions.footer(api, $any($event.target).value)"
>
<option value="on" selected>Show</option>
<option value="off">Hide</option>
</select>
</div>
<mx-grid style="height: 360px" [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;
branch: string;
region: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
}
const branches = ['Seoul HQ', 'Gangnam', 'Busan', 'Daegu', 'Incheon', 'Gwangju', 'Daejeon', 'Jeju'];
const regions = [
'Capital',
'Capital',
'Yeongnam',
'Yeongnam',
'Capital',
'Honam',
'Chungcheong',
'Jeju',
];
// Units are $ thousands. Each branch has sales for January to June.
const sales = (i: number, k: number) => 20 + ((i * 13 + k * 7) % 70);
const rows: Row[] = branches.map((branch, i) => ({
id: i + 1,
branch,
region: regions[i],
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
m4: sales(i, 4),
m5: sales(i, 5),
m6: sales(i, 6),
}));
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'] as const;
const money = (value: unknown) => Number(value).toLocaleString('en-US');
type Op = 'sum' | 'avg' | 'min' | 'max' | 'count';
const labels: Record<Op, string> = {
sum: 'Total',
avg: 'Average',
min: 'Min',
max: 'Max',
count: 'Count',
};
let op: Op = 'sum';
// Give a column an aggregate and its values are collected and calculated in the summary row at the bottom.
// Available calculations: sum (total), avg (average), min, max, count.
const columns = (): ColumnDef<Row>[] => [
{ id: 'branch', title: 'Branch', width: 120 },
...months.map((id, k) => ({
id,
title: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'][k],
kind: 'number' as const,
width: 90,
align: 'end' as const,
aggregate: op,
formatValue: money,
// Calculations that produce decimals, such as the average, show one decimal place at most.
aggregateFormat: (value: unknown) =>
Number(value).toLocaleString('en-US', { maximumFractionDigits: 1 }),
})),
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
summary: { label: labels[op] },
};
export const actions = {
op: (api: GridApi<Row>, value?: string) => {
op = (value ?? 'sum') as Op;
api.setColumns(columns());
api.setOptions({ summary: { label: labels[op] } });
},
footer: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: value === 'off' ? false : { label: labels[op] } }),
};