맨 아래에 합계와 평균 두 줄 붙이기Show two rows at the bottom: total and average
summary에 rows를 주면 맨 아래에 줄이 여러 개 생겨요. 줄마다 이름과 "어느 컬럼을 어떻게 계산할지"를 따로 적어요.Give summary a rows list and several rows appear at the bottom. Each row has its own label and says which columns to calculate and how.
코드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="position">
<option value="bottom" selected>맨 아래</option>
<option value="top">맨 위</option>
</select>
</div>
<div id="grid" style="height: 300px"></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('position').addEventListener('change', (event) => actions.position(api, event.target.value));const branches = ['서울 본점', '강남점', '부산점', '대구점', '인천점', '광주점'];
// 단위는 백만원이에요. 지점마다 1~3월 매출이 있어요.
const sales = (i, k) => 20 + ((i * 13 + k * 7) % 70);
// 합계는 정수 그대로, 평균은 소수 첫째 자리까지만 보여 줘요(47.833 -> 47.8).
const aggregateText = (value) => Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 1 });
const months = ['m1', 'm2', 'm3'];
// rows의 줄마다 이름(label)과 "어느 컬럼을 어떻게 계산할지"(aggregate)를 적어요.
// aggregate에 적은 컬럼만 계산하고, 적지 않은 컬럼은 빈 칸이에요.
const summary = (position) => ({
position,
rows: [
{ id: 'sum', label: '합계', aggregate: { m1: 'sum', m2: 'sum', m3: 'sum' } },
{ id: 'avg', label: '평균', aggregate: { m1: 'avg', m2: 'avg', m3: 'avg' } },
],
});
export const options = {
columns: [
{ id: 'branch', title: '지점', width: 120 },
...months.map((id, k) => ({
id,
title: `${k + 1}월`,
kind: 'number',
width: 90,
align: 'end',
aggregateFormat: aggregateText,
})),
],
rows: branches.map((branch, i) => ({
id: i + 1,
branch,
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
})),
rowKey: 'id',
summary: summary('bottom'),
};
export const actions = {
position: (api, value) => api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
};<!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="position">
<option value="bottom" selected>맨 아래</option>
<option value="top">맨 위</option>
</select>
</div>
<div id="grid" style="height: 300px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const branches = ['서울 본점', '강남점', '부산점', '대구점', '인천점', '광주점'];
// 단위는 백만원이에요. 지점마다 1~3월 매출이 있어요.
const sales = (i, k) => 20 + ((i * 13 + k * 7) % 70);
// 합계는 정수 그대로, 평균은 소수 첫째 자리까지만 보여 줘요(47.833 -> 47.8).
const aggregateText = (value) => Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 1 });
const months = ['m1', 'm2', 'm3'];
// rows의 줄마다 이름(label)과 "어느 컬럼을 어떻게 계산할지"(aggregate)를 적어요.
// aggregate에 적은 컬럼만 계산하고, 적지 않은 컬럼은 빈 칸이에요.
const summary = (position) => ({
position,
rows: [
{ id: 'sum', label: '합계', aggregate: { m1: 'sum', m2: 'sum', m3: 'sum' } },
{ id: 'avg', label: '평균', aggregate: { m1: 'avg', m2: 'avg', m3: 'avg' } },
],
});
const options = {
columns: [
{ id: 'branch', title: '지점', width: 120 },
...months.map((id, k) => ({
id,
title: `${k + 1}월`,
kind: 'number',
width: 90,
align: 'end',
aggregateFormat: aggregateText,
})),
],
rows: branches.map((branch, i) => ({
id: i + 1,
branch,
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
})),
rowKey: 'id',
summary: summary('bottom'),
};
const actions = {
position: (api, value) => api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
};
options.ariaLabel = "맨 아래에 합계와 평균 두 줄 붙이기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('position').addEventListener('change', (event) => actions.position(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.position(api, event.target.value)}
>
<option value="bottom">맨 아래</option>
<option value="top">맨 위</option>
</select>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions, SummaryOptions } from '@maxgrid/react';
export interface Row {
id: number;
branch: string;
m1: number;
m2: number;
m3: number;
}
const branches = ['서울 본점', '강남점', '부산점', '대구점', '인천점', '광주점'];
// 단위는 백만원이에요. 지점마다 1~3월 매출이 있어요.
const sales = (i: number, k: number) => 20 + ((i * 13 + k * 7) % 70);
// 합계는 정수 그대로, 평균은 소수 첫째 자리까지만 보여 줘요(47.833 -> 47.8).
const aggregateText = (value: unknown) =>
Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 1 });
const months = ['m1', 'm2', 'm3'] as const;
// rows의 줄마다 이름(label)과 "어느 컬럼을 어떻게 계산할지"(aggregate)를 적어요.
// aggregate에 적은 컬럼만 계산하고, 적지 않은 컬럼은 빈 칸이에요.
const summary = (position: 'bottom' | 'top'): SummaryOptions => ({
position,
rows: [
{ id: 'sum', label: '합계', aggregate: { m1: 'sum', m2: 'sum', m3: 'sum' } },
{ id: 'avg', label: '평균', aggregate: { m1: 'avg', m2: 'avg', m3: 'avg' } },
],
});
export const options: GridOptions<Row> = {
columns: [
{ id: 'branch', title: '지점', width: 120 },
...months.map((id, k) => ({
id,
title: `${k + 1}월`,
kind: 'number' as const,
width: 90,
align: 'end' as const,
aggregateFormat: aggregateText,
})),
],
rows: branches.map((branch, i) => ({
id: i + 1,
branch,
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
})),
rowKey: 'id',
summary: summary('bottom'),
};
export const actions = {
position: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
};버튼을 누르면 이 예제의 코드가 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.position(api, ($event.target as HTMLSelectElement).value)"
>
<option value="bottom" selected>맨 아래</option>
<option value="top">맨 위</option>
</select>
</div>
<Grid style="height: 300px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions, SummaryOptions } from '@maxgrid/vue';
export interface Row {
id: number;
branch: string;
m1: number;
m2: number;
m3: number;
}
const branches = ['서울 본점', '강남점', '부산점', '대구점', '인천점', '광주점'];
// 단위는 백만원이에요. 지점마다 1~3월 매출이 있어요.
const sales = (i: number, k: number) => 20 + ((i * 13 + k * 7) % 70);
// 합계는 정수 그대로, 평균은 소수 첫째 자리까지만 보여 줘요(47.833 -> 47.8).
const aggregateText = (value: unknown) =>
Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 1 });
const months = ['m1', 'm2', 'm3'] as const;
// rows의 줄마다 이름(label)과 "어느 컬럼을 어떻게 계산할지"(aggregate)를 적어요.
// aggregate에 적은 컬럼만 계산하고, 적지 않은 컬럼은 빈 칸이에요.
const summary = (position: 'bottom' | 'top'): SummaryOptions => ({
position,
rows: [
{ id: 'sum', label: '합계', aggregate: { m1: 'sum', m2: 'sum', m3: 'sum' } },
{ id: 'avg', label: '평균', aggregate: { m1: 'avg', m2: 'avg', m3: 'avg' } },
],
});
export const options: GridOptions<Row> = {
columns: [
{ id: 'branch', title: '지점', width: 120 },
...months.map((id, k) => ({
id,
title: `${k + 1}월`,
kind: 'number' as const,
width: 90,
align: 'end' as const,
aggregateFormat: aggregateText,
})),
],
rows: branches.map((branch, i) => ({
id: i + 1,
branch,
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
})),
rowKey: 'id',
summary: summary('bottom'),
};
export const actions = {
position: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
};버튼을 누르면 이 예제의 코드가 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.position(api, $any($event.target).value)"
>
<option value="bottom" selected>맨 아래</option>
<option value="top">맨 위</option>
</select>
</div>
<mx-grid style="height: 300px" [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, SummaryOptions } from '@maxgrid/angular';
export interface Row {
id: number;
branch: string;
m1: number;
m2: number;
m3: number;
}
const branches = ['서울 본점', '강남점', '부산점', '대구점', '인천점', '광주점'];
// 단위는 백만원이에요. 지점마다 1~3월 매출이 있어요.
const sales = (i: number, k: number) => 20 + ((i * 13 + k * 7) % 70);
// 합계는 정수 그대로, 평균은 소수 첫째 자리까지만 보여 줘요(47.833 -> 47.8).
const aggregateText = (value: unknown) =>
Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 1 });
const months = ['m1', 'm2', 'm3'] as const;
// rows의 줄마다 이름(label)과 "어느 컬럼을 어떻게 계산할지"(aggregate)를 적어요.
// aggregate에 적은 컬럼만 계산하고, 적지 않은 컬럼은 빈 칸이에요.
const summary = (position: 'bottom' | 'top'): SummaryOptions => ({
position,
rows: [
{ id: 'sum', label: '합계', aggregate: { m1: 'sum', m2: 'sum', m3: 'sum' } },
{ id: 'avg', label: '평균', aggregate: { m1: 'avg', m2: 'avg', m3: 'avg' } },
],
});
export const options: GridOptions<Row> = {
columns: [
{ id: 'branch', title: '지점', width: 120 },
...months.map((id, k) => ({
id,
title: `${k + 1}월`,
kind: 'number' as const,
width: 90,
align: 'end' as const,
aggregateFormat: aggregateText,
})),
],
rows: branches.map((branch, i) => ({
id: i + 1,
branch,
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
})),
rowKey: 'id',
summary: summary('bottom'),
};
export const actions = {
position: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
};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="Row position" id="position">
<option value="bottom" selected>Bottom</option>
<option value="top">Top</option>
</select>
</div>
<div id="grid" style="height: 300px"></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('position').addEventListener('change', (event) => actions.position(api, event.target.value));const branches = ['Seoul HQ', 'Gangnam', 'Busan', 'Daegu', 'Incheon', 'Gwangju'];
// Units are $ thousands. Each branch has sales for January to March.
const sales = (i, k) => 20 + ((i * 13 + k * 7) % 70);
// The total is shown as an integer, the average to one decimal place (47.833 -> 47.8).
const aggregateText = (value) => Number(value).toLocaleString('en-US', { maximumFractionDigits: 1 });
const months = ['m1', 'm2', 'm3'];
// Each entry in rows gets a name (label) and "which column to calculate and how" (aggregate).
// Only the columns listed in aggregate are calculated; the others are left blank.
const summary = (position) => ({
position,
rows: [
{ id: 'sum', label: 'Total', aggregate: { m1: 'sum', m2: 'sum', m3: 'sum' } },
{ id: 'avg', label: 'Average', aggregate: { m1: 'avg', m2: 'avg', m3: 'avg' } },
],
});
export const options = {
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',
aggregateFormat: aggregateText,
})),
],
rows: branches.map((branch, i) => ({
id: i + 1,
branch,
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
})),
rowKey: 'id',
summary: summary('bottom'),
};
export const actions = {
position: (api, value) => api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Show two rows at the bottom: total and average</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Row position" id="position">
<option value="bottom" selected>Bottom</option>
<option value="top">Top</option>
</select>
</div>
<div id="grid" style="height: 300px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const branches = ['Seoul HQ', 'Gangnam', 'Busan', 'Daegu', 'Incheon', 'Gwangju'];
// Units are $ thousands. Each branch has sales for January to March.
const sales = (i, k) => 20 + ((i * 13 + k * 7) % 70);
// The total is shown as an integer, the average to one decimal place (47.833 -> 47.8).
const aggregateText = (value) => Number(value).toLocaleString('en-US', { maximumFractionDigits: 1 });
const months = ['m1', 'm2', 'm3'];
// Each entry in rows gets a name (label) and "which column to calculate and how" (aggregate).
// Only the columns listed in aggregate are calculated; the others are left blank.
const summary = (position) => ({
position,
rows: [
{ id: 'sum', label: 'Total', aggregate: { m1: 'sum', m2: 'sum', m3: 'sum' } },
{ id: 'avg', label: 'Average', aggregate: { m1: 'avg', m2: 'avg', m3: 'avg' } },
],
});
const options = {
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',
aggregateFormat: aggregateText,
})),
],
rows: branches.map((branch, i) => ({
id: i + 1,
branch,
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
})),
rowKey: 'id',
summary: summary('bottom'),
};
const actions = {
position: (api, value) => api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
};
options.ariaLabel = "Show two rows at the bottom: total and average";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('position').addEventListener('change', (event) => actions.position(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="Row position"
disabled={!api}
onChange={(event) => api && actions.position(api, event.target.value)}
>
<option value="bottom">Bottom</option>
<option value="top">Top</option>
</select>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions, SummaryOptions } from '@maxgrid/react';
export interface Row {
id: number;
branch: string;
m1: number;
m2: number;
m3: number;
}
const branches = ['Seoul HQ', 'Gangnam', 'Busan', 'Daegu', 'Incheon', 'Gwangju'];
// Units are $ thousands. Each branch has sales for January to March.
const sales = (i: number, k: number) => 20 + ((i * 13 + k * 7) % 70);
// The total is shown as an integer, the average to one decimal place (47.833 -> 47.8).
const aggregateText = (value: unknown) =>
Number(value).toLocaleString('en-US', { maximumFractionDigits: 1 });
const months = ['m1', 'm2', 'm3'] as const;
// Each entry in rows gets a name (label) and "which column to calculate and how" (aggregate).
// Only the columns listed in aggregate are calculated; the others are left blank.
const summary = (position: 'bottom' | 'top'): SummaryOptions => ({
position,
rows: [
{ id: 'sum', label: 'Total', aggregate: { m1: 'sum', m2: 'sum', m3: 'sum' } },
{ id: 'avg', label: 'Average', aggregate: { m1: 'avg', m2: 'avg', m3: 'avg' } },
],
});
export const options: GridOptions<Row> = {
columns: [
{ 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,
aggregateFormat: aggregateText,
})),
],
rows: branches.map((branch, i) => ({
id: i + 1,
branch,
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
})),
rowKey: 'id',
summary: summary('bottom'),
};
export const actions = {
position: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
};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="Row position"
:disabled="!api"
@change="api && actions.position(api, ($event.target as HTMLSelectElement).value)"
>
<option value="bottom" selected>Bottom</option>
<option value="top">Top</option>
</select>
</div>
<Grid style="height: 300px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions, SummaryOptions } from '@maxgrid/vue';
export interface Row {
id: number;
branch: string;
m1: number;
m2: number;
m3: number;
}
const branches = ['Seoul HQ', 'Gangnam', 'Busan', 'Daegu', 'Incheon', 'Gwangju'];
// Units are $ thousands. Each branch has sales for January to March.
const sales = (i: number, k: number) => 20 + ((i * 13 + k * 7) % 70);
// The total is shown as an integer, the average to one decimal place (47.833 -> 47.8).
const aggregateText = (value: unknown) =>
Number(value).toLocaleString('en-US', { maximumFractionDigits: 1 });
const months = ['m1', 'm2', 'm3'] as const;
// Each entry in rows gets a name (label) and "which column to calculate and how" (aggregate).
// Only the columns listed in aggregate are calculated; the others are left blank.
const summary = (position: 'bottom' | 'top'): SummaryOptions => ({
position,
rows: [
{ id: 'sum', label: 'Total', aggregate: { m1: 'sum', m2: 'sum', m3: 'sum' } },
{ id: 'avg', label: 'Average', aggregate: { m1: 'avg', m2: 'avg', m3: 'avg' } },
],
});
export const options: GridOptions<Row> = {
columns: [
{ 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,
aggregateFormat: aggregateText,
})),
],
rows: branches.map((branch, i) => ({
id: i + 1,
branch,
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
})),
rowKey: 'id',
summary: summary('bottom'),
};
export const actions = {
position: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
};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="Row position"
[disabled]="!api"
(change)="api && actions.position(api, $any($event.target).value)"
>
<option value="bottom" selected>Bottom</option>
<option value="top">Top</option>
</select>
</div>
<mx-grid style="height: 300px" [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, SummaryOptions } from '@maxgrid/angular';
export interface Row {
id: number;
branch: string;
m1: number;
m2: number;
m3: number;
}
const branches = ['Seoul HQ', 'Gangnam', 'Busan', 'Daegu', 'Incheon', 'Gwangju'];
// Units are $ thousands. Each branch has sales for January to March.
const sales = (i: number, k: number) => 20 + ((i * 13 + k * 7) % 70);
// The total is shown as an integer, the average to one decimal place (47.833 -> 47.8).
const aggregateText = (value: unknown) =>
Number(value).toLocaleString('en-US', { maximumFractionDigits: 1 });
const months = ['m1', 'm2', 'm3'] as const;
// Each entry in rows gets a name (label) and "which column to calculate and how" (aggregate).
// Only the columns listed in aggregate are calculated; the others are left blank.
const summary = (position: 'bottom' | 'top'): SummaryOptions => ({
position,
rows: [
{ id: 'sum', label: 'Total', aggregate: { m1: 'sum', m2: 'sum', m3: 'sum' } },
{ id: 'avg', label: 'Average', aggregate: { m1: 'avg', m2: 'avg', m3: 'avg' } },
],
});
export const options: GridOptions<Row> = {
columns: [
{ 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,
aggregateFormat: aggregateText,
})),
],
rows: branches.map((branch, i) => ({
id: i + 1,
branch,
m1: sales(i, 1),
m2: sales(i, 2),
m3: sales(i, 3),
})),
rowKey: 'id',
summary: summary('bottom'),
};
export const actions = {
position: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
};