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

마스터 그리드와 요약 그리드 연결Master grid with a summary grid

편집할 수 있는 마스터와 읽기 전용 요약이 같은 저장소를 봐요. 마스터를 고치면 요약과 합계가 따라와요.An editable master and a read-only summary read the same store. Editing the master updates the summary and the total.

심화Advanced
직접 써 보기Live preview마스터 그리드와 요약 그리드 연결Master grid with a summary grid

코드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="add">행 추가</button>
    <button type="button" id="double">첫 행 수량 두 배</button>
    <button type="button" id="remove">마지막 행 지우기</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('add').addEventListener('click', () => actions.add(api));
document.getElementById('double').addEventListener('click', () => actions.double(api));
document.getElementById('remove').addEventListener('click', () => actions.remove(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' });
// 위 마스터 그리드는 편집할 수 있고, 아래 요약 그리드는 같은 저장소를 읽기 전용으로 작게 보여 줘요(이름·수량만, 수량 내림차순).
// 요약 그리드는 마스터에서 고친 값을 다음 프레임에 그대로 받고, 저장소 구독으로 합계를 기록 줄에 알려 줘요.
export const options = {
  density: 'normal', // 틀이 높이를 키우지 않게 고정(두 그리드 높이를 직접 정한다)
  columns: [
    { id: 'name', title: '이름', editable: true, width: 120 },
    { id: 'team', title: '팀', editable: true, width: 90 },
    { id: 'qty', title: '수량', kind: 'number', editable: true, width: 80 },
  ],
  rowsStore: store,
  ariaLabel: '마스터',
};
let summary = null;
let host = null;
let nextId = 100;
const total = () => store.getRows().reduce((sum, row) => sum + row.qty, 0);
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);
  summary = MaxGrid.createGrid(host, {
    locale: 'ko',
    columns: [
      { id: 'name', title: '이름', width: 120 },
      { id: 'qty', title: '수량', kind: 'number', width: 80 },
    ],
    rowsStore: store,
    ariaLabel: '요약',
  });
  summary.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
  const off = store.subscribe(() => log({ ko: `수량 합계: ${total()}`, en: `Total quantity: ${total()}` }));
  log({
    ko: `마스터에서 수량을 고치면 요약이 따라와요. 지금 합계는 ${total()}예요.`,
    en: `Edit a quantity in the master and the summary follows. The total is ${total()} now.`,
  });
  return () => {
    off();
    summary?.destroy();
    host?.remove();
    summary = host = null;
  };
}
export const actions = {
  add: (api) => {
    api.addRows([{ id: nextId, name: `새 사람 ${nextId}`, team: '개발', qty: 5 }]);
    nextId += 1;
  },
  double: (api) => {
    const row = api.getRows()[0];
    if (row)
      api.setCellValue(row.id, 'qty', row.qty * 2);
  },
  remove: (api) => {
    const last = api.getRows().at(-1);
    if (last)
      api.removeRows([last.id]);
  },
};

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="add">Add row</button>
    <button type="button" id="double">Double first qty</button>
    <button type="button" id="remove">Remove last row</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('add').addEventListener('click', () => actions.add(api));
document.getElementById('double').addEventListener('click', () => actions.double(api));
document.getElementById('remove').addEventListener('click', () => actions.remove(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' });
// The top master grid is editable; the bottom summary grid shows the same store small and read-only (name and quantity only, by quantity descending).
// The summary receives master edits on the next frame, and a store subscription reports the total in the log.
export const options = {
  density: 'normal', // keeps the frame from scaling heights (both grid heights are set here)
  columns: [
    { id: 'name', title: 'Name', editable: true, width: 120 },
    { id: 'team', title: 'Team', editable: true, width: 90 },
    { id: 'qty', title: 'Qty', kind: 'number', editable: true, width: 80 },
  ],
  rowsStore: store,
  ariaLabel: 'Master',
};
let summary = null;
let host = null;
let nextId = 100;
const total = () => store.getRows().reduce((sum, row) => sum + row.qty, 0);
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);
  summary = MaxGrid.createGrid(host, {
    locale: 'en',
    columns: [
      { id: 'name', title: 'Name', width: 120 },
      { id: 'qty', title: 'Qty', kind: 'number', width: 80 },
    ],
    rowsStore: store,
    ariaLabel: 'Summary',
  });
  summary.setSortModel([{ columnId: 'qty', direction: 'desc' }]);
  const off = store.subscribe(() => log(`Total quantity: ${total()}`));
  log(`Edit a quantity in the master and the summary follows. The total is ${total()} now.`);
  return () => {
    off();
    summary?.destroy();
    host?.remove();
    summary = host = null;
  };
}
export const actions = {
  add: (api) => {
    api.addRows([{ id: nextId, name: `New person ${nextId}`, team: 'Dev', qty: 5 }]);
    nextId += 1;
  },
  double: (api) => {
    const row = api.getRows()[0];
    if (row)
      api.setCellValue(row.id, 'qty', row.qty * 2);
  },
  remove: (api) => {
    const last = api.getRows().at(-1);
    if (last)
      api.removeRows([last.id]);
  },
};