저장 시점 만들고 돌아가기Create and restore save points
createSavePoint()는 지금 행의 깊은 복사본을 메모리에 보관하고, restoreSavePoint(id)는 그 모습으로 돌아가요. 최대 20개이고 되살리면 변경 추적과 되돌리기 기록이 비워져요.createSavePoint() keeps a deep copy of the current rows in memory and restoreSavePoint(id) goes back to it. At most 20 are kept, and a restore clears change tracking and the undo history.
코드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="create">시점 만들기</button>
<button type="button" id="mess">고치고 지우기</button>
<button type="button" id="restore">최근 시점 되살리기</button>
<button type="button" id="clear">시점 모두 지우기</button>
</div>
<div id="grid" style="height: 380px"></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('create').addEventListener('click', () => actions.create(api));
document.getElementById('mess').addEventListener('click', () => actions.mess(api));
document.getElementById('restore').addEventListener('click', () => actions.restore(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));const en = document.documentElement.dataset.lang === 'en';
const names = en
? ['Alex', 'Blair', 'Casey', 'Drew', 'Emery', 'Finn']
: ['김서연', '이준호', '박민지', '최도윤', '정하은', '한지우'];
export const options = {
columns: [
{ id: 'name', title: en ? 'Name' : '이름', flex: 2, minWidth: 120, editable: true },
{
id: 'score',
title: en ? 'Score' : '점수',
kind: 'number',
flex: 1,
minWidth: 90,
editable: true,
},
{ id: 'note', title: en ? 'Note' : '메모', flex: 2, minWidth: 120, editable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
score: 60 + i * 5,
note: en ? 'Edit me' : '고쳐 보세요',
})),
rowKey: 'id',
saveMode: 'batch',
};
let say = () => { };
const MAX = 20;
const time = (at) => new Date(at).toLocaleTimeString(en ? 'en-US' : 'ko-KR', { hour12: false });
/** 저장 시점 목록(이름·행 수·만든 시각)과 상한을 그리드 위쪽 안내 띠에 계속 보여 줘요. */
function show(api) {
const points = api.listSavePoints();
const items = points
.map((p) => `${p.name} (${p.rowCount}${en ? ' rows' : '행'}, ${time(p.createdAt)})`)
.join(' · ');
const head = en
? `${points.length} of ${MAX} save points`
: `저장 시점 ${points.length}/${MAX}개`;
api.showNotice(items === '' ? `${head}: ${en ? 'none yet' : '아직 없어요'}` : `${head}: ${items}`);
}
export function setup(api, log) {
say = log;
show(api);
const off = api.on('savePointRestored', (e) => say({ ko: `되살렸어요: ${e.name}`, en: `Restored: ${e.name}` }));
return () => {
off();
api.clearNotice();
};
}
const count = (n) => ({
ko: `저장 시점 ${n}개`,
en: `${n} save ${n === 1 ? 'point' : 'points'}`,
});
export const actions = {
// 지금 모습을 보관해요. 이름을 생략하면 "저장 시점 N"이에요.
create: (api) => {
const made = api.createSavePoint();
show(api);
const n = api.listSavePoints().length;
const c = count(n);
say({ ko: `${c.ko}: ${made.name}`, en: `${c.en}: ${made.name}` });
},
// 값을 고치고 줄을 지워 봐요.
mess: (api) => {
api.setCellValue(1, 'score', 100);
api.removeRows([2]);
say({ ko: '1번 점수를 고치고 2번 행을 지웠어요', en: 'Edited score 1 and removed row 2' });
},
// 가장 최근 시점으로 되살려요. 변경 추적과 되돌리기 기록도 비워져요.
restore: (api) => {
const last = api.listSavePoints().at(-1);
if (!last)
return say({ ko: '먼저 저장 시점을 만드세요', en: 'Create a save point first' });
api.restoreSavePoint(last.id);
},
clear: (api) => {
api.clearSavePoints();
show(api);
say({ ko: '저장 시점을 모두 지웠어요', en: 'Cleared all save points' });
},
};<!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="create">시점 만들기</button>
<button type="button" id="mess">고치고 지우기</button>
<button type="button" id="restore">최근 시점 되살리기</button>
<button type="button" id="clear">시점 모두 지우기</button>
</div>
<div id="grid" style="height: 380px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const en = document.documentElement.dataset.lang === 'en';
const names = en
? ['Alex', 'Blair', 'Casey', 'Drew', 'Emery', 'Finn']
: ['김서연', '이준호', '박민지', '최도윤', '정하은', '한지우'];
const options = {
columns: [
{ id: 'name', title: en ? 'Name' : '이름', flex: 2, minWidth: 120, editable: true },
{
id: 'score',
title: en ? 'Score' : '점수',
kind: 'number',
flex: 1,
minWidth: 90,
editable: true,
},
{ id: 'note', title: en ? 'Note' : '메모', flex: 2, minWidth: 120, editable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
score: 60 + i * 5,
note: en ? 'Edit me' : '고쳐 보세요',
})),
rowKey: 'id',
saveMode: 'batch',
};
let say = () => { };
const MAX = 20;
const time = (at) => new Date(at).toLocaleTimeString(en ? 'en-US' : 'ko-KR', { hour12: false });
/** 저장 시점 목록(이름·행 수·만든 시각)과 상한을 그리드 위쪽 안내 띠에 계속 보여 줘요. */
function show(api) {
const points = api.listSavePoints();
const items = points
.map((p) => `${p.name} (${p.rowCount}${en ? ' rows' : '행'}, ${time(p.createdAt)})`)
.join(' · ');
const head = en
? `${points.length} of ${MAX} save points`
: `저장 시점 ${points.length}/${MAX}개`;
api.showNotice(items === '' ? `${head}: ${en ? 'none yet' : '아직 없어요'}` : `${head}: ${items}`);
}
function setup(api, log) {
say = log;
show(api);
const off = api.on('savePointRestored', (e) => say({ ko: `되살렸어요: ${e.name}`, en: `Restored: ${e.name}` }));
return () => {
off();
api.clearNotice();
};
}
const count = (n) => ({
ko: `저장 시점 ${n}개`,
en: `${n} save ${n === 1 ? 'point' : 'points'}`,
});
const actions = {
// 지금 모습을 보관해요. 이름을 생략하면 "저장 시점 N"이에요.
create: (api) => {
const made = api.createSavePoint();
show(api);
const n = api.listSavePoints().length;
const c = count(n);
say({ ko: `${c.ko}: ${made.name}`, en: `${c.en}: ${made.name}` });
},
// 값을 고치고 줄을 지워 봐요.
mess: (api) => {
api.setCellValue(1, 'score', 100);
api.removeRows([2]);
say({ ko: '1번 점수를 고치고 2번 행을 지웠어요', en: 'Edited score 1 and removed row 2' });
},
// 가장 최근 시점으로 되살려요. 변경 추적과 되돌리기 기록도 비워져요.
restore: (api) => {
const last = api.listSavePoints().at(-1);
if (!last)
return say({ ko: '먼저 저장 시점을 만드세요', en: 'Create a save point first' });
api.restoreSavePoint(last.id);
},
clear: (api) => {
api.clearSavePoints();
show(api);
say({ ko: '저장 시점을 모두 지웠어요', en: 'Cleared all save points' });
},
};
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('create').addEventListener('click', () => actions.create(api));
document.getElementById('mess').addEventListener('click', () => actions.mess(api));
document.getElementById('restore').addEventListener('click', () => actions.restore(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(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.create(api)}
>시점 만들기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.mess(api)}
>고치고 지우기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.restore(api)}
>최근 시점 되살리기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.clear(api)}
>시점 모두 지우기</button>
</div>
<Grid style={{ height: 380 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
score: number;
note: string;
}
const en = document.documentElement.dataset.lang === 'en';
const names = en
? ['Alex', 'Blair', 'Casey', 'Drew', 'Emery', 'Finn']
: ['김서연', '이준호', '박민지', '최도윤', '정하은', '한지우'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: en ? 'Name' : '이름', flex: 2, minWidth: 120, editable: true },
{
id: 'score',
title: en ? 'Score' : '점수',
kind: 'number',
flex: 1,
minWidth: 90,
editable: true,
},
{ id: 'note', title: en ? 'Note' : '메모', flex: 2, minWidth: 120, editable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
score: 60 + i * 5,
note: en ? 'Edit me' : '고쳐 보세요',
})),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
const MAX = 20;
const time = (at: number): string =>
new Date(at).toLocaleTimeString(en ? 'en-US' : 'ko-KR', { hour12: false });
/** 저장 시점 목록(이름·행 수·만든 시각)과 상한을 그리드 위쪽 안내 띠에 계속 보여 줘요. */
function show(api: GridApi<Row>): void {
const points = api.listSavePoints();
const items = points
.map((p) => `${p.name} (${p.rowCount}${en ? ' rows' : '행'}, ${time(p.createdAt)})`)
.join(' · ');
const head = en
? `${points.length} of ${MAX} save points`
: `저장 시점 ${points.length}/${MAX}개`;
api.showNotice(
items === '' ? `${head}: ${en ? 'none yet' : '아직 없어요'}` : `${head}: ${items}`,
);
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
show(api);
const off = api.on('savePointRestored', (e) =>
say({ ko: `되살렸어요: ${e.name}`, en: `Restored: ${e.name}` }),
);
return () => {
off();
api.clearNotice();
};
}
const count = (n: number): LogText => ({
ko: `저장 시점 ${n}개`,
en: `${n} save ${n === 1 ? 'point' : 'points'}`,
});
export const actions = {
// 지금 모습을 보관해요. 이름을 생략하면 "저장 시점 N"이에요.
create: (api: GridApi<Row>) => {
const made = api.createSavePoint();
show(api);
const n = api.listSavePoints().length;
const c = count(n) as { ko: string; en: string };
say({ ko: `${c.ko}: ${made.name}`, en: `${c.en}: ${made.name}` });
},
// 값을 고치고 줄을 지워 봐요.
mess: (api: GridApi<Row>) => {
api.setCellValue(1, 'score', 100);
api.removeRows([2]);
say({ ko: '1번 점수를 고치고 2번 행을 지웠어요', en: 'Edited score 1 and removed row 2' });
},
// 가장 최근 시점으로 되살려요. 변경 추적과 되돌리기 기록도 비워져요.
restore: (api: GridApi<Row>) => {
const last = api.listSavePoints().at(-1);
if (!last) return say({ ko: '먼저 저장 시점을 만드세요', en: 'Create a save point first' });
api.restoreSavePoint(last.id);
},
clear: (api: GridApi<Row>) => {
api.clearSavePoints();
show(api);
say({ ko: '저장 시점을 모두 지웠어요', en: 'Cleared all save points' });
},
};버튼을 누르면 이 예제의 코드가 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.create(api)">시점 만들기</button>
<button type="button" :disabled="!api" @click="api && actions.mess(api)">고치고 지우기</button>
<button type="button" :disabled="!api" @click="api && actions.restore(api)">최근 시점 되살리기</button>
<button type="button" :disabled="!api" @click="api && actions.clear(api)">시점 모두 지우기</button>
</div>
<Grid style="height: 380px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
score: number;
note: string;
}
const en = document.documentElement.dataset.lang === 'en';
const names = en
? ['Alex', 'Blair', 'Casey', 'Drew', 'Emery', 'Finn']
: ['김서연', '이준호', '박민지', '최도윤', '정하은', '한지우'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: en ? 'Name' : '이름', flex: 2, minWidth: 120, editable: true },
{
id: 'score',
title: en ? 'Score' : '점수',
kind: 'number',
flex: 1,
minWidth: 90,
editable: true,
},
{ id: 'note', title: en ? 'Note' : '메모', flex: 2, minWidth: 120, editable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
score: 60 + i * 5,
note: en ? 'Edit me' : '고쳐 보세요',
})),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
const MAX = 20;
const time = (at: number): string =>
new Date(at).toLocaleTimeString(en ? 'en-US' : 'ko-KR', { hour12: false });
/** 저장 시점 목록(이름·행 수·만든 시각)과 상한을 그리드 위쪽 안내 띠에 계속 보여 줘요. */
function show(api: GridApi<Row>): void {
const points = api.listSavePoints();
const items = points
.map((p) => `${p.name} (${p.rowCount}${en ? ' rows' : '행'}, ${time(p.createdAt)})`)
.join(' · ');
const head = en
? `${points.length} of ${MAX} save points`
: `저장 시점 ${points.length}/${MAX}개`;
api.showNotice(
items === '' ? `${head}: ${en ? 'none yet' : '아직 없어요'}` : `${head}: ${items}`,
);
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
show(api);
const off = api.on('savePointRestored', (e) =>
say({ ko: `되살렸어요: ${e.name}`, en: `Restored: ${e.name}` }),
);
return () => {
off();
api.clearNotice();
};
}
const count = (n: number): LogText => ({
ko: `저장 시점 ${n}개`,
en: `${n} save ${n === 1 ? 'point' : 'points'}`,
});
export const actions = {
// 지금 모습을 보관해요. 이름을 생략하면 "저장 시점 N"이에요.
create: (api: GridApi<Row>) => {
const made = api.createSavePoint();
show(api);
const n = api.listSavePoints().length;
const c = count(n) as { ko: string; en: string };
say({ ko: `${c.ko}: ${made.name}`, en: `${c.en}: ${made.name}` });
},
// 값을 고치고 줄을 지워 봐요.
mess: (api: GridApi<Row>) => {
api.setCellValue(1, 'score', 100);
api.removeRows([2]);
say({ ko: '1번 점수를 고치고 2번 행을 지웠어요', en: 'Edited score 1 and removed row 2' });
},
// 가장 최근 시점으로 되살려요. 변경 추적과 되돌리기 기록도 비워져요.
restore: (api: GridApi<Row>) => {
const last = api.listSavePoints().at(-1);
if (!last) return say({ ko: '먼저 저장 시점을 만드세요', en: 'Create a save point first' });
api.restoreSavePoint(last.id);
},
clear: (api: GridApi<Row>) => {
api.clearSavePoints();
show(api);
say({ ko: '저장 시점을 모두 지웠어요', en: 'Cleared all save points' });
},
};버튼을 누르면 이 예제의 코드가 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.create(api)"
>시점 만들기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.mess(api)"
>고치고 지우기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.restore(api)"
>최근 시점 되살리기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.clear(api)"
>시점 모두 지우기</button>
</div>
<mx-grid style="height: 380px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
score: number;
note: string;
}
const en = document.documentElement.dataset.lang === 'en';
const names = en
? ['Alex', 'Blair', 'Casey', 'Drew', 'Emery', 'Finn']
: ['김서연', '이준호', '박민지', '최도윤', '정하은', '한지우'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: en ? 'Name' : '이름', flex: 2, minWidth: 120, editable: true },
{
id: 'score',
title: en ? 'Score' : '점수',
kind: 'number',
flex: 1,
minWidth: 90,
editable: true,
},
{ id: 'note', title: en ? 'Note' : '메모', flex: 2, minWidth: 120, editable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
score: 60 + i * 5,
note: en ? 'Edit me' : '고쳐 보세요',
})),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
const MAX = 20;
const time = (at: number): string =>
new Date(at).toLocaleTimeString(en ? 'en-US' : 'ko-KR', { hour12: false });
/** 저장 시점 목록(이름·행 수·만든 시각)과 상한을 그리드 위쪽 안내 띠에 계속 보여 줘요. */
function show(api: GridApi<Row>): void {
const points = api.listSavePoints();
const items = points
.map((p) => `${p.name} (${p.rowCount}${en ? ' rows' : '행'}, ${time(p.createdAt)})`)
.join(' · ');
const head = en
? `${points.length} of ${MAX} save points`
: `저장 시점 ${points.length}/${MAX}개`;
api.showNotice(
items === '' ? `${head}: ${en ? 'none yet' : '아직 없어요'}` : `${head}: ${items}`,
);
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
show(api);
const off = api.on('savePointRestored', (e) =>
say({ ko: `되살렸어요: ${e.name}`, en: `Restored: ${e.name}` }),
);
return () => {
off();
api.clearNotice();
};
}
const count = (n: number): LogText => ({
ko: `저장 시점 ${n}개`,
en: `${n} save ${n === 1 ? 'point' : 'points'}`,
});
export const actions = {
// 지금 모습을 보관해요. 이름을 생략하면 "저장 시점 N"이에요.
create: (api: GridApi<Row>) => {
const made = api.createSavePoint();
show(api);
const n = api.listSavePoints().length;
const c = count(n) as { ko: string; en: string };
say({ ko: `${c.ko}: ${made.name}`, en: `${c.en}: ${made.name}` });
},
// 값을 고치고 줄을 지워 봐요.
mess: (api: GridApi<Row>) => {
api.setCellValue(1, 'score', 100);
api.removeRows([2]);
say({ ko: '1번 점수를 고치고 2번 행을 지웠어요', en: 'Edited score 1 and removed row 2' });
},
// 가장 최근 시점으로 되살려요. 변경 추적과 되돌리기 기록도 비워져요.
restore: (api: GridApi<Row>) => {
const last = api.listSavePoints().at(-1);
if (!last) return say({ ko: '먼저 저장 시점을 만드세요', en: 'Create a save point first' });
api.restoreSavePoint(last.id);
},
clear: (api: GridApi<Row>) => {
api.clearSavePoints();
show(api);
say({ ko: '저장 시점을 모두 지웠어요', en: 'Cleared all save points' });
},
};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="create">Create point</button>
<button type="button" id="mess">Mess up</button>
<button type="button" id="restore">Restore latest</button>
<button type="button" id="clear">Clear points</button>
</div>
<div id="grid" style="height: 380px"></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('create').addEventListener('click', () => actions.create(api));
document.getElementById('mess').addEventListener('click', () => actions.mess(api));
document.getElementById('restore').addEventListener('click', () => actions.restore(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));const names = ['Liam Carter', 'Emma Stone', 'Noah Kim', 'Olivia Reed', 'Ava Brooks', 'Mason Lee'];
export const options = {
columns: [
{ id: 'name', title: 'Name', flex: 2, minWidth: 120, editable: true },
{
id: 'score',
title: 'Score',
kind: 'number',
flex: 1,
minWidth: 90,
editable: true,
},
{ id: 'note', title: 'Note', flex: 2, minWidth: 120, editable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
score: 60 + i * 5,
note: 'Edit me',
})),
rowKey: 'id',
saveMode: 'batch',
};
let say = () => { };
const MAX = 20;
const time = (at) => new Date(at).toLocaleTimeString('en-US', { hour12: false });
/** Keeps the list of save points (name, row count, creation time) and the limit visible in the notice strip above the grid. */
function show(api) {
const points = api.listSavePoints();
const items = points
.map((p) => `${p.name} (${p.rowCount} rows, ${time(p.createdAt)})`)
.join(' · ');
const head = `${points.length} of ${MAX} save points`;
api.showNotice(items === '' ? `${head}: none yet` : `${head}: ${items}`);
}
export function setup(api, log) {
say = log;
show(api);
const off = api.on('savePointRestored', (e) => say(`Restored: ${e.name}`));
return () => {
off();
api.clearNotice();
};
}
const count = (n) => `${n} save ${n === 1 ? 'point' : 'points'}`;
export const actions = {
// Keeps the current look. Without a name it is "Save point N".
create: (api) => {
const made = api.createSavePoint();
show(api);
const n = api.listSavePoints().length;
say(`${count(n)}: ${made.name}`);
},
// Edit a value and delete a row.
mess: (api) => {
api.setCellValue(1, 'score', 100);
api.removeRows([2]);
say('Edited score 1 and removed row 2');
},
// Restore the most recent save point. Change tracking and the undo history are cleared too.
restore: (api) => {
const last = api.listSavePoints().at(-1);
if (!last)
return say('Create a save point first');
api.restoreSavePoint(last.id);
},
clear: (api) => {
api.clearSavePoints();
show(api);
say('Cleared all save points');
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Create and restore save points</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="create">Create point</button>
<button type="button" id="mess">Mess up</button>
<button type="button" id="restore">Restore latest</button>
<button type="button" id="clear">Clear points</button>
</div>
<div id="grid" style="height: 380px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const names = ['Liam Carter', 'Emma Stone', 'Noah Kim', 'Olivia Reed', 'Ava Brooks', 'Mason Lee'];
const options = {
columns: [
{ id: 'name', title: 'Name', flex: 2, minWidth: 120, editable: true },
{
id: 'score',
title: 'Score',
kind: 'number',
flex: 1,
minWidth: 90,
editable: true,
},
{ id: 'note', title: 'Note', flex: 2, minWidth: 120, editable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
score: 60 + i * 5,
note: 'Edit me',
})),
rowKey: 'id',
saveMode: 'batch',
};
let say = () => { };
const MAX = 20;
const time = (at) => new Date(at).toLocaleTimeString('en-US', { hour12: false });
/** Keeps the list of save points (name, row count, creation time) and the limit visible in the notice strip above the grid. */
function show(api) {
const points = api.listSavePoints();
const items = points
.map((p) => `${p.name} (${p.rowCount} rows, ${time(p.createdAt)})`)
.join(' · ');
const head = `${points.length} of ${MAX} save points`;
api.showNotice(items === '' ? `${head}: none yet` : `${head}: ${items}`);
}
function setup(api, log) {
say = log;
show(api);
const off = api.on('savePointRestored', (e) => say(`Restored: ${e.name}`));
return () => {
off();
api.clearNotice();
};
}
const count = (n) => `${n} save ${n === 1 ? 'point' : 'points'}`;
const actions = {
// Keeps the current look. Without a name it is "Save point N".
create: (api) => {
const made = api.createSavePoint();
show(api);
const n = api.listSavePoints().length;
say(`${count(n)}: ${made.name}`);
},
// Edit a value and delete a row.
mess: (api) => {
api.setCellValue(1, 'score', 100);
api.removeRows([2]);
say('Edited score 1 and removed row 2');
},
// Restore the most recent save point. Change tracking and the undo history are cleared too.
restore: (api) => {
const last = api.listSavePoints().at(-1);
if (!last)
return say('Create a save point first');
api.restoreSavePoint(last.id);
},
clear: (api) => {
api.clearSavePoints();
show(api);
say('Cleared all save points');
},
};
options.ariaLabel = "Create and restore save points";
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('create').addEventListener('click', () => actions.create(api));
document.getElementById('mess').addEventListener('click', () => actions.mess(api));
document.getElementById('restore').addEventListener('click', () => actions.restore(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(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.create(api)}
>Create point</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.mess(api)}
>Mess up</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.restore(api)}
>Restore latest</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.clear(api)}
>Clear points</button>
</div>
<Grid style={{ height: 380 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
name: string;
score: number;
note: string;
}
const names = ['Liam Carter', 'Emma Stone', 'Noah Kim', 'Olivia Reed', 'Ava Brooks', 'Mason Lee'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Name', flex: 2, minWidth: 120, editable: true },
{
id: 'score',
title: 'Score',
kind: 'number',
flex: 1,
minWidth: 90,
editable: true,
},
{ id: 'note', title: 'Note', flex: 2, minWidth: 120, editable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
score: 60 + i * 5,
note: 'Edit me',
})),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
const MAX = 20;
const time = (at: number): string => new Date(at).toLocaleTimeString('en-US', { hour12: false });
/** Keeps the list of save points (name, row count, creation time) and the limit visible in the notice strip above the grid. */
function show(api: GridApi<Row>): void {
const points = api.listSavePoints();
const items = points
.map((p) => `${p.name} (${p.rowCount} rows, ${time(p.createdAt)})`)
.join(' · ');
const head = `${points.length} of ${MAX} save points`;
api.showNotice(items === '' ? `${head}: none yet` : `${head}: ${items}`);
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
show(api);
const off = api.on('savePointRestored', (e) => say(`Restored: ${e.name}`));
return () => {
off();
api.clearNotice();
};
}
const count = (n: number): string => `${n} save ${n === 1 ? 'point' : 'points'}`;
export const actions = {
// Keeps the current look. Without a name it is "Save point N".
create: (api: GridApi<Row>) => {
const made = api.createSavePoint();
show(api);
const n = api.listSavePoints().length;
say(`${count(n)}: ${made.name}`);
},
// Edit a value and delete a row.
mess: (api: GridApi<Row>) => {
api.setCellValue(1, 'score', 100);
api.removeRows([2]);
say('Edited score 1 and removed row 2');
},
// Restore the most recent save point. Change tracking and the undo history are cleared too.
restore: (api: GridApi<Row>) => {
const last = api.listSavePoints().at(-1);
if (!last) return say('Create a save point first');
api.restoreSavePoint(last.id);
},
clear: (api: GridApi<Row>) => {
api.clearSavePoints();
show(api);
say('Cleared all save points');
},
};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.create(api)">Create point</button>
<button type="button" :disabled="!api" @click="api && actions.mess(api)">Mess up</button>
<button type="button" :disabled="!api" @click="api && actions.restore(api)">Restore latest</button>
<button type="button" :disabled="!api" @click="api && actions.clear(api)">Clear points</button>
</div>
<Grid style="height: 380px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
name: string;
score: number;
note: string;
}
const names = ['Liam Carter', 'Emma Stone', 'Noah Kim', 'Olivia Reed', 'Ava Brooks', 'Mason Lee'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Name', flex: 2, minWidth: 120, editable: true },
{
id: 'score',
title: 'Score',
kind: 'number',
flex: 1,
minWidth: 90,
editable: true,
},
{ id: 'note', title: 'Note', flex: 2, minWidth: 120, editable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
score: 60 + i * 5,
note: 'Edit me',
})),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
const MAX = 20;
const time = (at: number): string => new Date(at).toLocaleTimeString('en-US', { hour12: false });
/** Keeps the list of save points (name, row count, creation time) and the limit visible in the notice strip above the grid. */
function show(api: GridApi<Row>): void {
const points = api.listSavePoints();
const items = points
.map((p) => `${p.name} (${p.rowCount} rows, ${time(p.createdAt)})`)
.join(' · ');
const head = `${points.length} of ${MAX} save points`;
api.showNotice(items === '' ? `${head}: none yet` : `${head}: ${items}`);
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
show(api);
const off = api.on('savePointRestored', (e) => say(`Restored: ${e.name}`));
return () => {
off();
api.clearNotice();
};
}
const count = (n: number): string => `${n} save ${n === 1 ? 'point' : 'points'}`;
export const actions = {
// Keeps the current look. Without a name it is "Save point N".
create: (api: GridApi<Row>) => {
const made = api.createSavePoint();
show(api);
const n = api.listSavePoints().length;
say(`${count(n)}: ${made.name}`);
},
// Edit a value and delete a row.
mess: (api: GridApi<Row>) => {
api.setCellValue(1, 'score', 100);
api.removeRows([2]);
say('Edited score 1 and removed row 2');
},
// Restore the most recent save point. Change tracking and the undo history are cleared too.
restore: (api: GridApi<Row>) => {
const last = api.listSavePoints().at(-1);
if (!last) return say('Create a save point first');
api.restoreSavePoint(last.id);
},
clear: (api: GridApi<Row>) => {
api.clearSavePoints();
show(api);
say('Cleared all save points');
},
};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.create(api)"
>Create point</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.mess(api)"
>Mess up</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.restore(api)"
>Restore latest</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.clear(api)"
>Clear points</button>
</div>
<mx-grid style="height: 380px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
name: string;
score: number;
note: string;
}
const names = ['Liam Carter', 'Emma Stone', 'Noah Kim', 'Olivia Reed', 'Ava Brooks', 'Mason Lee'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Name', flex: 2, minWidth: 120, editable: true },
{
id: 'score',
title: 'Score',
kind: 'number',
flex: 1,
minWidth: 90,
editable: true,
},
{ id: 'note', title: 'Note', flex: 2, minWidth: 120, editable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
score: 60 + i * 5,
note: 'Edit me',
})),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
const MAX = 20;
const time = (at: number): string => new Date(at).toLocaleTimeString('en-US', { hour12: false });
/** Keeps the list of save points (name, row count, creation time) and the limit visible in the notice strip above the grid. */
function show(api: GridApi<Row>): void {
const points = api.listSavePoints();
const items = points
.map((p) => `${p.name} (${p.rowCount} rows, ${time(p.createdAt)})`)
.join(' · ');
const head = `${points.length} of ${MAX} save points`;
api.showNotice(items === '' ? `${head}: none yet` : `${head}: ${items}`);
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
show(api);
const off = api.on('savePointRestored', (e) => say(`Restored: ${e.name}`));
return () => {
off();
api.clearNotice();
};
}
const count = (n: number): string => `${n} save ${n === 1 ? 'point' : 'points'}`;
export const actions = {
// Keeps the current look. Without a name it is "Save point N".
create: (api: GridApi<Row>) => {
const made = api.createSavePoint();
show(api);
const n = api.listSavePoints().length;
say(`${count(n)}: ${made.name}`);
},
// Edit a value and delete a row.
mess: (api: GridApi<Row>) => {
api.setCellValue(1, 'score', 100);
api.removeRows([2]);
say('Edited score 1 and removed row 2');
},
// Restore the most recent save point. Change tracking and the undo history are cleared too.
restore: (api: GridApi<Row>) => {
const last = api.listSavePoints().at(-1);
if (!last) return say('Create a save point first');
api.restoreSavePoint(last.id);
},
clear: (api: GridApi<Row>) => {
api.clearSavePoints();
show(api);
say('Cleared all save points');
},
};