페이지를 나눠도 합계·평균 두 줄은 그대로Two summary rows that stay put across pages
45건을 10건씩 나눠 보여 주면서 맨 아래에 합계와 평균 두 줄을 붙였어요. 두 줄은 이 페이지가 아니라 전체 행으로 계산하고, 페이지를 넘겨도 그 자리에 있어요.45 rows are shown 10 at a time with two summary rows at the bottom: total and average. Both are calculated over all rows, not just this page, and stay in place as you change pages.
코드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">
<button type="button" id="next">다음 페이지</button>
<button type="button" id="last">마지막 페이지</button>
<select aria-label="줄 위치" id="position">
<option value="bottom" selected>맨 아래</option>
<option value="top">맨 위</option>
</select>
</div>
<div id="grid" style="height: 520px"></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('next').addEventListener('click', () => actions.next(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
document.getElementById('position').addEventListener('change', (event) => actions.position(api, event.target.value));const products = ['무선 이어폰', '스마트 워치', '노트북 거치대', '보조배터리', '블루투스 스피커'];
const won = (value) => Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 0 });
// 요약 줄 두 개(합계, 평균)는 "페이지에 보이는 행"이 아니라 조건에 맞는 모든 행으로 계산해요.
// 그래서 페이지를 넘겨도 두 줄의 값은 그대로이고, 줄은 늘 표 맨 아래(또는 맨 위)에 붙어 있어요.
const summary = (position) => ({
position,
rows: [
{ id: 'sum', label: '전체 합계', aggregate: { quantity: 'sum', amount: 'sum' } },
{ id: 'avg', label: '전체 평균', aggregate: { quantity: 'avg', amount: 'avg' } },
],
});
export const options = {
columns: [
{ id: 'product', title: '상품', width: 170 },
{
id: 'quantity',
title: '수량',
kind: 'number',
width: 110,
align: 'end',
aggregateFormat: won,
},
{
id: 'amount',
title: '금액(원)',
kind: 'number',
width: 140,
align: 'end',
formatValue: won,
aggregateFormat: won,
},
],
rows: Array.from({ length: 45 }, (_, i) => ({
id: i + 1,
product: products[(i * 3) % 5],
quantity: 1 + ((i * 7) % 20),
amount: 10000 + ((i * 7919) % 90000),
})),
rowKey: 'id',
showRowNumbers: true,
summary: summary('bottom'),
pagination: { mode: 'client', pageSize: 10 },
};
export function setup(api, log) {
const off = api.on('pageChanged', ({ page, pageCount }) => {
const total = api.getSummary().amount;
log({
ko: `${page} / ${pageCount} 페이지 · 전체 금액 합계 ${won(total)}`,
en: `Page ${page} / ${pageCount} · total of all amounts ${won(total)}`,
});
});
return () => off();
}
export const actions = {
position: (api, value) => api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
next: (api) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
last: (api) => api.setPage(api.getPageCount()),
};<!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">
<button type="button" id="next">다음 페이지</button>
<button type="button" id="last">마지막 페이지</button>
<select aria-label="줄 위치" id="position">
<option value="bottom" selected>맨 아래</option>
<option value="top">맨 위</option>
</select>
</div>
<div id="grid" style="height: 520px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const products = ['무선 이어폰', '스마트 워치', '노트북 거치대', '보조배터리', '블루투스 스피커'];
const won = (value) => Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 0 });
// 요약 줄 두 개(합계, 평균)는 "페이지에 보이는 행"이 아니라 조건에 맞는 모든 행으로 계산해요.
// 그래서 페이지를 넘겨도 두 줄의 값은 그대로이고, 줄은 늘 표 맨 아래(또는 맨 위)에 붙어 있어요.
const summary = (position) => ({
position,
rows: [
{ id: 'sum', label: '전체 합계', aggregate: { quantity: 'sum', amount: 'sum' } },
{ id: 'avg', label: '전체 평균', aggregate: { quantity: 'avg', amount: 'avg' } },
],
});
const options = {
columns: [
{ id: 'product', title: '상품', width: 170 },
{
id: 'quantity',
title: '수량',
kind: 'number',
width: 110,
align: 'end',
aggregateFormat: won,
},
{
id: 'amount',
title: '금액(원)',
kind: 'number',
width: 140,
align: 'end',
formatValue: won,
aggregateFormat: won,
},
],
rows: Array.from({ length: 45 }, (_, i) => ({
id: i + 1,
product: products[(i * 3) % 5],
quantity: 1 + ((i * 7) % 20),
amount: 10000 + ((i * 7919) % 90000),
})),
rowKey: 'id',
showRowNumbers: true,
summary: summary('bottom'),
pagination: { mode: 'client', pageSize: 10 },
};
function setup(api, log) {
const off = api.on('pageChanged', ({ page, pageCount }) => {
const total = api.getSummary().amount;
log({
ko: `${page} / ${pageCount} 페이지 · 전체 금액 합계 ${won(total)}`,
en: `Page ${page} / ${pageCount} · total of all amounts ${won(total)}`,
});
});
return () => off();
}
const actions = {
position: (api, value) => api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
next: (api) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
last: (api) => api.setPage(api.getPageCount()),
};
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('next').addEventListener('click', () => actions.next(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
document.getElementById('position').addEventListener('change', (event) => actions.position(api, event.target.value));
</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">
<button
type="button"
disabled={!api}
onClick={() => api && actions.next(api)}
>다음 페이지</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.last(api)}
>마지막 페이지</button>
<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: 520 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions, SummaryOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
product: string;
quantity: number;
amount: number;
}
const products = ['무선 이어폰', '스마트 워치', '노트북 거치대', '보조배터리', '블루투스 스피커'];
const won = (value: unknown) => Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 0 });
// 요약 줄 두 개(합계, 평균)는 "페이지에 보이는 행"이 아니라 조건에 맞는 모든 행으로 계산해요.
// 그래서 페이지를 넘겨도 두 줄의 값은 그대로이고, 줄은 늘 표 맨 아래(또는 맨 위)에 붙어 있어요.
const summary = (position: 'bottom' | 'top'): SummaryOptions => ({
position,
rows: [
{ id: 'sum', label: '전체 합계', aggregate: { quantity: 'sum', amount: 'sum' } },
{ id: 'avg', label: '전체 평균', aggregate: { quantity: 'avg', amount: 'avg' } },
],
});
export const options: GridOptions<Row> = {
columns: [
{ id: 'product', title: '상품', width: 170 },
{
id: 'quantity',
title: '수량',
kind: 'number',
width: 110,
align: 'end',
aggregateFormat: won,
},
{
id: 'amount',
title: '금액(원)',
kind: 'number',
width: 140,
align: 'end',
formatValue: won,
aggregateFormat: won,
},
],
rows: Array.from({ length: 45 }, (_, i) => ({
id: i + 1,
product: products[(i * 3) % 5] as string,
quantity: 1 + ((i * 7) % 20),
amount: 10000 + ((i * 7919) % 90000),
})),
rowKey: 'id',
showRowNumbers: true,
summary: summary('bottom'),
pagination: { mode: 'client', pageSize: 10 },
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const off = api.on('pageChanged', ({ page, pageCount }) => {
const total = api.getSummary().amount;
log({
ko: `${page} / ${pageCount} 페이지 · 전체 금액 합계 ${won(total)}`,
en: `Page ${page} / ${pageCount} · total of all amounts ${won(total)}`,
});
});
return () => off();
}
export const actions = {
position: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
next: (api: GridApi<Row>) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
last: (api: GridApi<Row>) => api.setPage(api.getPageCount()),
};버튼을 누르면 이 예제의 코드가 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">
<button type="button" :disabled="!api" @click="api && actions.next(api)">다음 페이지</button>
<button type="button" :disabled="!api" @click="api && actions.last(api)">마지막 페이지</button>
<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: 520px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions, SummaryOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
product: string;
quantity: number;
amount: number;
}
const products = ['무선 이어폰', '스마트 워치', '노트북 거치대', '보조배터리', '블루투스 스피커'];
const won = (value: unknown) => Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 0 });
// 요약 줄 두 개(합계, 평균)는 "페이지에 보이는 행"이 아니라 조건에 맞는 모든 행으로 계산해요.
// 그래서 페이지를 넘겨도 두 줄의 값은 그대로이고, 줄은 늘 표 맨 아래(또는 맨 위)에 붙어 있어요.
const summary = (position: 'bottom' | 'top'): SummaryOptions => ({
position,
rows: [
{ id: 'sum', label: '전체 합계', aggregate: { quantity: 'sum', amount: 'sum' } },
{ id: 'avg', label: '전체 평균', aggregate: { quantity: 'avg', amount: 'avg' } },
],
});
export const options: GridOptions<Row> = {
columns: [
{ id: 'product', title: '상품', width: 170 },
{
id: 'quantity',
title: '수량',
kind: 'number',
width: 110,
align: 'end',
aggregateFormat: won,
},
{
id: 'amount',
title: '금액(원)',
kind: 'number',
width: 140,
align: 'end',
formatValue: won,
aggregateFormat: won,
},
],
rows: Array.from({ length: 45 }, (_, i) => ({
id: i + 1,
product: products[(i * 3) % 5] as string,
quantity: 1 + ((i * 7) % 20),
amount: 10000 + ((i * 7919) % 90000),
})),
rowKey: 'id',
showRowNumbers: true,
summary: summary('bottom'),
pagination: { mode: 'client', pageSize: 10 },
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const off = api.on('pageChanged', ({ page, pageCount }) => {
const total = api.getSummary().amount;
log({
ko: `${page} / ${pageCount} 페이지 · 전체 금액 합계 ${won(total)}`,
en: `Page ${page} / ${pageCount} · total of all amounts ${won(total)}`,
});
});
return () => off();
}
export const actions = {
position: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
next: (api: GridApi<Row>) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
last: (api: GridApi<Row>) => api.setPage(api.getPageCount()),
};버튼을 누르면 이 예제의 코드가 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">
<button
type="button"
[disabled]="!api"
(click)="api && actions.next(api)"
>다음 페이지</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.last(api)"
>마지막 페이지</button>
<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: 520px" [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, SummaryOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
product: string;
quantity: number;
amount: number;
}
const products = ['무선 이어폰', '스마트 워치', '노트북 거치대', '보조배터리', '블루투스 스피커'];
const won = (value: unknown) => Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 0 });
// 요약 줄 두 개(합계, 평균)는 "페이지에 보이는 행"이 아니라 조건에 맞는 모든 행으로 계산해요.
// 그래서 페이지를 넘겨도 두 줄의 값은 그대로이고, 줄은 늘 표 맨 아래(또는 맨 위)에 붙어 있어요.
const summary = (position: 'bottom' | 'top'): SummaryOptions => ({
position,
rows: [
{ id: 'sum', label: '전체 합계', aggregate: { quantity: 'sum', amount: 'sum' } },
{ id: 'avg', label: '전체 평균', aggregate: { quantity: 'avg', amount: 'avg' } },
],
});
export const options: GridOptions<Row> = {
columns: [
{ id: 'product', title: '상품', width: 170 },
{
id: 'quantity',
title: '수량',
kind: 'number',
width: 110,
align: 'end',
aggregateFormat: won,
},
{
id: 'amount',
title: '금액(원)',
kind: 'number',
width: 140,
align: 'end',
formatValue: won,
aggregateFormat: won,
},
],
rows: Array.from({ length: 45 }, (_, i) => ({
id: i + 1,
product: products[(i * 3) % 5] as string,
quantity: 1 + ((i * 7) % 20),
amount: 10000 + ((i * 7919) % 90000),
})),
rowKey: 'id',
showRowNumbers: true,
summary: summary('bottom'),
pagination: { mode: 'client', pageSize: 10 },
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const off = api.on('pageChanged', ({ page, pageCount }) => {
const total = api.getSummary().amount;
log({
ko: `${page} / ${pageCount} 페이지 · 전체 금액 합계 ${won(total)}`,
en: `Page ${page} / ${pageCount} · total of all amounts ${won(total)}`,
});
});
return () => off();
}
export const actions = {
position: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
next: (api: GridApi<Row>) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
last: (api: GridApi<Row>) => api.setPage(api.getPageCount()),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div class="toolbar">
<button type="button" id="next">Next page</button>
<button type="button" id="last">Last page</button>
<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: 520px"></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('next').addEventListener('click', () => actions.next(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
document.getElementById('position').addEventListener('change', (event) => actions.position(api, event.target.value));const products = [
'Wireless earbuds',
'Smart watch',
'Laptop stand',
'Power bank',
'Bluetooth speaker',
];
const won = (value) => Number(value).toLocaleString('en-US', { maximumFractionDigits: 0 });
// The two summary rows (total, average) are computed from all rows that match, not from the rows visible on the page.
// So the values of both rows stay the same as you change pages, and the rows always stick to the bottom (or top) of the table.
const summary = (position) => ({
position,
rows: [
{ id: 'sum', label: 'Overall total', aggregate: { quantity: 'sum', amount: 'sum' } },
{ id: 'avg', label: 'Overall average', aggregate: { quantity: 'avg', amount: 'avg' } },
],
});
export const options = {
columns: [
{ id: 'product', title: 'Product', width: 170 },
{
id: 'quantity',
title: 'Qty',
kind: 'number',
width: 110,
align: 'end',
aggregateFormat: won,
},
{
id: 'amount',
title: 'Amount ($)',
kind: 'number',
width: 140,
align: 'end',
formatValue: won,
aggregateFormat: won,
},
],
rows: Array.from({ length: 45 }, (_, i) => ({
id: i + 1,
product: products[(i * 3) % 5],
quantity: 1 + ((i * 7) % 20),
amount: 10 + Math.round(((i * 7919) % 90000) / 1000),
})),
rowKey: 'id',
showRowNumbers: true,
summary: summary('bottom'),
pagination: { mode: 'client', pageSize: 10 },
};
export function setup(api, log) {
const off = api.on('pageChanged', ({ page, pageCount }) => {
const total = api.getSummary().amount;
log(`Page ${page} / ${pageCount} · total of all amounts ${won(total)}`);
});
return () => off();
}
export const actions = {
position: (api, value) => api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
next: (api) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
last: (api) => api.setPage(api.getPageCount()),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Two summary rows that stay put across pages</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="next">Next page</button>
<button type="button" id="last">Last page</button>
<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: 520px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const products = [
'Wireless earbuds',
'Smart watch',
'Laptop stand',
'Power bank',
'Bluetooth speaker',
];
const won = (value) => Number(value).toLocaleString('en-US', { maximumFractionDigits: 0 });
// The two summary rows (total, average) are computed from all rows that match, not from the rows visible on the page.
// So the values of both rows stay the same as you change pages, and the rows always stick to the bottom (or top) of the table.
const summary = (position) => ({
position,
rows: [
{ id: 'sum', label: 'Overall total', aggregate: { quantity: 'sum', amount: 'sum' } },
{ id: 'avg', label: 'Overall average', aggregate: { quantity: 'avg', amount: 'avg' } },
],
});
const options = {
columns: [
{ id: 'product', title: 'Product', width: 170 },
{
id: 'quantity',
title: 'Qty',
kind: 'number',
width: 110,
align: 'end',
aggregateFormat: won,
},
{
id: 'amount',
title: 'Amount ($)',
kind: 'number',
width: 140,
align: 'end',
formatValue: won,
aggregateFormat: won,
},
],
rows: Array.from({ length: 45 }, (_, i) => ({
id: i + 1,
product: products[(i * 3) % 5],
quantity: 1 + ((i * 7) % 20),
amount: 10 + Math.round(((i * 7919) % 90000) / 1000),
})),
rowKey: 'id',
showRowNumbers: true,
summary: summary('bottom'),
pagination: { mode: 'client', pageSize: 10 },
};
function setup(api, log) {
const off = api.on('pageChanged', ({ page, pageCount }) => {
const total = api.getSummary().amount;
log(`Page ${page} / ${pageCount} · total of all amounts ${won(total)}`);
});
return () => off();
}
const actions = {
position: (api, value) => api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
next: (api) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
last: (api) => api.setPage(api.getPageCount()),
};
options.ariaLabel = "Two summary rows that stay put across pages";
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('next').addEventListener('click', () => actions.next(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
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 { 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">
<button
type="button"
disabled={!api}
onClick={() => api && actions.next(api)}
>Next page</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.last(api)}
>Last page</button>
<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: 520 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions, SummaryOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
product: string;
quantity: number;
amount: number;
}
const products = [
'Wireless earbuds',
'Smart watch',
'Laptop stand',
'Power bank',
'Bluetooth speaker',
];
const won = (value: unknown) => Number(value).toLocaleString('en-US', { maximumFractionDigits: 0 });
// The two summary rows (total, average) are computed from all rows that match, not from the rows visible on the page.
// So the values of both rows stay the same as you change pages, and the rows always stick to the bottom (or top) of the table.
const summary = (position: 'bottom' | 'top'): SummaryOptions => ({
position,
rows: [
{ id: 'sum', label: 'Overall total', aggregate: { quantity: 'sum', amount: 'sum' } },
{ id: 'avg', label: 'Overall average', aggregate: { quantity: 'avg', amount: 'avg' } },
],
});
export const options: GridOptions<Row> = {
columns: [
{ id: 'product', title: 'Product', width: 170 },
{
id: 'quantity',
title: 'Qty',
kind: 'number',
width: 110,
align: 'end',
aggregateFormat: won,
},
{
id: 'amount',
title: 'Amount ($)',
kind: 'number',
width: 140,
align: 'end',
formatValue: won,
aggregateFormat: won,
},
],
rows: Array.from({ length: 45 }, (_, i) => ({
id: i + 1,
product: products[(i * 3) % 5] as string,
quantity: 1 + ((i * 7) % 20),
amount: 10 + Math.round(((i * 7919) % 90000) / 1000),
})),
rowKey: 'id',
showRowNumbers: true,
summary: summary('bottom'),
pagination: { mode: 'client', pageSize: 10 },
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const off = api.on('pageChanged', ({ page, pageCount }) => {
const total = api.getSummary().amount;
log(`Page ${page} / ${pageCount} · total of all amounts ${won(total)}`);
});
return () => off();
}
export const actions = {
position: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
next: (api: GridApi<Row>) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
last: (api: GridApi<Row>) => api.setPage(api.getPageCount()),
};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">
<button type="button" :disabled="!api" @click="api && actions.next(api)">Next page</button>
<button type="button" :disabled="!api" @click="api && actions.last(api)">Last page</button>
<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: 520px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions, SummaryOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
product: string;
quantity: number;
amount: number;
}
const products = [
'Wireless earbuds',
'Smart watch',
'Laptop stand',
'Power bank',
'Bluetooth speaker',
];
const won = (value: unknown) => Number(value).toLocaleString('en-US', { maximumFractionDigits: 0 });
// The two summary rows (total, average) are computed from all rows that match, not from the rows visible on the page.
// So the values of both rows stay the same as you change pages, and the rows always stick to the bottom (or top) of the table.
const summary = (position: 'bottom' | 'top'): SummaryOptions => ({
position,
rows: [
{ id: 'sum', label: 'Overall total', aggregate: { quantity: 'sum', amount: 'sum' } },
{ id: 'avg', label: 'Overall average', aggregate: { quantity: 'avg', amount: 'avg' } },
],
});
export const options: GridOptions<Row> = {
columns: [
{ id: 'product', title: 'Product', width: 170 },
{
id: 'quantity',
title: 'Qty',
kind: 'number',
width: 110,
align: 'end',
aggregateFormat: won,
},
{
id: 'amount',
title: 'Amount ($)',
kind: 'number',
width: 140,
align: 'end',
formatValue: won,
aggregateFormat: won,
},
],
rows: Array.from({ length: 45 }, (_, i) => ({
id: i + 1,
product: products[(i * 3) % 5] as string,
quantity: 1 + ((i * 7) % 20),
amount: 10 + Math.round(((i * 7919) % 90000) / 1000),
})),
rowKey: 'id',
showRowNumbers: true,
summary: summary('bottom'),
pagination: { mode: 'client', pageSize: 10 },
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const off = api.on('pageChanged', ({ page, pageCount }) => {
const total = api.getSummary().amount;
log(`Page ${page} / ${pageCount} · total of all amounts ${won(total)}`);
});
return () => off();
}
export const actions = {
position: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
next: (api: GridApi<Row>) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
last: (api: GridApi<Row>) => api.setPage(api.getPageCount()),
};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">
<button
type="button"
[disabled]="!api"
(click)="api && actions.next(api)"
>Next page</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.last(api)"
>Last page</button>
<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: 520px" [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, SummaryOptions } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
product: string;
quantity: number;
amount: number;
}
const products = [
'Wireless earbuds',
'Smart watch',
'Laptop stand',
'Power bank',
'Bluetooth speaker',
];
const won = (value: unknown) => Number(value).toLocaleString('en-US', { maximumFractionDigits: 0 });
// The two summary rows (total, average) are computed from all rows that match, not from the rows visible on the page.
// So the values of both rows stay the same as you change pages, and the rows always stick to the bottom (or top) of the table.
const summary = (position: 'bottom' | 'top'): SummaryOptions => ({
position,
rows: [
{ id: 'sum', label: 'Overall total', aggregate: { quantity: 'sum', amount: 'sum' } },
{ id: 'avg', label: 'Overall average', aggregate: { quantity: 'avg', amount: 'avg' } },
],
});
export const options: GridOptions<Row> = {
columns: [
{ id: 'product', title: 'Product', width: 170 },
{
id: 'quantity',
title: 'Qty',
kind: 'number',
width: 110,
align: 'end',
aggregateFormat: won,
},
{
id: 'amount',
title: 'Amount ($)',
kind: 'number',
width: 140,
align: 'end',
formatValue: won,
aggregateFormat: won,
},
],
rows: Array.from({ length: 45 }, (_, i) => ({
id: i + 1,
product: products[(i * 3) % 5] as string,
quantity: 1 + ((i * 7) % 20),
amount: 10 + Math.round(((i * 7919) % 90000) / 1000),
})),
rowKey: 'id',
showRowNumbers: true,
summary: summary('bottom'),
pagination: { mode: 'client', pageSize: 10 },
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const off = api.on('pageChanged', ({ page, pageCount }) => {
const total = api.getSummary().amount;
log(`Page ${page} / ${pageCount} · total of all amounts ${won(total)}`);
});
return () => off();
}
export const actions = {
position: (api: GridApi<Row>, value?: string) =>
api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
next: (api: GridApi<Row>) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
last: (api: GridApi<Row>) => api.setPage(api.getPageCount()),
};