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

그룹 줄의 이름·합계 모양과 펼침 아이콘 바꾸기Format group labels, totals and the toggle icon

묶음 줄에 "결제 완료"처럼 읽기 좋은 이름과 "1,200원" 같은 합계를 보여 주고, 펼침 아이콘을 화살표·폴더·더하기빼기로 바꿔요.Show readable group names and money totals on group rows and switch the toggle icon between arrow, folder and plus/minus.

활용Intermediate
직접 써 보기Live preview그룹 줄의 이름·합계 모양과 펼침 아이콘 바꾸기Format group labels, totals and the toggle icon

코드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="icon">
      <option value="arrow" selected>화살표</option>
      <option value="folder">폴더</option>
      <option value="plusMinus">더하기·빼기</option>
    </select>
    <button type="button" id="expandAll">모두 펼치기</button>
    <button type="button" id="collapseAll">모두 접기</button>
  </div>
  <div id="grid" style="height: 300px"></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('icon').addEventListener('change', (event) => actions.icon(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));
example.js
const statuses = ['paid', 'pending', 'refunded', ''];
const labels = {
  paid: '결제 완료',
  pending: '입금 대기',
  refunded: '환불',
};
const customers = ['한빛푸드', '소나무철물', '파란마켓', '단풍상회'];
const grouping = (toggleIcon) => ({ by: ['status'], expandDepth: 0, toggleIcon });
// 그룹 줄의 이름은 묶은 컬럼의 formatValue로 써요. 같은 함수가 그 컬럼의 일반 칸에도 쓰이니 빈 값도 안전하게 처리해요.
// aggregateFormat은 그룹 줄과 총계 줄에만 적용돼요.
export const options = {
  columns: [
    {
      id: 'status',
      title: '주문 상태',
      width: 200,
      formatValue: (value) => value === '' ? '상태 없음' : (labels[String(value)] ?? String(value)),
    },
    { id: 'order', title: '주문번호', width: 110, aggregate: 'rowCount' },
    { id: 'customer', title: '거래처', width: 120 },
    {
      id: 'amount',
      title: '금액',
      kind: 'number',
      width: 150,
      align: 'end',
      aggregate: 'sum',
      formatValue: (value) => `${Number(value).toLocaleString('ko-KR')}원`,
      aggregateFormat: (value, ctx) => ctx.scope === 'total'
        ? `전체 ${Number(value).toLocaleString('ko-KR')}원`
        : `${Number(value).toLocaleString('ko-KR')}원`,
    },
  ],
  rows: Array.from({ length: 14 }, (_, i) => ({
    id: i + 1,
    status: statuses[i % 4],
    order: `#${1000 + i}`,
    customer: customers[(i + Math.floor(i / 4)) % 4],
    amount: 40000 + ((i * 53) % 210) * 1000 + 250 * (i % 4),
  })),
  rowKey: 'id',
  grouping: grouping('arrow'),
  summary: { label: '모든 주문' },
};
export const actions = {
  icon: (api, value) => api.setOptions({
    grouping: grouping(value === 'folder' || value === 'plusMinus' ? value : 'arrow'),
  }),
  expandAll: (api) => api.expandAll(),
  collapseAll: (api) => api.collapseAll(),
};

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="Toggle icon" id="icon">
      <option value="arrow" selected>Arrow</option>
      <option value="folder">Folder</option>
      <option value="plusMinus">Plus/minus</option>
    </select>
    <button type="button" id="expandAll">Expand all</button>
    <button type="button" id="collapseAll">Collapse all</button>
  </div>
  <div id="grid" style="height: 300px"></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('icon').addEventListener('change', (event) => actions.icon(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));
example.js
const statuses = ['paid', 'pending', 'refunded', ''];
const labels = {
  paid: 'Paid',
  pending: 'Awaiting payment',
  refunded: 'Refunded',
};
const customers = ['Harbor Foods', 'Pine Hardware', 'Blue Market', 'Maple Trading'];
const grouping = (toggleIcon) => ({ by: ['status'], expandDepth: 0, toggleIcon });
// A group row's name comes from the grouped column's formatValue. The same function also formats that column's normal cells, so it handles empty values safely.
// aggregateFormat applies only to group rows and the total row.
export const options = {
  columns: [
    {
      id: 'status',
      title: 'Order status',
      width: 200,
      formatValue: (value) => value === '' ? 'No status' : (labels[String(value)] ?? String(value)),
    },
    { id: 'order', title: 'Order no.', width: 110, aggregate: 'rowCount' },
    { id: 'customer', title: 'Customer', width: 130 },
    {
      id: 'amount',
      title: 'Amount',
      kind: 'number',
      width: 150,
      align: 'end',
      aggregate: 'sum',
      formatValue: (value) => `$${Number(value).toLocaleString('en-US')}`,
      aggregateFormat: (value, ctx) => ctx.scope === 'total'
        ? `Total $${Number(value).toLocaleString('en-US')}`
        : `$${Number(value).toLocaleString('en-US')}`,
    },
  ],
  rows: Array.from({ length: 14 }, (_, i) => ({
    id: i + 1,
    status: statuses[i % 4],
    order: `#${1000 + i}`,
    customer: customers[(i + Math.floor(i / 4)) % 4],
    amount: 40 + ((i * 53) % 210) + (i % 4),
  })),
  rowKey: 'id',
  grouping: grouping('arrow'),
  summary: { label: 'All orders' },
};
export const actions = {
  icon: (api, value) => api.setOptions({
    grouping: grouping(value === 'folder' || value === 'plusMinus' ? value : 'arrow'),
  }),
  expandAll: (api) => api.expandAll(),
  collapseAll: (api) => api.collapseAll(),
};