행 저장소 하나를 두 그리드가 같이 쓰기Two grids on one shared rows store
createRowsStore로 만든 저장소를 두 그리드의 rowsStore 옵션에 주면 한쪽의 편집·추가·삭제가 다른 쪽에도 나타나요.Give a store from createRowsStore to the rowsStore option of two grids and edits, adds and removes in one show up in the other.
코드Code
탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.
이 화면은 새 모양 옵션을 켠 모양이에요: iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: 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="addA">A에 행 추가</button>
<button type="button" id="removeB">B에서 지우기</button>
<button type="button" id="bumpB">B에서 수량 올리기</button>
<button type="button" id="undoA">A 되돌리기</button>
<button type="button" id="txStore">저장소에 직접 쓰기</button>
</div>
<div id="grid" style="height: 260px"></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('addA').addEventListener('click', () => actions.addA(api));
document.getElementById('removeB').addEventListener('click', () => actions.removeB(api));
document.getElementById('bumpB').addEventListener('click', () => actions.bumpB(api));
document.getElementById('undoA').addEventListener('click', () => actions.undoA(api));
document.getElementById('txStore').addEventListener('click', () => actions.txStore(api));const store = MaxGrid.createRowsStore([
{ id: 1, name: '김하늘', team: '개발', qty: 4 },
{ id: 2, name: '이도윤', team: '운영', qty: 9 },
{ id: 3, name: '박서연', team: '디자인', qty: 2 },
{ id: 4, name: '최지우', team: '개발', qty: 12 },
{ id: 5, name: '정민준', team: '운영', qty: 6 },
], { rowKey: 'id' });
// 두 그리드가 같은 저장소(rowsStore)를 보여요. 한쪽에서 고치거나 넣거나 지우면 다른 쪽에도 다음 프레임에 나타나요.
// 정렬·필터·선택·컬럼은 그리드마다 따로예요. rows와 rowKey는 저장소가 정하므로 옵션에 쓰지 않아요.
const columns = () => [
{ id: 'name', title: '이름', editable: true, width: 120 },
{ id: 'team', title: '팀', width: 90 },
{ id: 'qty', title: '수량', kind: 'number', editable: true, width: 80 },
];
export const options = {
density: 'normal', // 틀이 높이를 키우지 않게 고정(두 그리드 높이를 직접 정한다)
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: '그리드 A',
};
let second = null;
let host = null;
let nextId = 100;
export function setup(api, log) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null)
first.after(host);
else
document.body.append(host);
second = MaxGrid.createGrid(host, {
locale: 'ko',
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: '그리드 B',
});
second.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe((c) => log({ ko: `저장소: ${c.reason} (${c.count}행)`, en: `store: ${c.reason} (${c.count} rows)` }));
log({
ko: '위(A)에서 수량을 고쳐 보세요. 아래(B)는 수량 내림차순으로 정렬돼 있고, 같은 데이터가 따라와요.',
en: 'Edit a quantity in the top grid (A). The bottom grid (B) is sorted by quantity, descending, and follows the same data.',
});
return () => {
off();
second?.destroy();
host?.remove();
second = host = null;
};
}
export const actions = {
addA: (api) => {
api.addRows([{ id: nextId, name: `새 사람 ${nextId}`, team: '개발', qty: 1 }]);
nextId += 1;
},
removeB: () => {
const key = second?.getSelectedKeys()[0] ?? second?.getRows().at(-1)?.id;
if (key !== undefined)
second?.removeRows([key]);
},
bumpB: () => {
const row = second?.getRows()[0];
if (row)
second?.setCellValue(row.id, 'qty', row.qty + 10);
},
undoA: (api) => void api.undo(),
txStore: () => store.transaction(() => {
store.updateRow(1, { qty: 50 });
store.addRows([{ id: nextId++, name: '저장소가 넣은 행', team: '운영', qty: 3 }]);
}),
};<!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="addA">A에 행 추가</button>
<button type="button" id="removeB">B에서 지우기</button>
<button type="button" id="bumpB">B에서 수량 올리기</button>
<button type="button" id="undoA">A 되돌리기</button>
<button type="button" id="txStore">저장소에 직접 쓰기</button>
</div>
<div id="grid" style="height: 260px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const store = MaxGrid.createRowsStore([
{ id: 1, name: '김하늘', team: '개발', qty: 4 },
{ id: 2, name: '이도윤', team: '운영', qty: 9 },
{ id: 3, name: '박서연', team: '디자인', qty: 2 },
{ id: 4, name: '최지우', team: '개발', qty: 12 },
{ id: 5, name: '정민준', team: '운영', qty: 6 },
], { rowKey: 'id' });
// 두 그리드가 같은 저장소(rowsStore)를 보여요. 한쪽에서 고치거나 넣거나 지우면 다른 쪽에도 다음 프레임에 나타나요.
// 정렬·필터·선택·컬럼은 그리드마다 따로예요. rows와 rowKey는 저장소가 정하므로 옵션에 쓰지 않아요.
const columns = () => [
{ id: 'name', title: '이름', editable: true, width: 120 },
{ id: 'team', title: '팀', width: 90 },
{ id: 'qty', title: '수량', kind: 'number', editable: true, width: 80 },
];
const options = {
density: 'normal', // 틀이 높이를 키우지 않게 고정(두 그리드 높이를 직접 정한다)
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: '그리드 A',
};
let second = null;
let host = null;
let nextId = 100;
function setup(api, log) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null)
first.after(host);
else
document.body.append(host);
second = MaxGrid.createGrid(host, {
locale: 'ko',
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: '그리드 B',
});
second.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe((c) => log({ ko: `저장소: ${c.reason} (${c.count}행)`, en: `store: ${c.reason} (${c.count} rows)` }));
log({
ko: '위(A)에서 수량을 고쳐 보세요. 아래(B)는 수량 내림차순으로 정렬돼 있고, 같은 데이터가 따라와요.',
en: 'Edit a quantity in the top grid (A). The bottom grid (B) is sorted by quantity, descending, and follows the same data.',
});
return () => {
off();
second?.destroy();
host?.remove();
second = host = null;
};
}
const actions = {
addA: (api) => {
api.addRows([{ id: nextId, name: `새 사람 ${nextId}`, team: '개발', qty: 1 }]);
nextId += 1;
},
removeB: () => {
const key = second?.getSelectedKeys()[0] ?? second?.getRows().at(-1)?.id;
if (key !== undefined)
second?.removeRows([key]);
},
bumpB: () => {
const row = second?.getRows()[0];
if (row)
second?.setCellValue(row.id, 'qty', row.qty + 10);
},
undoA: (api) => void api.undo(),
txStore: () => store.transaction(() => {
store.updateRow(1, { qty: 50 });
store.addRows([{ id: nextId++, name: '저장소가 넣은 행', team: '운영', qty: 3 }]);
}),
};
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('addA').addEventListener('click', () => actions.addA(api));
document.getElementById('removeB').addEventListener('click', () => actions.removeB(api));
document.getElementById('bumpB').addEventListener('click', () => actions.bumpB(api));
document.getElementById('undoA').addEventListener('click', () => actions.undoA(api));
document.getElementById('txStore').addEventListener('click', () => actions.txStore(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.addA(api)}
>A에 행 추가</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.removeB(api)}
>B에서 지우기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.bumpB(api)}
>B에서 수량 올리기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.undoA(api)}
>A 되돌리기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.txStore(api)}
>저장소에 직접 쓰기</button>
</div>
<Grid style={{ height: 260 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions, RowsStore } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
qty: number;
}
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
createRowsStore(rows: Row[], options: { rowKey: 'id' }): RowsStore<Row>;
};
const store = MaxGrid.createRowsStore(
[
{ id: 1, name: '김하늘', team: '개발', qty: 4 },
{ id: 2, name: '이도윤', team: '운영', qty: 9 },
{ id: 3, name: '박서연', team: '디자인', qty: 2 },
{ id: 4, name: '최지우', team: '개발', qty: 12 },
{ id: 5, name: '정민준', team: '운영', qty: 6 },
],
{ rowKey: 'id' },
);
// 두 그리드가 같은 저장소(rowsStore)를 보여요. 한쪽에서 고치거나 넣거나 지우면 다른 쪽에도 다음 프레임에 나타나요.
// 정렬·필터·선택·컬럼은 그리드마다 따로예요. rows와 rowKey는 저장소가 정하므로 옵션에 쓰지 않아요.
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{ id: 'name', title: '이름', editable: true, width: 120 },
{ id: 'team', title: '팀', width: 90 },
{ id: 'qty', title: '수량', kind: 'number', editable: true, width: 80 },
];
export const options: GridOptions<Row> = {
density: 'normal', // 틀이 높이를 키우지 않게 고정(두 그리드 높이를 직접 정한다)
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: '그리드 A',
};
let second: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
let nextId = 100;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
second = MaxGrid.createGrid(host, {
locale: 'ko',
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: '그리드 B',
});
second.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe((c) =>
log({ ko: `저장소: ${c.reason} (${c.count}행)`, en: `store: ${c.reason} (${c.count} rows)` }),
);
log({
ko: '위(A)에서 수량을 고쳐 보세요. 아래(B)는 수량 내림차순으로 정렬돼 있고, 같은 데이터가 따라와요.',
en: 'Edit a quantity in the top grid (A). The bottom grid (B) is sorted by quantity, descending, and follows the same data.',
});
return () => {
off();
second?.destroy();
host?.remove();
second = host = null;
};
}
export const actions = {
addA: (api: GridApi<Row>) => {
api.addRows([{ id: nextId, name: `새 사람 ${nextId}`, team: '개발', qty: 1 }]);
nextId += 1;
},
removeB: () => {
const key = second?.getSelectedKeys()[0] ?? second?.getRows().at(-1)?.id;
if (key !== undefined) second?.removeRows([key]);
},
bumpB: () => {
const row = second?.getRows()[0];
if (row) second?.setCellValue(row.id, 'qty', row.qty + 10);
},
undoA: (api: GridApi<Row>) => void api.undo(),
txStore: () =>
store.transaction(() => {
store.updateRow(1, { qty: 50 });
store.addRows([{ id: nextId++, name: '저장소가 넣은 행', team: '운영', qty: 3 }]);
}),
};버튼을 누르면 이 예제의 코드가 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.addA(api)">A에 행 추가</button>
<button type="button" :disabled="!api" @click="api && actions.removeB(api)">B에서 지우기</button>
<button type="button" :disabled="!api" @click="api && actions.bumpB(api)">B에서 수량 올리기</button>
<button type="button" :disabled="!api" @click="api && actions.undoA(api)">A 되돌리기</button>
<button type="button" :disabled="!api" @click="api && actions.txStore(api)">저장소에 직접 쓰기</button>
</div>
<Grid style="height: 260px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions, RowsStore } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
qty: number;
}
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
createRowsStore(rows: Row[], options: { rowKey: 'id' }): RowsStore<Row>;
};
const store = MaxGrid.createRowsStore(
[
{ id: 1, name: '김하늘', team: '개발', qty: 4 },
{ id: 2, name: '이도윤', team: '운영', qty: 9 },
{ id: 3, name: '박서연', team: '디자인', qty: 2 },
{ id: 4, name: '최지우', team: '개발', qty: 12 },
{ id: 5, name: '정민준', team: '운영', qty: 6 },
],
{ rowKey: 'id' },
);
// 두 그리드가 같은 저장소(rowsStore)를 보여요. 한쪽에서 고치거나 넣거나 지우면 다른 쪽에도 다음 프레임에 나타나요.
// 정렬·필터·선택·컬럼은 그리드마다 따로예요. rows와 rowKey는 저장소가 정하므로 옵션에 쓰지 않아요.
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{ id: 'name', title: '이름', editable: true, width: 120 },
{ id: 'team', title: '팀', width: 90 },
{ id: 'qty', title: '수량', kind: 'number', editable: true, width: 80 },
];
export const options: GridOptions<Row> = {
density: 'normal', // 틀이 높이를 키우지 않게 고정(두 그리드 높이를 직접 정한다)
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: '그리드 A',
};
let second: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
let nextId = 100;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
second = MaxGrid.createGrid(host, {
locale: 'ko',
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: '그리드 B',
});
second.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe((c) =>
log({ ko: `저장소: ${c.reason} (${c.count}행)`, en: `store: ${c.reason} (${c.count} rows)` }),
);
log({
ko: '위(A)에서 수량을 고쳐 보세요. 아래(B)는 수량 내림차순으로 정렬돼 있고, 같은 데이터가 따라와요.',
en: 'Edit a quantity in the top grid (A). The bottom grid (B) is sorted by quantity, descending, and follows the same data.',
});
return () => {
off();
second?.destroy();
host?.remove();
second = host = null;
};
}
export const actions = {
addA: (api: GridApi<Row>) => {
api.addRows([{ id: nextId, name: `새 사람 ${nextId}`, team: '개발', qty: 1 }]);
nextId += 1;
},
removeB: () => {
const key = second?.getSelectedKeys()[0] ?? second?.getRows().at(-1)?.id;
if (key !== undefined) second?.removeRows([key]);
},
bumpB: () => {
const row = second?.getRows()[0];
if (row) second?.setCellValue(row.id, 'qty', row.qty + 10);
},
undoA: (api: GridApi<Row>) => void api.undo(),
txStore: () =>
store.transaction(() => {
store.updateRow(1, { qty: 50 });
store.addRows([{ id: nextId++, name: '저장소가 넣은 행', team: '운영', qty: 3 }]);
}),
};버튼을 누르면 이 예제의 코드가 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.addA(api)"
>A에 행 추가</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.removeB(api)"
>B에서 지우기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.bumpB(api)"
>B에서 수량 올리기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.undoA(api)"
>A 되돌리기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.txStore(api)"
>저장소에 직접 쓰기</button>
</div>
<mx-grid style="height: 260px" [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, RowsStore } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
qty: number;
}
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
createRowsStore(rows: Row[], options: { rowKey: 'id' }): RowsStore<Row>;
};
const store = MaxGrid.createRowsStore(
[
{ id: 1, name: '김하늘', team: '개발', qty: 4 },
{ id: 2, name: '이도윤', team: '운영', qty: 9 },
{ id: 3, name: '박서연', team: '디자인', qty: 2 },
{ id: 4, name: '최지우', team: '개발', qty: 12 },
{ id: 5, name: '정민준', team: '운영', qty: 6 },
],
{ rowKey: 'id' },
);
// 두 그리드가 같은 저장소(rowsStore)를 보여요. 한쪽에서 고치거나 넣거나 지우면 다른 쪽에도 다음 프레임에 나타나요.
// 정렬·필터·선택·컬럼은 그리드마다 따로예요. rows와 rowKey는 저장소가 정하므로 옵션에 쓰지 않아요.
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{ id: 'name', title: '이름', editable: true, width: 120 },
{ id: 'team', title: '팀', width: 90 },
{ id: 'qty', title: '수량', kind: 'number', editable: true, width: 80 },
];
export const options: GridOptions<Row> = {
density: 'normal', // 틀이 높이를 키우지 않게 고정(두 그리드 높이를 직접 정한다)
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: '그리드 A',
};
let second: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
let nextId = 100;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
second = MaxGrid.createGrid(host, {
locale: 'ko',
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: '그리드 B',
});
second.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe((c) =>
log({ ko: `저장소: ${c.reason} (${c.count}행)`, en: `store: ${c.reason} (${c.count} rows)` }),
);
log({
ko: '위(A)에서 수량을 고쳐 보세요. 아래(B)는 수량 내림차순으로 정렬돼 있고, 같은 데이터가 따라와요.',
en: 'Edit a quantity in the top grid (A). The bottom grid (B) is sorted by quantity, descending, and follows the same data.',
});
return () => {
off();
second?.destroy();
host?.remove();
second = host = null;
};
}
export const actions = {
addA: (api: GridApi<Row>) => {
api.addRows([{ id: nextId, name: `새 사람 ${nextId}`, team: '개발', qty: 1 }]);
nextId += 1;
},
removeB: () => {
const key = second?.getSelectedKeys()[0] ?? second?.getRows().at(-1)?.id;
if (key !== undefined) second?.removeRows([key]);
},
bumpB: () => {
const row = second?.getRows()[0];
if (row) second?.setCellValue(row.id, 'qty', row.qty + 10);
},
undoA: (api: GridApi<Row>) => void api.undo(),
txStore: () =>
store.transaction(() => {
store.updateRow(1, { qty: 50 });
store.addRows([{ id: nextId++, name: '저장소가 넣은 행', team: '운영', qty: 3 }]);
}),
};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="addA">Add row in A</button>
<button type="button" id="removeB">Remove in B</button>
<button type="button" id="bumpB">Raise qty in B</button>
<button type="button" id="undoA">Undo in A</button>
<button type="button" id="txStore">Write to the store</button>
</div>
<div id="grid" style="height: 260px"></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('addA').addEventListener('click', () => actions.addA(api));
document.getElementById('removeB').addEventListener('click', () => actions.removeB(api));
document.getElementById('bumpB').addEventListener('click', () => actions.bumpB(api));
document.getElementById('undoA').addEventListener('click', () => actions.undoA(api));
document.getElementById('txStore').addEventListener('click', () => actions.txStore(api));const store = MaxGrid.createRowsStore([
{ id: 1, name: 'Liam Carter', team: 'Dev', qty: 4 },
{ id: 2, name: 'Emma Stone', team: 'Ops', qty: 9 },
{ id: 3, name: 'Noah Reed', team: 'Design', qty: 2 },
{ id: 4, name: 'Ava Brooks', team: 'Dev', qty: 12 },
{ id: 5, name: 'Mia Lopez', team: 'Ops', qty: 6 },
], { rowKey: 'id' });
// Two grids show the same store (rowsStore). An edit, add or remove in one appears in the other on the next frame.
// Sort, filter, selection and columns are per grid. rows and rowKey come from the store, so they are not options.
const columns = () => [
{ id: 'name', title: 'Name', editable: true, width: 120 },
{ id: 'team', title: 'Team', width: 90 },
{ id: 'qty', title: 'Qty', kind: 'number', editable: true, width: 80 },
];
export const options = {
density: 'normal', // keeps the frame from scaling heights (both grid heights are set here)
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: 'Grid A',
};
let second = null;
let host = null;
let nextId = 100;
export function setup(api, log) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null)
first.after(host);
else
document.body.append(host);
second = MaxGrid.createGrid(host, {
locale: 'en',
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: 'Grid B',
});
second.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe((c) => log(`store: ${c.reason} (${c.count} rows)`));
log('Edit a quantity in the top grid (A). The bottom grid (B) is sorted by quantity, descending, and follows the same data.');
return () => {
off();
second?.destroy();
host?.remove();
second = host = null;
};
}
export const actions = {
addA: (api) => {
api.addRows([{ id: nextId, name: `New person ${nextId}`, team: 'Dev', qty: 1 }]);
nextId += 1;
},
removeB: () => {
const key = second?.getSelectedKeys()[0] ?? second?.getRows().at(-1)?.id;
if (key !== undefined)
second?.removeRows([key]);
},
bumpB: () => {
const row = second?.getRows()[0];
if (row)
second?.setCellValue(row.id, 'qty', row.qty + 10);
},
undoA: (api) => void api.undo(),
txStore: () => store.transaction(() => {
store.updateRow(1, { qty: 50 });
store.addRows([{ id: nextId++, name: 'Row added by the store', team: 'Ops', qty: 3 }]);
}),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Two grids on one shared rows store</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="addA">Add row in A</button>
<button type="button" id="removeB">Remove in B</button>
<button type="button" id="bumpB">Raise qty in B</button>
<button type="button" id="undoA">Undo in A</button>
<button type="button" id="txStore">Write to the store</button>
</div>
<div id="grid" style="height: 260px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const store = MaxGrid.createRowsStore([
{ id: 1, name: 'Liam Carter', team: 'Dev', qty: 4 },
{ id: 2, name: 'Emma Stone', team: 'Ops', qty: 9 },
{ id: 3, name: 'Noah Reed', team: 'Design', qty: 2 },
{ id: 4, name: 'Ava Brooks', team: 'Dev', qty: 12 },
{ id: 5, name: 'Mia Lopez', team: 'Ops', qty: 6 },
], { rowKey: 'id' });
// Two grids show the same store (rowsStore). An edit, add or remove in one appears in the other on the next frame.
// Sort, filter, selection and columns are per grid. rows and rowKey come from the store, so they are not options.
const columns = () => [
{ id: 'name', title: 'Name', editable: true, width: 120 },
{ id: 'team', title: 'Team', width: 90 },
{ id: 'qty', title: 'Qty', kind: 'number', editable: true, width: 80 },
];
const options = {
density: 'normal', // keeps the frame from scaling heights (both grid heights are set here)
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: 'Grid A',
};
let second = null;
let host = null;
let nextId = 100;
function setup(api, log) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null)
first.after(host);
else
document.body.append(host);
second = MaxGrid.createGrid(host, {
locale: 'en',
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: 'Grid B',
});
second.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe((c) => log(`store: ${c.reason} (${c.count} rows)`));
log('Edit a quantity in the top grid (A). The bottom grid (B) is sorted by quantity, descending, and follows the same data.');
return () => {
off();
second?.destroy();
host?.remove();
second = host = null;
};
}
const actions = {
addA: (api) => {
api.addRows([{ id: nextId, name: `New person ${nextId}`, team: 'Dev', qty: 1 }]);
nextId += 1;
},
removeB: () => {
const key = second?.getSelectedKeys()[0] ?? second?.getRows().at(-1)?.id;
if (key !== undefined)
second?.removeRows([key]);
},
bumpB: () => {
const row = second?.getRows()[0];
if (row)
second?.setCellValue(row.id, 'qty', row.qty + 10);
},
undoA: (api) => void api.undo(),
txStore: () => store.transaction(() => {
store.updateRow(1, { qty: 50 });
store.addRows([{ id: nextId++, name: 'Row added by the store', team: 'Ops', qty: 3 }]);
}),
};
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('addA').addEventListener('click', () => actions.addA(api));
document.getElementById('removeB').addEventListener('click', () => actions.removeB(api));
document.getElementById('bumpB').addEventListener('click', () => actions.bumpB(api));
document.getElementById('undoA').addEventListener('click', () => actions.undoA(api));
document.getElementById('txStore').addEventListener('click', () => actions.txStore(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.addA(api)}
>Add row in A</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.removeB(api)}
>Remove in B</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.bumpB(api)}
>Raise qty in B</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.undoA(api)}
>Undo in A</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.txStore(api)}
>Write to the store</button>
</div>
<Grid style={{ height: 260 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions, RowsStore } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
name: string;
team: string;
qty: number;
}
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
createRowsStore(rows: Row[], options: { rowKey: 'id' }): RowsStore<Row>;
};
const store = MaxGrid.createRowsStore(
[
{ id: 1, name: 'Liam Carter', team: 'Dev', qty: 4 },
{ id: 2, name: 'Emma Stone', team: 'Ops', qty: 9 },
{ id: 3, name: 'Noah Reed', team: 'Design', qty: 2 },
{ id: 4, name: 'Ava Brooks', team: 'Dev', qty: 12 },
{ id: 5, name: 'Mia Lopez', team: 'Ops', qty: 6 },
],
{ rowKey: 'id' },
);
// Two grids show the same store (rowsStore). An edit, add or remove in one appears in the other on the next frame.
// Sort, filter, selection and columns are per grid. rows and rowKey come from the store, so they are not options.
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{ id: 'name', title: 'Name', editable: true, width: 120 },
{ id: 'team', title: 'Team', width: 90 },
{ id: 'qty', title: 'Qty', kind: 'number', editable: true, width: 80 },
];
export const options: GridOptions<Row> = {
density: 'normal', // keeps the frame from scaling heights (both grid heights are set here)
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: 'Grid A',
};
let second: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
let nextId = 100;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
second = MaxGrid.createGrid(host, {
locale: 'en',
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: 'Grid B',
});
second.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe((c) => log(`store: ${c.reason} (${c.count} rows)`));
log(
'Edit a quantity in the top grid (A). The bottom grid (B) is sorted by quantity, descending, and follows the same data.',
);
return () => {
off();
second?.destroy();
host?.remove();
second = host = null;
};
}
export const actions = {
addA: (api: GridApi<Row>) => {
api.addRows([{ id: nextId, name: `New person ${nextId}`, team: 'Dev', qty: 1 }]);
nextId += 1;
},
removeB: () => {
const key = second?.getSelectedKeys()[0] ?? second?.getRows().at(-1)?.id;
if (key !== undefined) second?.removeRows([key]);
},
bumpB: () => {
const row = second?.getRows()[0];
if (row) second?.setCellValue(row.id, 'qty', row.qty + 10);
},
undoA: (api: GridApi<Row>) => void api.undo(),
txStore: () =>
store.transaction(() => {
store.updateRow(1, { qty: 50 });
store.addRows([{ id: nextId++, name: 'Row added by the store', team: 'Ops', qty: 3 }]);
}),
};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.addA(api)">Add row in A</button>
<button type="button" :disabled="!api" @click="api && actions.removeB(api)">Remove in B</button>
<button type="button" :disabled="!api" @click="api && actions.bumpB(api)">Raise qty in B</button>
<button type="button" :disabled="!api" @click="api && actions.undoA(api)">Undo in A</button>
<button type="button" :disabled="!api" @click="api && actions.txStore(api)">Write to the store</button>
</div>
<Grid style="height: 260px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions, RowsStore } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
name: string;
team: string;
qty: number;
}
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
createRowsStore(rows: Row[], options: { rowKey: 'id' }): RowsStore<Row>;
};
const store = MaxGrid.createRowsStore(
[
{ id: 1, name: 'Liam Carter', team: 'Dev', qty: 4 },
{ id: 2, name: 'Emma Stone', team: 'Ops', qty: 9 },
{ id: 3, name: 'Noah Reed', team: 'Design', qty: 2 },
{ id: 4, name: 'Ava Brooks', team: 'Dev', qty: 12 },
{ id: 5, name: 'Mia Lopez', team: 'Ops', qty: 6 },
],
{ rowKey: 'id' },
);
// Two grids show the same store (rowsStore). An edit, add or remove in one appears in the other on the next frame.
// Sort, filter, selection and columns are per grid. rows and rowKey come from the store, so they are not options.
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{ id: 'name', title: 'Name', editable: true, width: 120 },
{ id: 'team', title: 'Team', width: 90 },
{ id: 'qty', title: 'Qty', kind: 'number', editable: true, width: 80 },
];
export const options: GridOptions<Row> = {
density: 'normal', // keeps the frame from scaling heights (both grid heights are set here)
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: 'Grid A',
};
let second: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
let nextId = 100;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
second = MaxGrid.createGrid(host, {
locale: 'en',
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: 'Grid B',
});
second.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe((c) => log(`store: ${c.reason} (${c.count} rows)`));
log(
'Edit a quantity in the top grid (A). The bottom grid (B) is sorted by quantity, descending, and follows the same data.',
);
return () => {
off();
second?.destroy();
host?.remove();
second = host = null;
};
}
export const actions = {
addA: (api: GridApi<Row>) => {
api.addRows([{ id: nextId, name: `New person ${nextId}`, team: 'Dev', qty: 1 }]);
nextId += 1;
},
removeB: () => {
const key = second?.getSelectedKeys()[0] ?? second?.getRows().at(-1)?.id;
if (key !== undefined) second?.removeRows([key]);
},
bumpB: () => {
const row = second?.getRows()[0];
if (row) second?.setCellValue(row.id, 'qty', row.qty + 10);
},
undoA: (api: GridApi<Row>) => void api.undo(),
txStore: () =>
store.transaction(() => {
store.updateRow(1, { qty: 50 });
store.addRows([{ id: nextId++, name: 'Row added by the store', team: 'Ops', qty: 3 }]);
}),
};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.addA(api)"
>Add row in A</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.removeB(api)"
>Remove in B</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.bumpB(api)"
>Raise qty in B</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.undoA(api)"
>Undo in A</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.txStore(api)"
>Write to the store</button>
</div>
<mx-grid style="height: 260px" [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, RowsStore } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
name: string;
team: string;
qty: number;
}
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
createRowsStore(rows: Row[], options: { rowKey: 'id' }): RowsStore<Row>;
};
const store = MaxGrid.createRowsStore(
[
{ id: 1, name: 'Liam Carter', team: 'Dev', qty: 4 },
{ id: 2, name: 'Emma Stone', team: 'Ops', qty: 9 },
{ id: 3, name: 'Noah Reed', team: 'Design', qty: 2 },
{ id: 4, name: 'Ava Brooks', team: 'Dev', qty: 12 },
{ id: 5, name: 'Mia Lopez', team: 'Ops', qty: 6 },
],
{ rowKey: 'id' },
);
// Two grids show the same store (rowsStore). An edit, add or remove in one appears in the other on the next frame.
// Sort, filter, selection and columns are per grid. rows and rowKey come from the store, so they are not options.
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{ id: 'name', title: 'Name', editable: true, width: 120 },
{ id: 'team', title: 'Team', width: 90 },
{ id: 'qty', title: 'Qty', kind: 'number', editable: true, width: 80 },
];
export const options: GridOptions<Row> = {
density: 'normal', // keeps the frame from scaling heights (both grid heights are set here)
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: 'Grid A',
};
let second: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
let nextId = 100;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const first = document.getElementById('grid');
host = document.createElement('div');
host.style.cssText = 'height:260px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
second = MaxGrid.createGrid(host, {
locale: 'en',
columns: columns(),
rowsStore: store,
selection: 'multiple',
ariaLabel: 'Grid B',
});
second.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
const off = store.subscribe((c) => log(`store: ${c.reason} (${c.count} rows)`));
log(
'Edit a quantity in the top grid (A). The bottom grid (B) is sorted by quantity, descending, and follows the same data.',
);
return () => {
off();
second?.destroy();
host?.remove();
second = host = null;
};
}
export const actions = {
addA: (api: GridApi<Row>) => {
api.addRows([{ id: nextId, name: `New person ${nextId}`, team: 'Dev', qty: 1 }]);
nextId += 1;
},
removeB: () => {
const key = second?.getSelectedKeys()[0] ?? second?.getRows().at(-1)?.id;
if (key !== undefined) second?.removeRows([key]);
},
bumpB: () => {
const row = second?.getRows()[0];
if (row) second?.setCellValue(row.id, 'qty', row.qty + 10);
},
undoA: (api: GridApi<Row>) => void api.undo(),
txStore: () =>
store.transaction(() => {
store.updateRow(1, { qty: 50 });
store.addRows([{ id: nextId++, name: 'Row added by the store', team: 'Ops', qty: 3 }]);
}),
};