해 보고 마음에 안 들면 되돌리기Try something and roll back
해 보기를 시작할 때 저장 시점을 만들고, 마음에 안 들면 restoreSavePoint(id, { keepView: true })로 되돌려요. keepView: true는 정렬·선택·쪽을 그대로 둬요. 마음에 들면 deleteSavePoint로 시점만 지워요.Create a save point when a trial starts and go back with restoreSavePoint(id, { keepView: true }) if you do not like it. keepView: true keeps sort, selection and page. If you like it, deleteSavePoint just drops the point.
코드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="sort">수량으로 정렬</button>
<button type="button" id="start">해 보기 시작</button>
<button type="button" id="rollback">되돌리기</button>
<button type="button" id="keep">그대로 두기</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('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('start').addEventListener('click', () => actions.start(api));
document.getElementById('rollback').addEventListener('click', () => actions.rollback(api));
document.getElementById('keep').addEventListener('click', () => actions.keep(api));const en = document.documentElement.dataset.lang === 'en';
const items = en
? ['Pen', 'Notebook', 'Stapler', 'Tape', 'Ruler', 'Eraser']
: ['펜', '노트', '스테이플러', '테이프', '자', '지우개'];
export const options = {
columns: [
{ id: 'item', title: en ? 'Item' : '품목', width: 130, sortable: true },
{
id: 'qty',
title: en ? 'Qty' : '수량',
kind: 'number',
width: 90,
sortable: true,
editable: true,
},
],
rows: items.map((item, i) => ({ id: i + 1, item, qty: 10 + i * 5 })),
rowKey: 'id',
saveMode: 'batch',
};
let say = () => { };
let trial = null;
let trialId = 100;
export function setup(_api, log) {
say = log;
}
export const actions = {
// "해 보기" 시작: 지금 모습을 보관해 두고 마음껏 바꿔 봐요.
start: (api) => {
if (trial !== null)
api.deleteSavePoint(trial); // 다시 누르면 이전 시점을 새 시점으로 바꿔요
trial = api.createSavePoint(en ? 'Before trying' : '해 보기 전').id;
api.setCellValue(1, 'qty', 999);
api.addRows([{ id: (trialId += 1), item: en ? 'Trial item' : '시험 품목', qty: 1 }]);
say({
ko: '해 보는 중이에요: 수량을 고치고 품목을 더했어요',
en: 'Trying: changed a quantity and added an item',
});
},
// 마음에 안 들면 보관한 모습으로. keepView: true라서 정렬·선택은 그대로예요.
rollback: (api) => {
if (trial === null)
return say({ ko: '먼저 해 보기를 시작하세요', en: 'Start a trial first' });
api.restoreSavePoint(trial, { keepView: true });
say({
ko: `되돌렸어요. 행 ${api.getRowCount()}개, 변경 ${api.getChanges().length}건`,
en: `Rolled back. ${api.getRowCount()} rows, ${api.getChanges().length} changes`,
});
},
// 마음에 들면 시점을 지우고 그대로 둬요.
keep: (api) => {
if (trial !== null)
api.deleteSavePoint(trial);
trial = null;
say({ ko: '그대로 두었어요. 저장 시점을 지웠어요', en: 'Kept it. The save point was deleted' });
},
sort: (api) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
};<!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="sort">수량으로 정렬</button>
<button type="button" id="start">해 보기 시작</button>
<button type="button" id="rollback">되돌리기</button>
<button type="button" id="keep">그대로 두기</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 items = en
? ['Pen', 'Notebook', 'Stapler', 'Tape', 'Ruler', 'Eraser']
: ['펜', '노트', '스테이플러', '테이프', '자', '지우개'];
const options = {
columns: [
{ id: 'item', title: en ? 'Item' : '품목', width: 130, sortable: true },
{
id: 'qty',
title: en ? 'Qty' : '수량',
kind: 'number',
width: 90,
sortable: true,
editable: true,
},
],
rows: items.map((item, i) => ({ id: i + 1, item, qty: 10 + i * 5 })),
rowKey: 'id',
saveMode: 'batch',
};
let say = () => { };
let trial = null;
let trialId = 100;
function setup(_api, log) {
say = log;
}
const actions = {
// "해 보기" 시작: 지금 모습을 보관해 두고 마음껏 바꿔 봐요.
start: (api) => {
if (trial !== null)
api.deleteSavePoint(trial); // 다시 누르면 이전 시점을 새 시점으로 바꿔요
trial = api.createSavePoint(en ? 'Before trying' : '해 보기 전').id;
api.setCellValue(1, 'qty', 999);
api.addRows([{ id: (trialId += 1), item: en ? 'Trial item' : '시험 품목', qty: 1 }]);
say({
ko: '해 보는 중이에요: 수량을 고치고 품목을 더했어요',
en: 'Trying: changed a quantity and added an item',
});
},
// 마음에 안 들면 보관한 모습으로. keepView: true라서 정렬·선택은 그대로예요.
rollback: (api) => {
if (trial === null)
return say({ ko: '먼저 해 보기를 시작하세요', en: 'Start a trial first' });
api.restoreSavePoint(trial, { keepView: true });
say({
ko: `되돌렸어요. 행 ${api.getRowCount()}개, 변경 ${api.getChanges().length}건`,
en: `Rolled back. ${api.getRowCount()} rows, ${api.getChanges().length} changes`,
});
},
// 마음에 들면 시점을 지우고 그대로 둬요.
keep: (api) => {
if (trial !== null)
api.deleteSavePoint(trial);
trial = null;
say({ ko: '그대로 두었어요. 저장 시점을 지웠어요', en: 'Kept it. The save point was deleted' });
},
sort: (api) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
};
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('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('start').addEventListener('click', () => actions.start(api));
document.getElementById('rollback').addEventListener('click', () => actions.rollback(api));
document.getElementById('keep').addEventListener('click', () => actions.keep(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.sort(api)}
>수량으로 정렬</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.start(api)}
>해 보기 시작</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.rollback(api)}
>되돌리기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.keep(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;
item: string;
qty: number;
}
const en = document.documentElement.dataset.lang === 'en';
const items = en
? ['Pen', 'Notebook', 'Stapler', 'Tape', 'Ruler', 'Eraser']
: ['펜', '노트', '스테이플러', '테이프', '자', '지우개'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: en ? 'Item' : '품목', width: 130, sortable: true },
{
id: 'qty',
title: en ? 'Qty' : '수량',
kind: 'number',
width: 90,
sortable: true,
editable: true,
},
],
rows: items.map((item, i) => ({ id: i + 1, item, qty: 10 + i * 5 })),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
let trial: string | null = null;
let trialId = 100;
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
export const actions = {
// "해 보기" 시작: 지금 모습을 보관해 두고 마음껏 바꿔 봐요.
start: (api: GridApi<Row>) => {
if (trial !== null) api.deleteSavePoint(trial); // 다시 누르면 이전 시점을 새 시점으로 바꿔요
trial = api.createSavePoint(en ? 'Before trying' : '해 보기 전').id;
api.setCellValue(1, 'qty', 999);
api.addRows([{ id: (trialId += 1), item: en ? 'Trial item' : '시험 품목', qty: 1 }]);
say({
ko: '해 보는 중이에요: 수량을 고치고 품목을 더했어요',
en: 'Trying: changed a quantity and added an item',
});
},
// 마음에 안 들면 보관한 모습으로. keepView: true라서 정렬·선택은 그대로예요.
rollback: (api: GridApi<Row>) => {
if (trial === null) return say({ ko: '먼저 해 보기를 시작하세요', en: 'Start a trial first' });
api.restoreSavePoint(trial, { keepView: true });
say({
ko: `되돌렸어요. 행 ${api.getRowCount()}개, 변경 ${api.getChanges().length}건`,
en: `Rolled back. ${api.getRowCount()} rows, ${api.getChanges().length} changes`,
});
},
// 마음에 들면 시점을 지우고 그대로 둬요.
keep: (api: GridApi<Row>) => {
if (trial !== null) api.deleteSavePoint(trial);
trial = null;
say({ ko: '그대로 두었어요. 저장 시점을 지웠어요', en: 'Kept it. The save point was deleted' });
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
};버튼을 누르면 이 예제의 코드가 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.sort(api)">수량으로 정렬</button>
<button type="button" :disabled="!api" @click="api && actions.start(api)">해 보기 시작</button>
<button
type="button"
:disabled="!api"
@click="api && actions.rollback(api)"
>되돌리기</button>
<button type="button" :disabled="!api" @click="api && actions.keep(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;
item: string;
qty: number;
}
const en = document.documentElement.dataset.lang === 'en';
const items = en
? ['Pen', 'Notebook', 'Stapler', 'Tape', 'Ruler', 'Eraser']
: ['펜', '노트', '스테이플러', '테이프', '자', '지우개'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: en ? 'Item' : '품목', width: 130, sortable: true },
{
id: 'qty',
title: en ? 'Qty' : '수량',
kind: 'number',
width: 90,
sortable: true,
editable: true,
},
],
rows: items.map((item, i) => ({ id: i + 1, item, qty: 10 + i * 5 })),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
let trial: string | null = null;
let trialId = 100;
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
export const actions = {
// "해 보기" 시작: 지금 모습을 보관해 두고 마음껏 바꿔 봐요.
start: (api: GridApi<Row>) => {
if (trial !== null) api.deleteSavePoint(trial); // 다시 누르면 이전 시점을 새 시점으로 바꿔요
trial = api.createSavePoint(en ? 'Before trying' : '해 보기 전').id;
api.setCellValue(1, 'qty', 999);
api.addRows([{ id: (trialId += 1), item: en ? 'Trial item' : '시험 품목', qty: 1 }]);
say({
ko: '해 보는 중이에요: 수량을 고치고 품목을 더했어요',
en: 'Trying: changed a quantity and added an item',
});
},
// 마음에 안 들면 보관한 모습으로. keepView: true라서 정렬·선택은 그대로예요.
rollback: (api: GridApi<Row>) => {
if (trial === null) return say({ ko: '먼저 해 보기를 시작하세요', en: 'Start a trial first' });
api.restoreSavePoint(trial, { keepView: true });
say({
ko: `되돌렸어요. 행 ${api.getRowCount()}개, 변경 ${api.getChanges().length}건`,
en: `Rolled back. ${api.getRowCount()} rows, ${api.getChanges().length} changes`,
});
},
// 마음에 들면 시점을 지우고 그대로 둬요.
keep: (api: GridApi<Row>) => {
if (trial !== null) api.deleteSavePoint(trial);
trial = null;
say({ ko: '그대로 두었어요. 저장 시점을 지웠어요', en: 'Kept it. The save point was deleted' });
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
};버튼을 누르면 이 예제의 코드가 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.sort(api)"
>수량으로 정렬</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.start(api)"
>해 보기 시작</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.rollback(api)"
>되돌리기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.keep(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;
item: string;
qty: number;
}
const en = document.documentElement.dataset.lang === 'en';
const items = en
? ['Pen', 'Notebook', 'Stapler', 'Tape', 'Ruler', 'Eraser']
: ['펜', '노트', '스테이플러', '테이프', '자', '지우개'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: en ? 'Item' : '품목', width: 130, sortable: true },
{
id: 'qty',
title: en ? 'Qty' : '수량',
kind: 'number',
width: 90,
sortable: true,
editable: true,
},
],
rows: items.map((item, i) => ({ id: i + 1, item, qty: 10 + i * 5 })),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
let trial: string | null = null;
let trialId = 100;
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
export const actions = {
// "해 보기" 시작: 지금 모습을 보관해 두고 마음껏 바꿔 봐요.
start: (api: GridApi<Row>) => {
if (trial !== null) api.deleteSavePoint(trial); // 다시 누르면 이전 시점을 새 시점으로 바꿔요
trial = api.createSavePoint(en ? 'Before trying' : '해 보기 전').id;
api.setCellValue(1, 'qty', 999);
api.addRows([{ id: (trialId += 1), item: en ? 'Trial item' : '시험 품목', qty: 1 }]);
say({
ko: '해 보는 중이에요: 수량을 고치고 품목을 더했어요',
en: 'Trying: changed a quantity and added an item',
});
},
// 마음에 안 들면 보관한 모습으로. keepView: true라서 정렬·선택은 그대로예요.
rollback: (api: GridApi<Row>) => {
if (trial === null) return say({ ko: '먼저 해 보기를 시작하세요', en: 'Start a trial first' });
api.restoreSavePoint(trial, { keepView: true });
say({
ko: `되돌렸어요. 행 ${api.getRowCount()}개, 변경 ${api.getChanges().length}건`,
en: `Rolled back. ${api.getRowCount()} rows, ${api.getChanges().length} changes`,
});
},
// 마음에 들면 시점을 지우고 그대로 둬요.
keep: (api: GridApi<Row>) => {
if (trial !== null) api.deleteSavePoint(trial);
trial = null;
say({ ko: '그대로 두었어요. 저장 시점을 지웠어요', en: 'Kept it. The save point was deleted' });
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
};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="sort">Sort by quantity</button>
<button type="button" id="start">Start trial</button>
<button type="button" id="rollback">Roll back</button>
<button type="button" id="keep">Keep it</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('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('start').addEventListener('click', () => actions.start(api));
document.getElementById('rollback').addEventListener('click', () => actions.rollback(api));
document.getElementById('keep').addEventListener('click', () => actions.keep(api));const items = ['Pen', 'Notebook', 'Stapler', 'Tape', 'Ruler', 'Eraser'];
export const options = {
columns: [
{ id: 'item', title: 'Item', width: 130, sortable: true },
{
id: 'qty',
title: 'Qty',
kind: 'number',
width: 90,
sortable: true,
editable: true,
},
],
rows: items.map((item, i) => ({ id: i + 1, item, qty: 10 + i * 5 })),
rowKey: 'id',
saveMode: 'batch',
};
let say = () => { };
let trial = null;
let trialId = 100;
export function setup(_api, log) {
say = log;
}
export const actions = {
// Start a "trial": keep the current look and change things freely.
start: (api) => {
if (trial !== null)
api.deleteSavePoint(trial); // pressing again replaces the previous save point with a new one
trial = api.createSavePoint('Before trying').id;
api.setCellValue(1, 'qty', 999);
api.addRows([{ id: (trialId += 1), item: 'Trial item', qty: 1 }]);
say('Trying: changed a quantity and added an item');
},
// If you do not like it, go back to the kept look. keepView: true leaves sorting and selection as they are.
rollback: (api) => {
if (trial === null)
return say('Start a trial first');
api.restoreSavePoint(trial, { keepView: true });
say(`Rolled back. ${api.getRowCount()} rows, ${api.getChanges().length} changes`);
},
// If you like it, delete the save point and keep things as they are.
keep: (api) => {
if (trial !== null)
api.deleteSavePoint(trial);
trial = null;
say('Kept it. The save point was deleted');
},
sort: (api) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Try something and roll back</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="sort">Sort by quantity</button>
<button type="button" id="start">Start trial</button>
<button type="button" id="rollback">Roll back</button>
<button type="button" id="keep">Keep it</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 items = ['Pen', 'Notebook', 'Stapler', 'Tape', 'Ruler', 'Eraser'];
const options = {
columns: [
{ id: 'item', title: 'Item', width: 130, sortable: true },
{
id: 'qty',
title: 'Qty',
kind: 'number',
width: 90,
sortable: true,
editable: true,
},
],
rows: items.map((item, i) => ({ id: i + 1, item, qty: 10 + i * 5 })),
rowKey: 'id',
saveMode: 'batch',
};
let say = () => { };
let trial = null;
let trialId = 100;
function setup(_api, log) {
say = log;
}
const actions = {
// Start a "trial": keep the current look and change things freely.
start: (api) => {
if (trial !== null)
api.deleteSavePoint(trial); // pressing again replaces the previous save point with a new one
trial = api.createSavePoint('Before trying').id;
api.setCellValue(1, 'qty', 999);
api.addRows([{ id: (trialId += 1), item: 'Trial item', qty: 1 }]);
say('Trying: changed a quantity and added an item');
},
// If you do not like it, go back to the kept look. keepView: true leaves sorting and selection as they are.
rollback: (api) => {
if (trial === null)
return say('Start a trial first');
api.restoreSavePoint(trial, { keepView: true });
say(`Rolled back. ${api.getRowCount()} rows, ${api.getChanges().length} changes`);
},
// If you like it, delete the save point and keep things as they are.
keep: (api) => {
if (trial !== null)
api.deleteSavePoint(trial);
trial = null;
say('Kept it. The save point was deleted');
},
sort: (api) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
};
options.ariaLabel = "Try something and roll back";
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('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('start').addEventListener('click', () => actions.start(api));
document.getElementById('rollback').addEventListener('click', () => actions.rollback(api));
document.getElementById('keep').addEventListener('click', () => actions.keep(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.sort(api)}
>Sort by quantity</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.start(api)}
>Start trial</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.rollback(api)}
>Roll back</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.keep(api)}
>Keep it</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;
item: string;
qty: number;
}
const items = ['Pen', 'Notebook', 'Stapler', 'Tape', 'Ruler', 'Eraser'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: 'Item', width: 130, sortable: true },
{
id: 'qty',
title: 'Qty',
kind: 'number',
width: 90,
sortable: true,
editable: true,
},
],
rows: items.map((item, i) => ({ id: i + 1, item, qty: 10 + i * 5 })),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
let trial: string | null = null;
let trialId = 100;
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
export const actions = {
// Start a "trial": keep the current look and change things freely.
start: (api: GridApi<Row>) => {
if (trial !== null) api.deleteSavePoint(trial); // pressing again replaces the previous save point with a new one
trial = api.createSavePoint('Before trying').id;
api.setCellValue(1, 'qty', 999);
api.addRows([{ id: (trialId += 1), item: 'Trial item', qty: 1 }]);
say('Trying: changed a quantity and added an item');
},
// If you do not like it, go back to the kept look. keepView: true leaves sorting and selection as they are.
rollback: (api: GridApi<Row>) => {
if (trial === null) return say('Start a trial first');
api.restoreSavePoint(trial, { keepView: true });
say(`Rolled back. ${api.getRowCount()} rows, ${api.getChanges().length} changes`);
},
// If you like it, delete the save point and keep things as they are.
keep: (api: GridApi<Row>) => {
if (trial !== null) api.deleteSavePoint(trial);
trial = null;
say('Kept it. The save point was deleted');
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
};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.sort(api)">Sort by quantity</button>
<button type="button" :disabled="!api" @click="api && actions.start(api)">Start trial</button>
<button
type="button"
:disabled="!api"
@click="api && actions.rollback(api)"
>Roll back</button>
<button type="button" :disabled="!api" @click="api && actions.keep(api)">Keep it</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;
item: string;
qty: number;
}
const items = ['Pen', 'Notebook', 'Stapler', 'Tape', 'Ruler', 'Eraser'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: 'Item', width: 130, sortable: true },
{
id: 'qty',
title: 'Qty',
kind: 'number',
width: 90,
sortable: true,
editable: true,
},
],
rows: items.map((item, i) => ({ id: i + 1, item, qty: 10 + i * 5 })),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
let trial: string | null = null;
let trialId = 100;
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
export const actions = {
// Start a "trial": keep the current look and change things freely.
start: (api: GridApi<Row>) => {
if (trial !== null) api.deleteSavePoint(trial); // pressing again replaces the previous save point with a new one
trial = api.createSavePoint('Before trying').id;
api.setCellValue(1, 'qty', 999);
api.addRows([{ id: (trialId += 1), item: 'Trial item', qty: 1 }]);
say('Trying: changed a quantity and added an item');
},
// If you do not like it, go back to the kept look. keepView: true leaves sorting and selection as they are.
rollback: (api: GridApi<Row>) => {
if (trial === null) return say('Start a trial first');
api.restoreSavePoint(trial, { keepView: true });
say(`Rolled back. ${api.getRowCount()} rows, ${api.getChanges().length} changes`);
},
// If you like it, delete the save point and keep things as they are.
keep: (api: GridApi<Row>) => {
if (trial !== null) api.deleteSavePoint(trial);
trial = null;
say('Kept it. The save point was deleted');
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
};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.sort(api)"
>Sort by quantity</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.start(api)"
>Start trial</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.rollback(api)"
>Roll back</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.keep(api)"
>Keep it</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;
item: string;
qty: number;
}
const items = ['Pen', 'Notebook', 'Stapler', 'Tape', 'Ruler', 'Eraser'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: 'Item', width: 130, sortable: true },
{
id: 'qty',
title: 'Qty',
kind: 'number',
width: 90,
sortable: true,
editable: true,
},
],
rows: items.map((item, i) => ({ id: i + 1, item, qty: 10 + i * 5 })),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
let trial: string | null = null;
let trialId = 100;
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
export const actions = {
// Start a "trial": keep the current look and change things freely.
start: (api: GridApi<Row>) => {
if (trial !== null) api.deleteSavePoint(trial); // pressing again replaces the previous save point with a new one
trial = api.createSavePoint('Before trying').id;
api.setCellValue(1, 'qty', 999);
api.addRows([{ id: (trialId += 1), item: 'Trial item', qty: 1 }]);
say('Trying: changed a quantity and added an item');
},
// If you do not like it, go back to the kept look. keepView: true leaves sorting and selection as they are.
rollback: (api: GridApi<Row>) => {
if (trial === null) return say('Start a trial first');
api.restoreSavePoint(trial, { keepView: true });
say(`Rolled back. ${api.getRowCount()} rows, ${api.getChanges().length} changes`);
},
// If you like it, delete the save point and keep things as they are.
keep: (api: GridApi<Row>) => {
if (trial !== null) api.deleteSavePoint(trial);
trial = null;
say('Kept it. The save point was deleted');
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
};