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

소계를 그룹 이름 줄에 함께 보여 주기Show subtotals on the group name row itself

소계 줄을 따로 만들지 않고 그룹 이름 줄에 수량과 금액의 소계를 같이 적어요. 줄 수가 늘지 않고, 그룹을 접어 둔 채로도 지역별 합계를 한눈에 비교할 수 있어요.Instead of separate subtotal rows, the quantity and amount subtotals sit on the group name row. No extra rows, and folded groups still show their totals for easy comparison.

활용Intermediate
직접 써 보기Live preview소계를 그룹 이름 줄에 함께 보여 주기Show subtotals on the group name row itself

코드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">
    <select aria-label="소계 위치" id="where">
      <option value="header" selected>그룹 이름 줄 (기본)</option>
      <option value="footer">그룹 끝의 별도 줄</option>
      <option value="both">둘 다</option>
    </select>
    <select aria-label="펼침" id="depth">
      <option value="all" selected>모두 펼치기</option>
      <option value="region">지역만 펼치기</option>
      <option value="none">모두 접기</option>
    </select>
  </div>
  <div id="grid" style="height: 740px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('where').addEventListener('change', (event) => actions.where(api, event.target.value));
document.getElementById('depth').addEventListener('change', (event) => actions.depth(api, event.target.value));
example.js
const PEOPLE = {
  서울: ['김민준', '이서연'],
  부산: ['박지호', '최유나'],
};
const items = ['A4 용지', '복합기 토너', '사무용 의자', '모니터 받침대', '화이트보드'];
const rows = [];
for (const [region, managers] of Object.entries(PEOPLE)) {
  managers.forEach((manager, m) => {
    for (let k = 0; k < 2; k += 1) {
      const id = rows.length + 1;
      rows.push({
        id,
        region,
        manager,
        item: items[(id * 3 + m) % 5],
        quantity: 2 + ((id * 7) % 18),
        amount: 18000 + ((id * 41000) % 260000),
      });
    }
  });
}
// summary: 'header'(기본값)면 소계를 따로 줄로 만들지 않고 그룹 이름 줄(가지)에 함께 보여 줘요.
// 그룹을 접어도 소계가 그 줄에 남아 있어서, 접어 둔 채로 합계를 비교할 수 있어요.
export const options = {
  columns: [
    { id: 'region', title: '지역', width: 170, aggregate: 'rowCount' },
    { id: 'manager', title: '담당자', width: 110 },
    { id: 'item', title: '품목', width: 140 },
    { id: 'quantity', title: '수량', kind: 'number', width: 90, align: 'end', aggregate: 'sum' },
    {
      id: 'amount',
      title: '금액(원)',
      kind: 'number',
      width: 130,
      align: 'end',
      aggregate: 'sum',
      formatValue: (value) => Number(value).toLocaleString('ko-KR'),
    },
  ],
  rows,
  rowKey: 'id',
  grouping: { by: ['region', 'manager'], expandDepth: 'all', summary: 'header' },
  summary: { label: '전체 합계' },
};
export const actions = {
  depth: (api, value) => {
    if (value === 'none')
      api.collapseAll();
    else if (value === 'region')
      api.expandToDepth(1);
    else
      api.expandAll();
  },
  // 소계를 어디에 둘지 바꿔 봐요: 그룹 줄 / 그룹 끝 줄 / 둘 다.
  where: (api, value) => {
    const summary = value === 'footer' || value === 'both' ? value : 'header';
    api.setOptions({ grouping: { by: api.getGroupBy(), expandDepth: 'all', summary } });
  },
};

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">
    <select aria-label="Subtotal position" id="where">
      <option value="header" selected>On the group row (default)</option>
      <option value="footer">Separate row at the end</option>
      <option value="both">Both</option>
    </select>
    <select aria-label="Expansion" id="depth">
      <option value="all" selected>Expand all</option>
      <option value="region">Regions only</option>
      <option value="none">Collapse all</option>
    </select>
  </div>
  <div id="grid" style="height: 740px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('where').addEventListener('change', (event) => actions.where(api, event.target.value));
document.getElementById('depth').addEventListener('change', (event) => actions.depth(api, event.target.value));
example.js
const PEOPLE = {
  'New York': ['Liam Carter', 'Emma Stone'],
  Boston: ['Noah Kim', 'Olivia Park'],
};
const items = ['Letter paper', 'Printer toner', 'Office chair', 'Monitor stand', 'Whiteboard'];
const rows = [];
for (const [region, managers] of Object.entries(PEOPLE)) {
  managers.forEach((manager, m) => {
    for (let k = 0; k < 2; k += 1) {
      const id = rows.length + 1;
      rows.push({
        id,
        region,
        manager,
        item: items[(id * 3 + m) % 5],
        quantity: 2 + ((id * 7) % 18),
        amount: 18 + ((id * 41) % 260),
      });
    }
  });
}
// With summary: 'header' (the default), the subtotal is not a separate row but is shown on the group name row (the branch).
// A collapsed group keeps its subtotal on that row, so you can compare totals while everything is folded.
export const options = {
  columns: [
    { id: 'region', title: 'Region', width: 170, aggregate: 'rowCount' },
    { id: 'manager', title: 'Manager', width: 110 },
    { id: 'item', title: 'Item', width: 140 },
    { id: 'quantity', title: 'Qty', kind: 'number', width: 90, align: 'end', aggregate: 'sum' },
    {
      id: 'amount',
      title: 'Amount ($)',
      kind: 'number',
      width: 130,
      align: 'end',
      aggregate: 'sum',
      formatValue: (value) => Number(value).toLocaleString('en-US'),
    },
  ],
  rows,
  rowKey: 'id',
  grouping: { by: ['region', 'manager'], expandDepth: 'all', summary: 'header' },
  summary: { label: 'Grand total' },
};
export const actions = {
  depth: (api, value) => {
    if (value === 'none')
      api.collapseAll();
    else if (value === 'region')
      api.expandToDepth(1);
    else
      api.expandAll();
  },
  // Try placing the subtotal: on the group row / on a row at the group end / both.
  where: (api, value) => {
    const summary = value === 'footer' || value === 'both' ? value : 'header';
    api.setOptions({ grouping: { by: api.getGroupBy(), expandDepth: 'all', summary } });
  },
};