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

날짜를 연 > 월로 묶기Group dates by year > month

날짜 컬럼의 groupBy에 ['year', 'month']처럼 굵은 단위부터 배열을 주면, 그 컬럼으로 묶을 때 2026 > 11월 같은 중첩 그룹이 만들어져요. 그룹은 시간 순서로 놓이고 합계도 단계마다 나와요.Give a date column groupBy: ['year', 'month'] (coarse to fine) and grouping by it creates nested groups such as 2026 > November. Groups are in chronological order and show totals at every level.

입문Beginner
직접 써 보기Live preview날짜를 연 > 월로 묶기Group dates by year > month

코드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="unit">
      <option value="ym" selected>연 > 월</option>
      <option value="yq">연 > 분기 > 월</option>
      <option value="year">연</option>
    </select>
    <button type="button" id="expandAll">모두 펼치기</button>
    <button type="button" id="collapseAll">모두 접기</button>
  </div>
  <div id="grid" style="height: 320px"></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('unit').addEventListener('change', (event) => actions.unit(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));
example.js
const customers = ['한빛상사', '새봄유통', '늘푸른식품', '바다물산', '하늘전자'];
// 2025-01-03부터 2026년 말까지 흩뿌린 주문이에요(날짜 순서가 아니라 섞여 있어요).
const rows = Array.from({ length: 72 }, (_, i) => {
  const day = (i * 29) % 700;
  const date = new Date(2025, 0, 3 + day);
  return {
    id: i + 1,
    customer: customers[i % customers.length],
    date: `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`,
    amount: 20 + ((i * 37) % 180),
  };
});
// 날짜 컬럼에 groupBy 배열을 주면 그 컬럼으로 묶을 때 연 > 월처럼 중첩 그룹이 만들어져요. 그룹 값은 2026, 2026-11처럼 안정적인 글자이고 시간 순서로 놓여요.
export const options = {
  columns: [
    { id: 'customer', title: '고객', width: 150 },
    { id: 'date', title: '주문일', kind: 'date', width: 150, groupBy: ['year', 'month'] },
    { id: 'amount', title: '금액 ($)', kind: 'number', width: 120, align: 'end', aggregate: 'sum' },
  ],
  rows,
  rowKey: 'id',
  grouping: { by: ['date'], expandDepth: 1 },
};
export function setup(_api, log) {
  log({
    ko: '날짜 컬럼이 연 > 월로 묶였어요. 그룹을 펼쳐 보고 "묶음 단위"를 바꿔 보세요.',
    en: 'The date column is grouped year > month. Expand a group and change "Grouping unit".',
  });
  return () => undefined;
}
export const actions = {
  unit: (api, value) => {
    const by = (value === 'yq'
      ? ['year', 'quarter', 'month']
      : value === 'year'
        ? ['year']
        : ['year', 'month']);
    api.updateColumn('date', { groupBy: by });
  },
  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="Grouping unit" id="unit">
      <option value="ym" selected>Year > month</option>
      <option value="yq">Year > quarter > month</option>
      <option value="year">Year</option>
    </select>
    <button type="button" id="expandAll">Expand all</button>
    <button type="button" id="collapseAll">Collapse all</button>
  </div>
  <div id="grid" style="height: 320px"></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('unit').addEventListener('change', (event) => actions.unit(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));
example.js
const customers = [
  'Harbor Trading',
  'Spring Goods',
  'Evergreen Foods',
  'Ocean Supply',
  'Skyline Tech',
];
// Orders scattered from 2025-01-03 to the end of 2026, not in date order.
const rows = Array.from({ length: 72 }, (_, i) => {
  const day = (i * 29) % 700;
  const date = new Date(2025, 0, 3 + day);
  return {
    id: i + 1,
    customer: customers[i % customers.length],
    date: `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`,
    amount: 20 + ((i * 37) % 180),
  };
});
// A groupBy array on a date column makes nested groups (year > month) when you group by that column. Group values are stable strings such as 2026 and 2026-11, in chronological order.
export const options = {
  columns: [
    { id: 'customer', title: 'Customer', width: 150 },
    { id: 'date', title: 'Order date', kind: 'date', width: 150, groupBy: ['year', 'month'] },
    {
      id: 'amount',
      title: 'Amount ($)',
      kind: 'number',
      width: 120,
      align: 'end',
      aggregate: 'sum',
    },
  ],
  rows,
  rowKey: 'id',
  grouping: { by: ['date'], expandDepth: 1 },
};
export function setup(_api, log) {
  log('The date column is grouped year > month. Expand a group and change "Grouping unit".');
  return () => undefined;
}
export const actions = {
  unit: (api, value) => {
    const by = (value === 'yq'
      ? ['year', 'quarter', 'month']
      : value === 'year'
        ? ['year']
        : ['year', 'month']);
    api.updateColumn('date', { groupBy: by });
  },
  expandAll: (api) => api.expandAll(),
  collapseAll: (api) => api.collapseAll(),
};