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

다른 그리드의 변경 기록 적용하기Apply the changes of another grid

applyChanges(changes)는 다른 그리드의 getChanges() 결과를 같은 컬럼의 그리드에 적용해요. 신규는 추가, 수정은 셀 단위 갱신, 삭제는 제거이고 없는 키는 건너뛰며 { applied, skipped }를 돌려줘요.applyChanges(changes) applies the getChanges() result of another grid to a grid with the same columns. Added rows are added, modified rows updated per cell, removed rows removed; unknown keys are skipped and { applied, skipped } is returned.

심화Advanced
직접 써 보기Live preview다른 그리드의 변경 기록 적용하기Apply the changes of another grid

코드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="make">변경 만들기</button>
    <button type="button" id="apply">복사본에 적용</button>
    <button type="button" id="undo">되돌리기</button>
  </div>
  <div id="grid" style="height: 230px"></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('make').addEventListener('click', () => actions.make(api));
document.getElementById('apply').addEventListener('click', () => actions.apply(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
example.js
const make = () => [
  { id: 1, name: '사과', qty: 10 },
  { id: 2, name: '배', qty: 5 },
  { id: 3, name: '귤', qty: 8 },
];
// applyChanges는 다른 그리드의 getChanges() 결과를 이 그리드에 적용해요. 이 예제는 한 그리드가 "보내는 쪽"의 변경 기록을
// 만들고, 같은 컬럼의 두 번째 그리드(가상의 사본)에 적용하는 과정을 기록으로 보여 줘요.
export const options = {
  columns: [
    { id: 'id', title: '번호', kind: 'number', width: 70 },
    { id: 'name', title: '이름', width: 110, editable: true },
    { id: 'qty', title: '수량', kind: 'number', width: 90, editable: true },
  ],
  rows: make(),
  rowKey: 'id',
};
let say = () => undefined;
let pending = [];
export function setup(api, log) {
  say = log;
  pending = [];
  log({
    ko: '"변경 만들기"로 변경을 쌓고 "복사본에 적용"을 눌러요. 이 그리드가 받는 쪽이에요.',
    en: 'Build changes with "Make changes", then press "Apply a copy". This grid is the receiving side.',
  });
  return () => {
    say = () => undefined;
  };
}
export const actions = {
  make: () => {
    // 보내는 쪽 그리드의 getChanges()와 같은 모양의 목록이에요(수정 1, 추가 1, 삭제 1, 이 그리드에 없는 키의 수정 1).
    pending = [
      {
        key: 2,
        state: 'modified',
        row: { id: 2, name: '배', qty: 50 },
        cells: [{ columnId: 'qty', oldValue: 5, newValue: 50 }],
      },
      { key: 10, state: 'added', row: { id: 10, name: '새 행', qty: 1 } },
      { key: 3, state: 'removed', row: { id: 3, name: '귤', qty: 8 } },
      {
        key: 99,
        state: 'modified',
        row: { id: 99, name: '없음', qty: 0 },
        cells: [{ columnId: 'qty', oldValue: 1, newValue: 2 }],
      },
    ];
    say({
      ko: `보낼 변경 ${pending.length}건을 만들었어요`,
      en: `Built ${pending.length} changes to send`,
    });
  },
  apply: (api) => {
    const report = api.applyChanges(pending);
    say({
      ko: `적용 ${report.applied}건, 건너뜀 ${report.skipped}건 (이 그리드에 없는 키는 건너뛰어요)`,
      en: `Applied ${report.applied}, skipped ${report.skipped} (unknown keys are skipped)`,
    });
  },
  undo: (api) => void api.undo(),
};

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="make">Make changes</button>
    <button type="button" id="apply">Apply a copy</button>
    <button type="button" id="undo">Undo</button>
  </div>
  <div id="grid" style="height: 230px"></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('make').addEventListener('click', () => actions.make(api));
document.getElementById('apply').addEventListener('click', () => actions.apply(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
example.js
const make = () => [
  { id: 1, name: 'Apple', qty: 10 },
  { id: 2, name: 'Pear', qty: 5 },
  { id: 3, name: 'Orange', qty: 8 },
];
// applyChanges applies the getChanges() result of another grid to this grid. This example builds the change list of a "sending" grid
// and shows in the log how it is applied to a second grid with the same columns (an imaginary copy).
export const options = {
  columns: [
    { id: 'id', title: 'ID', kind: 'number', width: 70 },
    { id: 'name', title: 'Name', width: 110, editable: true },
    { id: 'qty', title: 'Qty', kind: 'number', width: 90, editable: true },
  ],
  rows: make(),
  rowKey: 'id',
};
let say = () => undefined;
let pending = [];
export function setup(api, log) {
  say = log;
  pending = [];
  log('Build changes with "Make changes", then press "Apply a copy". This grid is the receiving side.');
  return () => {
    say = () => undefined;
  };
}
export const actions = {
  make: () => {
    // A list shaped like the sending grid's getChanges() (1 modified, 1 added, 1 removed, 1 modified with a key this grid lacks).
    pending = [
      {
        key: 2,
        state: 'modified',
        row: { id: 2, name: 'Pear', qty: 50 },
        cells: [{ columnId: 'qty', oldValue: 5, newValue: 50 }],
      },
      { key: 10, state: 'added', row: { id: 10, name: 'New row', qty: 1 } },
      { key: 3, state: 'removed', row: { id: 3, name: 'Orange', qty: 8 } },
      {
        key: 99,
        state: 'modified',
        row: { id: 99, name: 'Missing', qty: 0 },
        cells: [{ columnId: 'qty', oldValue: 1, newValue: 2 }],
      },
    ];
    say(`Built ${pending.length} changes to send`);
  },
  apply: (api) => {
    const report = api.applyChanges(pending);
    say(`Applied ${report.applied}, skipped ${report.skipped} (unknown keys are skipped)`);
  },
  undo: (api) => void api.undo(),
};