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

행 저장소 하나를 두 그리드가 같이 쓰기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.

심화Advanced
직접 써 보기Live preview행 저장소 하나를 두 그리드가 같이 쓰기Two grids on one shared rows store

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

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

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

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

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="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>
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('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));
example.js
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 }]);
  }),
};