본문으로 건너뛰기Skip to content
예제 첫화면Examples home

해 보고 마음에 안 들면 되돌리기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.

활용Intermediate
직접 써 보기Live preview해 보고 마음에 안 들면 되돌리기Try something and roll back

코드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)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<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>
main.js
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));
example.js
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' }]),
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<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>
main.js
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));
example.js
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' }]),
};