다른 그리드의 변경 기록 적용하기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.
코드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="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>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));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(),
};<!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="make">변경 만들기</button>
<button type="button" id="apply">복사본에 적용</button>
<button type="button" id="undo">되돌리기</button>
</div>
<div id="grid" style="height: 230px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const make = () => [
{ id: 1, name: '사과', qty: 10 },
{ id: 2, name: '배', qty: 5 },
{ id: 3, name: '귤', qty: 8 },
];
// applyChanges는 다른 그리드의 getChanges() 결과를 이 그리드에 적용해요. 이 예제는 한 그리드가 "보내는 쪽"의 변경 기록을
// 만들고, 같은 컬럼의 두 번째 그리드(가상의 사본)에 적용하는 과정을 기록으로 보여 줘요.
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 = [];
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;
};
}
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(),
};
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('make').addEventListener('click', () => actions.make(api));
document.getElementById('apply').addEventListener('click', () => actions.apply(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(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.make(api)}
>변경 만들기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.apply(api)}
>복사본에 적용</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.undo(api)}
>되돌리기</button>
</div>
<Grid style={{ height: 230 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions, RowChange } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
qty: number;
}
const make = (): Row[] => [
{ id: 1, name: '사과', qty: 10 },
{ id: 2, name: '배', qty: 5 },
{ id: 3, name: '귤', qty: 8 },
];
// applyChanges는 다른 그리드의 getChanges() 결과를 이 그리드에 적용해요. 이 예제는 한 그리드가 "보내는 쪽"의 변경 기록을
// 만들고, 같은 컬럼의 두 번째 그리드(가상의 사본)에 적용하는 과정을 기록으로 보여 줘요.
export const options: GridOptions<Row> = {
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: (message: LogText) => void = () => undefined;
let pending: RowChange<Row>[] = [];
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
const report = api.applyChanges(pending);
say({
ko: `적용 ${report.applied}건, 건너뜀 ${report.skipped}건 (이 그리드에 없는 키는 건너뛰어요)`,
en: `Applied ${report.applied}, skipped ${report.skipped} (unknown keys are skipped)`,
});
},
undo: (api: GridApi<Row>) => void api.undo(),
};버튼을 누르면 이 예제의 코드가 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.make(api)">변경 만들기</button>
<button type="button" :disabled="!api" @click="api && actions.apply(api)">복사본에 적용</button>
<button type="button" :disabled="!api" @click="api && actions.undo(api)">되돌리기</button>
</div>
<Grid style="height: 230px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions, RowChange } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
qty: number;
}
const make = (): Row[] => [
{ id: 1, name: '사과', qty: 10 },
{ id: 2, name: '배', qty: 5 },
{ id: 3, name: '귤', qty: 8 },
];
// applyChanges는 다른 그리드의 getChanges() 결과를 이 그리드에 적용해요. 이 예제는 한 그리드가 "보내는 쪽"의 변경 기록을
// 만들고, 같은 컬럼의 두 번째 그리드(가상의 사본)에 적용하는 과정을 기록으로 보여 줘요.
export const options: GridOptions<Row> = {
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: (message: LogText) => void = () => undefined;
let pending: RowChange<Row>[] = [];
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
const report = api.applyChanges(pending);
say({
ko: `적용 ${report.applied}건, 건너뜀 ${report.skipped}건 (이 그리드에 없는 키는 건너뛰어요)`,
en: `Applied ${report.applied}, skipped ${report.skipped} (unknown keys are skipped)`,
});
},
undo: (api: GridApi<Row>) => void api.undo(),
};버튼을 누르면 이 예제의 코드가 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.make(api)"
>변경 만들기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.apply(api)"
>복사본에 적용</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.undo(api)"
>되돌리기</button>
</div>
<mx-grid style="height: 230px" [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, RowChange } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
qty: number;
}
const make = (): Row[] => [
{ id: 1, name: '사과', qty: 10 },
{ id: 2, name: '배', qty: 5 },
{ id: 3, name: '귤', qty: 8 },
];
// applyChanges는 다른 그리드의 getChanges() 결과를 이 그리드에 적용해요. 이 예제는 한 그리드가 "보내는 쪽"의 변경 기록을
// 만들고, 같은 컬럼의 두 번째 그리드(가상의 사본)에 적용하는 과정을 기록으로 보여 줘요.
export const options: GridOptions<Row> = {
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: (message: LogText) => void = () => undefined;
let pending: RowChange<Row>[] = [];
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
const report = api.applyChanges(pending);
say({
ko: `적용 ${report.applied}건, 건너뜀 ${report.skipped}건 (이 그리드에 없는 키는 건너뛰어요)`,
en: `Applied ${report.applied}, skipped ${report.skipped} (unknown keys are skipped)`,
});
},
undo: (api: GridApi<Row>) => void api.undo(),
};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="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>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));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(),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Apply the changes of another grid</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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).
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 = [];
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;
};
}
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(),
};
options.ariaLabel = "Apply the changes of another grid";
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('make').addEventListener('click', () => actions.make(api));
document.getElementById('apply').addEventListener('click', () => actions.apply(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(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.make(api)}
>Make changes</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.apply(api)}
>Apply a copy</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.undo(api)}
>Undo</button>
</div>
<Grid style={{ height: 230 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions, RowChange } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
qty: number;
}
const make = (): Row[] => [
{ 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: GridOptions<Row> = {
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: (message: LogText) => void = () => undefined;
let pending: RowChange<Row>[] = [];
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
const report = api.applyChanges(pending);
say(`Applied ${report.applied}, skipped ${report.skipped} (unknown keys are skipped)`);
},
undo: (api: GridApi<Row>) => void api.undo(),
};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.make(api)">Make changes</button>
<button type="button" :disabled="!api" @click="api && actions.apply(api)">Apply a copy</button>
<button type="button" :disabled="!api" @click="api && actions.undo(api)">Undo</button>
</div>
<Grid style="height: 230px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions, RowChange } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
qty: number;
}
const make = (): Row[] => [
{ 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: GridOptions<Row> = {
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: (message: LogText) => void = () => undefined;
let pending: RowChange<Row>[] = [];
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
const report = api.applyChanges(pending);
say(`Applied ${report.applied}, skipped ${report.skipped} (unknown keys are skipped)`);
},
undo: (api: GridApi<Row>) => void api.undo(),
};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.make(api)"
>Make changes</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.apply(api)"
>Apply a copy</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.undo(api)"
>Undo</button>
</div>
<mx-grid style="height: 230px" [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, RowChange } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
qty: number;
}
const make = (): Row[] => [
{ 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: GridOptions<Row> = {
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: (message: LogText) => void = () => undefined;
let pending: RowChange<Row>[] = [];
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
const report = api.applyChanges(pending);
say(`Applied ${report.applied}, skipped ${report.skipped} (unknown keys are skipped)`);
},
undo: (api: GridApi<Row>) => void api.undo(),
};