마스터 그리드와 요약 그리드 연결Master grid with a summary grid
편집할 수 있는 마스터와 읽기 전용 요약이 같은 저장소를 봐요. 마스터를 고치면 요약과 합계가 따라와요.An editable master and a read-only summary read the same store. Editing the master updates the summary and the total.
코드Code
탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.
이 화면은 새 모양 옵션을 켠 모양이에요: iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: 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="add">행 추가</button>
<button type="button" id="double">첫 행 수량 두 배</button>
<button type="button" id="remove">마지막 행 지우기</button>
</div>
<div id="grid" style="height: 260px"></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('add').addEventListener('click', () => actions.add(api));
document.getElementById('double').addEventListener('click', () => actions.double(api));
document.getElementById('remove').addEventListener('click', () => actions.remove(api));const store = MaxGrid.createRowsStore([
{ id: 1, name: '김하늘', team: '개발', qty: 4 },
{ id: 2, name: '이도윤', team: '운영', qty: 9 },
{ id: 3, name: '박서연', team: '디자인', qty: 2 },
{ id: 4, name: '최지우', team: '개발', qty: 12 },
{ id: 5, name: '정민준', team: '운영', qty: 6 },
], { rowKey: 'id' });
// 위 마스터 그리드는 편집할 수 있고, 아래 요약 그리드는 같은 저장소를 읽기 전용으로 작게 보여 줘요(이름·수량만, 수량 내림차순).
// 요약 그리드는 마스터에서 고친 값을 다음 프레임에 그대로 받고, 저장소 구독으로 합계를 기록 줄에 알려 줘요.
export const options = {
density: 'normal', // 틀이 높이를 키우지 않게 고정(두 그리드 높이를 직접 정한다)
columns: [
{ id: 'name', title: '이름', editable: true, width: 120 },
{ id: 'team', title: '팀', editable: true, width: 90 },
{ id: 'qty', title: '수량', kind: 'number', editable: true, width: 80 },
],
rowsStore: store,
ariaLabel: '마스터',
};
let summary = null;
let host = null;
let nextId = 100;
const total = () => store.getRows().reduce((sum, row) => sum + row.qty, 0);
export function setup(api, log) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null)
first.after(host);
else
document.body.append(host);
summary = MaxGrid.createGrid(host, {
locale: 'ko',
columns: [
{ id: 'name', title: '이름', width: 120 },
{ id: 'qty', title: '수량', kind: 'number', width: 80 },
],
rowsStore: store,
ariaLabel: '요약',
});
summary.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe(() => log({ ko: `수량 합계: ${total()}`, en: `Total quantity: ${total()}` }));
log({
ko: `마스터에서 수량을 고치면 요약이 따라와요. 지금 합계는 ${total()}예요.`,
en: `Edit a quantity in the master and the summary follows. The total is ${total()} now.`,
});
return () => {
off();
summary?.destroy();
host?.remove();
summary = host = null;
};
}
export const actions = {
add: (api) => {
api.addRows([{ id: nextId, name: `새 사람 ${nextId}`, team: '개발', qty: 5 }]);
nextId += 1;
},
double: (api) => {
const row = api.getRows()[0];
if (row)
api.setCellValue(row.id, 'qty', row.qty * 2);
},
remove: (api) => {
const last = api.getRows().at(-1);
if (last)
api.removeRows([last.id]);
},
};<!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="add">행 추가</button>
<button type="button" id="double">첫 행 수량 두 배</button>
<button type="button" id="remove">마지막 행 지우기</button>
</div>
<div id="grid" style="height: 260px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const store = MaxGrid.createRowsStore([
{ id: 1, name: '김하늘', team: '개발', qty: 4 },
{ id: 2, name: '이도윤', team: '운영', qty: 9 },
{ id: 3, name: '박서연', team: '디자인', qty: 2 },
{ id: 4, name: '최지우', team: '개발', qty: 12 },
{ id: 5, name: '정민준', team: '운영', qty: 6 },
], { rowKey: 'id' });
// 위 마스터 그리드는 편집할 수 있고, 아래 요약 그리드는 같은 저장소를 읽기 전용으로 작게 보여 줘요(이름·수량만, 수량 내림차순).
// 요약 그리드는 마스터에서 고친 값을 다음 프레임에 그대로 받고, 저장소 구독으로 합계를 기록 줄에 알려 줘요.
const options = {
density: 'normal', // 틀이 높이를 키우지 않게 고정(두 그리드 높이를 직접 정한다)
columns: [
{ id: 'name', title: '이름', editable: true, width: 120 },
{ id: 'team', title: '팀', editable: true, width: 90 },
{ id: 'qty', title: '수량', kind: 'number', editable: true, width: 80 },
],
rowsStore: store,
ariaLabel: '마스터',
};
let summary = null;
let host = null;
let nextId = 100;
const total = () => store.getRows().reduce((sum, row) => sum + row.qty, 0);
function setup(api, log) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null)
first.after(host);
else
document.body.append(host);
summary = MaxGrid.createGrid(host, {
locale: 'ko',
columns: [
{ id: 'name', title: '이름', width: 120 },
{ id: 'qty', title: '수량', kind: 'number', width: 80 },
],
rowsStore: store,
ariaLabel: '요약',
});
summary.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe(() => log({ ko: `수량 합계: ${total()}`, en: `Total quantity: ${total()}` }));
log({
ko: `마스터에서 수량을 고치면 요약이 따라와요. 지금 합계는 ${total()}예요.`,
en: `Edit a quantity in the master and the summary follows. The total is ${total()} now.`,
});
return () => {
off();
summary?.destroy();
host?.remove();
summary = host = null;
};
}
const actions = {
add: (api) => {
api.addRows([{ id: nextId, name: `새 사람 ${nextId}`, team: '개발', qty: 5 }]);
nextId += 1;
},
double: (api) => {
const row = api.getRows()[0];
if (row)
api.setCellValue(row.id, 'qty', row.qty * 2);
},
remove: (api) => {
const last = api.getRows().at(-1);
if (last)
api.removeRows([last.id]);
},
};
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('add').addEventListener('click', () => actions.add(api));
document.getElementById('double').addEventListener('click', () => actions.double(api));
document.getElementById('remove').addEventListener('click', () => actions.remove(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">
<button
type="button"
disabled={!api}
onClick={() => api && actions.add(api)}
>행 추가</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.double(api)}
>첫 행 수량 두 배</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.remove(api)}
>마지막 행 지우기</button>
</div>
<Grid style={{ height: 260 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions, RowsStore } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
qty: number;
}
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
createRowsStore(rows: Row[], options: { rowKey: 'id' }): RowsStore<Row>;
};
const store = MaxGrid.createRowsStore(
[
{ id: 1, name: '김하늘', team: '개발', qty: 4 },
{ id: 2, name: '이도윤', team: '운영', qty: 9 },
{ id: 3, name: '박서연', team: '디자인', qty: 2 },
{ id: 4, name: '최지우', team: '개발', qty: 12 },
{ id: 5, name: '정민준', team: '운영', qty: 6 },
],
{ rowKey: 'id' },
);
// 위 마스터 그리드는 편집할 수 있고, 아래 요약 그리드는 같은 저장소를 읽기 전용으로 작게 보여 줘요(이름·수량만, 수량 내림차순).
// 요약 그리드는 마스터에서 고친 값을 다음 프레임에 그대로 받고, 저장소 구독으로 합계를 기록 줄에 알려 줘요.
export const options: GridOptions<Row> = {
density: 'normal', // 틀이 높이를 키우지 않게 고정(두 그리드 높이를 직접 정한다)
columns: [
{ id: 'name', title: '이름', editable: true, width: 120 },
{ id: 'team', title: '팀', editable: true, width: 90 },
{ id: 'qty', title: '수량', kind: 'number', editable: true, width: 80 },
],
rowsStore: store,
ariaLabel: '마스터',
};
let summary: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
let nextId = 100;
const total = (): number => store.getRows().reduce((sum, row) => sum + row.qty, 0);
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
summary = MaxGrid.createGrid(host, {
locale: 'ko',
columns: [
{ id: 'name', title: '이름', width: 120 },
{ id: 'qty', title: '수량', kind: 'number', width: 80 },
],
rowsStore: store,
ariaLabel: '요약',
});
summary.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe(() =>
log({ ko: `수량 합계: ${total()}`, en: `Total quantity: ${total()}` }),
);
log({
ko: `마스터에서 수량을 고치면 요약이 따라와요. 지금 합계는 ${total()}예요.`,
en: `Edit a quantity in the master and the summary follows. The total is ${total()} now.`,
});
return () => {
off();
summary?.destroy();
host?.remove();
summary = host = null;
};
}
export const actions = {
add: (api: GridApi<Row>) => {
api.addRows([{ id: nextId, name: `새 사람 ${nextId}`, team: '개발', qty: 5 }]);
nextId += 1;
},
double: (api: GridApi<Row>) => {
const row = api.getRows()[0];
if (row) api.setCellValue(row.id, 'qty', row.qty * 2);
},
remove: (api: GridApi<Row>) => {
const last = api.getRows().at(-1);
if (last) api.removeRows([last.id]);
},
};버튼을 누르면 이 예제의 코드가 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.add(api)">행 추가</button>
<button type="button" :disabled="!api" @click="api && actions.double(api)">첫 행 수량 두 배</button>
<button type="button" :disabled="!api" @click="api && actions.remove(api)">마지막 행 지우기</button>
</div>
<Grid style="height: 260px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions, RowsStore } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
qty: number;
}
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
createRowsStore(rows: Row[], options: { rowKey: 'id' }): RowsStore<Row>;
};
const store = MaxGrid.createRowsStore(
[
{ id: 1, name: '김하늘', team: '개발', qty: 4 },
{ id: 2, name: '이도윤', team: '운영', qty: 9 },
{ id: 3, name: '박서연', team: '디자인', qty: 2 },
{ id: 4, name: '최지우', team: '개발', qty: 12 },
{ id: 5, name: '정민준', team: '운영', qty: 6 },
],
{ rowKey: 'id' },
);
// 위 마스터 그리드는 편집할 수 있고, 아래 요약 그리드는 같은 저장소를 읽기 전용으로 작게 보여 줘요(이름·수량만, 수량 내림차순).
// 요약 그리드는 마스터에서 고친 값을 다음 프레임에 그대로 받고, 저장소 구독으로 합계를 기록 줄에 알려 줘요.
export const options: GridOptions<Row> = {
density: 'normal', // 틀이 높이를 키우지 않게 고정(두 그리드 높이를 직접 정한다)
columns: [
{ id: 'name', title: '이름', editable: true, width: 120 },
{ id: 'team', title: '팀', editable: true, width: 90 },
{ id: 'qty', title: '수량', kind: 'number', editable: true, width: 80 },
],
rowsStore: store,
ariaLabel: '마스터',
};
let summary: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
let nextId = 100;
const total = (): number => store.getRows().reduce((sum, row) => sum + row.qty, 0);
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
summary = MaxGrid.createGrid(host, {
locale: 'ko',
columns: [
{ id: 'name', title: '이름', width: 120 },
{ id: 'qty', title: '수량', kind: 'number', width: 80 },
],
rowsStore: store,
ariaLabel: '요약',
});
summary.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe(() =>
log({ ko: `수량 합계: ${total()}`, en: `Total quantity: ${total()}` }),
);
log({
ko: `마스터에서 수량을 고치면 요약이 따라와요. 지금 합계는 ${total()}예요.`,
en: `Edit a quantity in the master and the summary follows. The total is ${total()} now.`,
});
return () => {
off();
summary?.destroy();
host?.remove();
summary = host = null;
};
}
export const actions = {
add: (api: GridApi<Row>) => {
api.addRows([{ id: nextId, name: `새 사람 ${nextId}`, team: '개발', qty: 5 }]);
nextId += 1;
},
double: (api: GridApi<Row>) => {
const row = api.getRows()[0];
if (row) api.setCellValue(row.id, 'qty', row.qty * 2);
},
remove: (api: GridApi<Row>) => {
const last = api.getRows().at(-1);
if (last) api.removeRows([last.id]);
},
};버튼을 누르면 이 예제의 코드가 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.add(api)"
>행 추가</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.double(api)"
>첫 행 수량 두 배</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.remove(api)"
>마지막 행 지우기</button>
</div>
<mx-grid style="height: 260px" [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, RowsStore } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
qty: number;
}
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
createRowsStore(rows: Row[], options: { rowKey: 'id' }): RowsStore<Row>;
};
const store = MaxGrid.createRowsStore(
[
{ id: 1, name: '김하늘', team: '개발', qty: 4 },
{ id: 2, name: '이도윤', team: '운영', qty: 9 },
{ id: 3, name: '박서연', team: '디자인', qty: 2 },
{ id: 4, name: '최지우', team: '개발', qty: 12 },
{ id: 5, name: '정민준', team: '운영', qty: 6 },
],
{ rowKey: 'id' },
);
// 위 마스터 그리드는 편집할 수 있고, 아래 요약 그리드는 같은 저장소를 읽기 전용으로 작게 보여 줘요(이름·수량만, 수량 내림차순).
// 요약 그리드는 마스터에서 고친 값을 다음 프레임에 그대로 받고, 저장소 구독으로 합계를 기록 줄에 알려 줘요.
export const options: GridOptions<Row> = {
density: 'normal', // 틀이 높이를 키우지 않게 고정(두 그리드 높이를 직접 정한다)
columns: [
{ id: 'name', title: '이름', editable: true, width: 120 },
{ id: 'team', title: '팀', editable: true, width: 90 },
{ id: 'qty', title: '수량', kind: 'number', editable: true, width: 80 },
],
rowsStore: store,
ariaLabel: '마스터',
};
let summary: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
let nextId = 100;
const total = (): number => store.getRows().reduce((sum, row) => sum + row.qty, 0);
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
summary = MaxGrid.createGrid(host, {
locale: 'ko',
columns: [
{ id: 'name', title: '이름', width: 120 },
{ id: 'qty', title: '수량', kind: 'number', width: 80 },
],
rowsStore: store,
ariaLabel: '요약',
});
summary.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe(() =>
log({ ko: `수량 합계: ${total()}`, en: `Total quantity: ${total()}` }),
);
log({
ko: `마스터에서 수량을 고치면 요약이 따라와요. 지금 합계는 ${total()}예요.`,
en: `Edit a quantity in the master and the summary follows. The total is ${total()} now.`,
});
return () => {
off();
summary?.destroy();
host?.remove();
summary = host = null;
};
}
export const actions = {
add: (api: GridApi<Row>) => {
api.addRows([{ id: nextId, name: `새 사람 ${nextId}`, team: '개발', qty: 5 }]);
nextId += 1;
},
double: (api: GridApi<Row>) => {
const row = api.getRows()[0];
if (row) api.setCellValue(row.id, 'qty', row.qty * 2);
},
remove: (api: GridApi<Row>) => {
const last = api.getRows().at(-1);
if (last) api.removeRows([last.id]);
},
};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="add">Add row</button>
<button type="button" id="double">Double first qty</button>
<button type="button" id="remove">Remove last row</button>
</div>
<div id="grid" style="height: 260px"></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('add').addEventListener('click', () => actions.add(api));
document.getElementById('double').addEventListener('click', () => actions.double(api));
document.getElementById('remove').addEventListener('click', () => actions.remove(api));const store = MaxGrid.createRowsStore([
{ id: 1, name: 'Liam Carter', team: 'Dev', qty: 4 },
{ id: 2, name: 'Emma Stone', team: 'Ops', qty: 9 },
{ id: 3, name: 'Noah Reed', team: 'Design', qty: 2 },
{ id: 4, name: 'Ava Brooks', team: 'Dev', qty: 12 },
{ id: 5, name: 'Mia Lopez', team: 'Ops', qty: 6 },
], { rowKey: 'id' });
// The top master grid is editable; the bottom summary grid shows the same store small and read-only (name and quantity only, by quantity descending).
// The summary receives master edits on the next frame, and a store subscription reports the total in the log.
export const options = {
density: 'normal', // keeps the frame from scaling heights (both grid heights are set here)
columns: [
{ id: 'name', title: 'Name', editable: true, width: 120 },
{ id: 'team', title: 'Team', editable: true, width: 90 },
{ id: 'qty', title: 'Qty', kind: 'number', editable: true, width: 80 },
],
rowsStore: store,
ariaLabel: 'Master',
};
let summary = null;
let host = null;
let nextId = 100;
const total = () => store.getRows().reduce((sum, row) => sum + row.qty, 0);
export function setup(api, log) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null)
first.after(host);
else
document.body.append(host);
summary = MaxGrid.createGrid(host, {
locale: 'en',
columns: [
{ id: 'name', title: 'Name', width: 120 },
{ id: 'qty', title: 'Qty', kind: 'number', width: 80 },
],
rowsStore: store,
ariaLabel: 'Summary',
});
summary.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe(() => log(`Total quantity: ${total()}`));
log(`Edit a quantity in the master and the summary follows. The total is ${total()} now.`);
return () => {
off();
summary?.destroy();
host?.remove();
summary = host = null;
};
}
export const actions = {
add: (api) => {
api.addRows([{ id: nextId, name: `New person ${nextId}`, team: 'Dev', qty: 5 }]);
nextId += 1;
},
double: (api) => {
const row = api.getRows()[0];
if (row)
api.setCellValue(row.id, 'qty', row.qty * 2);
},
remove: (api) => {
const last = api.getRows().at(-1);
if (last)
api.removeRows([last.id]);
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Master grid with a summary grid</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="add">Add row</button>
<button type="button" id="double">Double first qty</button>
<button type="button" id="remove">Remove last row</button>
</div>
<div id="grid" style="height: 260px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const store = MaxGrid.createRowsStore([
{ id: 1, name: 'Liam Carter', team: 'Dev', qty: 4 },
{ id: 2, name: 'Emma Stone', team: 'Ops', qty: 9 },
{ id: 3, name: 'Noah Reed', team: 'Design', qty: 2 },
{ id: 4, name: 'Ava Brooks', team: 'Dev', qty: 12 },
{ id: 5, name: 'Mia Lopez', team: 'Ops', qty: 6 },
], { rowKey: 'id' });
// The top master grid is editable; the bottom summary grid shows the same store small and read-only (name and quantity only, by quantity descending).
// The summary receives master edits on the next frame, and a store subscription reports the total in the log.
const options = {
density: 'normal', // keeps the frame from scaling heights (both grid heights are set here)
columns: [
{ id: 'name', title: 'Name', editable: true, width: 120 },
{ id: 'team', title: 'Team', editable: true, width: 90 },
{ id: 'qty', title: 'Qty', kind: 'number', editable: true, width: 80 },
],
rowsStore: store,
ariaLabel: 'Master',
};
let summary = null;
let host = null;
let nextId = 100;
const total = () => store.getRows().reduce((sum, row) => sum + row.qty, 0);
function setup(api, log) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null)
first.after(host);
else
document.body.append(host);
summary = MaxGrid.createGrid(host, {
locale: 'en',
columns: [
{ id: 'name', title: 'Name', width: 120 },
{ id: 'qty', title: 'Qty', kind: 'number', width: 80 },
],
rowsStore: store,
ariaLabel: 'Summary',
});
summary.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe(() => log(`Total quantity: ${total()}`));
log(`Edit a quantity in the master and the summary follows. The total is ${total()} now.`);
return () => {
off();
summary?.destroy();
host?.remove();
summary = host = null;
};
}
const actions = {
add: (api) => {
api.addRows([{ id: nextId, name: `New person ${nextId}`, team: 'Dev', qty: 5 }]);
nextId += 1;
},
double: (api) => {
const row = api.getRows()[0];
if (row)
api.setCellValue(row.id, 'qty', row.qty * 2);
},
remove: (api) => {
const last = api.getRows().at(-1);
if (last)
api.removeRows([last.id]);
},
};
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('add').addEventListener('click', () => actions.add(api));
document.getElementById('double').addEventListener('click', () => actions.double(api));
document.getElementById('remove').addEventListener('click', () => actions.remove(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">
<button
type="button"
disabled={!api}
onClick={() => api && actions.add(api)}
>Add row</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.double(api)}
>Double first qty</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.remove(api)}
>Remove last row</button>
</div>
<Grid style={{ height: 260 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions, RowsStore } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
name: string;
team: string;
qty: number;
}
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
createRowsStore(rows: Row[], options: { rowKey: 'id' }): RowsStore<Row>;
};
const store = MaxGrid.createRowsStore(
[
{ id: 1, name: 'Liam Carter', team: 'Dev', qty: 4 },
{ id: 2, name: 'Emma Stone', team: 'Ops', qty: 9 },
{ id: 3, name: 'Noah Reed', team: 'Design', qty: 2 },
{ id: 4, name: 'Ava Brooks', team: 'Dev', qty: 12 },
{ id: 5, name: 'Mia Lopez', team: 'Ops', qty: 6 },
],
{ rowKey: 'id' },
);
// The top master grid is editable; the bottom summary grid shows the same store small and read-only (name and quantity only, by quantity descending).
// The summary receives master edits on the next frame, and a store subscription reports the total in the log.
export const options: GridOptions<Row> = {
density: 'normal', // keeps the frame from scaling heights (both grid heights are set here)
columns: [
{ id: 'name', title: 'Name', editable: true, width: 120 },
{ id: 'team', title: 'Team', editable: true, width: 90 },
{ id: 'qty', title: 'Qty', kind: 'number', editable: true, width: 80 },
],
rowsStore: store,
ariaLabel: 'Master',
};
let summary: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
let nextId = 100;
const total = (): number => store.getRows().reduce((sum, row) => sum + row.qty, 0);
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
summary = MaxGrid.createGrid(host, {
locale: 'en',
columns: [
{ id: 'name', title: 'Name', width: 120 },
{ id: 'qty', title: 'Qty', kind: 'number', width: 80 },
],
rowsStore: store,
ariaLabel: 'Summary',
});
summary.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe(() => log(`Total quantity: ${total()}`));
log(`Edit a quantity in the master and the summary follows. The total is ${total()} now.`);
return () => {
off();
summary?.destroy();
host?.remove();
summary = host = null;
};
}
export const actions = {
add: (api: GridApi<Row>) => {
api.addRows([{ id: nextId, name: `New person ${nextId}`, team: 'Dev', qty: 5 }]);
nextId += 1;
},
double: (api: GridApi<Row>) => {
const row = api.getRows()[0];
if (row) api.setCellValue(row.id, 'qty', row.qty * 2);
},
remove: (api: GridApi<Row>) => {
const last = api.getRows().at(-1);
if (last) api.removeRows([last.id]);
},
};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.add(api)">Add row</button>
<button type="button" :disabled="!api" @click="api && actions.double(api)">Double first qty</button>
<button type="button" :disabled="!api" @click="api && actions.remove(api)">Remove last row</button>
</div>
<Grid style="height: 260px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions, RowsStore } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
name: string;
team: string;
qty: number;
}
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
createRowsStore(rows: Row[], options: { rowKey: 'id' }): RowsStore<Row>;
};
const store = MaxGrid.createRowsStore(
[
{ id: 1, name: 'Liam Carter', team: 'Dev', qty: 4 },
{ id: 2, name: 'Emma Stone', team: 'Ops', qty: 9 },
{ id: 3, name: 'Noah Reed', team: 'Design', qty: 2 },
{ id: 4, name: 'Ava Brooks', team: 'Dev', qty: 12 },
{ id: 5, name: 'Mia Lopez', team: 'Ops', qty: 6 },
],
{ rowKey: 'id' },
);
// The top master grid is editable; the bottom summary grid shows the same store small and read-only (name and quantity only, by quantity descending).
// The summary receives master edits on the next frame, and a store subscription reports the total in the log.
export const options: GridOptions<Row> = {
density: 'normal', // keeps the frame from scaling heights (both grid heights are set here)
columns: [
{ id: 'name', title: 'Name', editable: true, width: 120 },
{ id: 'team', title: 'Team', editable: true, width: 90 },
{ id: 'qty', title: 'Qty', kind: 'number', editable: true, width: 80 },
],
rowsStore: store,
ariaLabel: 'Master',
};
let summary: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
let nextId = 100;
const total = (): number => store.getRows().reduce((sum, row) => sum + row.qty, 0);
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
summary = MaxGrid.createGrid(host, {
locale: 'en',
columns: [
{ id: 'name', title: 'Name', width: 120 },
{ id: 'qty', title: 'Qty', kind: 'number', width: 80 },
],
rowsStore: store,
ariaLabel: 'Summary',
});
summary.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe(() => log(`Total quantity: ${total()}`));
log(`Edit a quantity in the master and the summary follows. The total is ${total()} now.`);
return () => {
off();
summary?.destroy();
host?.remove();
summary = host = null;
};
}
export const actions = {
add: (api: GridApi<Row>) => {
api.addRows([{ id: nextId, name: `New person ${nextId}`, team: 'Dev', qty: 5 }]);
nextId += 1;
},
double: (api: GridApi<Row>) => {
const row = api.getRows()[0];
if (row) api.setCellValue(row.id, 'qty', row.qty * 2);
},
remove: (api: GridApi<Row>) => {
const last = api.getRows().at(-1);
if (last) api.removeRows([last.id]);
},
};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.add(api)"
>Add row</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.double(api)"
>Double first qty</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.remove(api)"
>Remove last row</button>
</div>
<mx-grid style="height: 260px" [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, RowsStore } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
name: string;
team: string;
qty: number;
}
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
createRowsStore(rows: Row[], options: { rowKey: 'id' }): RowsStore<Row>;
};
const store = MaxGrid.createRowsStore(
[
{ id: 1, name: 'Liam Carter', team: 'Dev', qty: 4 },
{ id: 2, name: 'Emma Stone', team: 'Ops', qty: 9 },
{ id: 3, name: 'Noah Reed', team: 'Design', qty: 2 },
{ id: 4, name: 'Ava Brooks', team: 'Dev', qty: 12 },
{ id: 5, name: 'Mia Lopez', team: 'Ops', qty: 6 },
],
{ rowKey: 'id' },
);
// The top master grid is editable; the bottom summary grid shows the same store small and read-only (name and quantity only, by quantity descending).
// The summary receives master edits on the next frame, and a store subscription reports the total in the log.
export const options: GridOptions<Row> = {
density: 'normal', // keeps the frame from scaling heights (both grid heights are set here)
columns: [
{ id: 'name', title: 'Name', editable: true, width: 120 },
{ id: 'team', title: 'Team', editable: true, width: 90 },
{ id: 'qty', title: 'Qty', kind: 'number', editable: true, width: 80 },
],
rowsStore: store,
ariaLabel: 'Master',
};
let summary: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
let nextId = 100;
const total = (): number => store.getRows().reduce((sum, row) => sum + row.qty, 0);
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
summary = MaxGrid.createGrid(host, {
locale: 'en',
columns: [
{ id: 'name', title: 'Name', width: 120 },
{ id: 'qty', title: 'Qty', kind: 'number', width: 80 },
],
rowsStore: store,
ariaLabel: 'Summary',
});
summary.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe(() => log(`Total quantity: ${total()}`));
log(`Edit a quantity in the master and the summary follows. The total is ${total()} now.`);
return () => {
off();
summary?.destroy();
host?.remove();
summary = host = null;
};
}
export const actions = {
add: (api: GridApi<Row>) => {
api.addRows([{ id: nextId, name: `New person ${nextId}`, team: 'Dev', qty: 5 }]);
nextId += 1;
},
double: (api: GridApi<Row>) => {
const row = api.getRows()[0];
if (row) api.setCellValue(row.id, 'qty', row.qty * 2);
},
remove: (api: GridApi<Row>) => {
const last = api.getRows().at(-1);
if (last) api.removeRows([last.id]);
},
};