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

분기별 교차표: 지역·제품 합계Quarterly cross table: totals by region and product

분기마다 컬럼을 두고 지역(또는 제품)별로 묶어서, 묶음 줄과 맨 아래 전체 줄에 분기별 합계와 연 합계를 보여 줘요.One column per quarter grouped by region (or product), with quarterly and yearly totals on group and total rows.

활용Intermediate
직접 써 보기Live preview분기별 교차표: 지역·제품 합계Quarterly cross table: totals by region and product

코드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="by">
      <option value="region" selected>지역</option>
      <option value="product">제품</option>
      <option value="none">묶지 않기</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, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('by').addEventListener('change', (event) => actions.by(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));
example.js
const products = ['노트북', '모니터', '독'];
const regions = ['서울', '부산', '대전'];
const sum = (row) => row.q1 + row.q2 + row.q3 + row.q4;
// 피벗 기능 없이 만든 교차표예요: 분기마다 컬럼을 하나씩 두고, 모든 컬럼이 그룹 줄과 총계 줄에서 합쳐져요.
// "연 합계"는 줄마다 네 분기를 더한 계산 컬럼이에요.
export const options = {
  columns: [
    { id: 'region', title: '지역', width: 100 },
    { id: 'product', title: '제품', width: 100 },
    ...['q1', 'q2', 'q3', 'q4'].map((id, i) => ({
      id,
      title: `${i + 1}분기`,
      kind: 'number',
      width: 80,
      align: 'end',
      aggregate: 'sum',
    })),
    {
      id: 'year',
      title: '연 합계',
      kind: 'number',
      width: 100,
      align: 'end',
      getValue: sum,
      aggregate: 'sum',
    },
  ],
  rows: regions.flatMap((region, r) => products.map((product, p) => ({
    id: r * 3 + p + 1,
    region,
    product,
    q1: 40 + r * 7 + p * 5,
    q2: 55 + r * 3 + p * 9,
    q3: 48 + r * 11 + p * 4,
    q4: 70 + r * 5 + p * 6,
  }))),
  rowKey: 'id',
  grouping: { by: ['region'], column: 'region', expandDepth: 0 },
  summary: { label: '전체' },
};
export function setup(_api, log) {
  log({
    ko: '지역별 합계가 한눈에 보여요. 지역을 눌러 제품별 줄을 펼쳐 보세요.',
    en: 'Totals by region at a glance. Click a region to expand the product rows.',
  });
}
export const actions = {
  // 그룹 이름이 보이는 칸(column)도 함께 바꿔요. 안 그러면 제품 이름이 "지역" 칸에 나와요.
  by: (api, value) => {
    if (value === 'none')
      return api.setGroupBy([]);
    const id = value === 'product' ? 'product' : 'region';
    api.setOptions({ grouping: { by: [id], column: id, expandDepth: 0 } });
  },
  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="Group by" id="by">
      <option value="region" selected>Region</option>
      <option value="product">Product</option>
      <option value="none">No grouping</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, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('by').addEventListener('change', (event) => actions.by(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));
example.js
const products = ['Laptop', 'Monitor', 'Dock'];
const regions = ['New York', 'Boston', 'Austin'];
const sum = (row) => row.q1 + row.q2 + row.q3 + row.q4;
// A cross-tab built without a pivot feature: one column per quarter, and every column is summed on group rows and the total row.
// "Year total" is a computed column that adds the four quarters of each row.
export const options = {
  columns: [
    { id: 'region', title: 'Region', width: 100 },
    { id: 'product', title: 'Product', width: 100 },
    ...['q1', 'q2', 'q3', 'q4'].map((id, i) => ({
      id,
      title: `Q${i + 1}`,
      kind: 'number',
      width: 80,
      align: 'end',
      aggregate: 'sum',
    })),
    {
      id: 'year',
      title: 'Year total',
      kind: 'number',
      width: 130,
      align: 'end',
      getValue: sum,
      aggregate: 'sum',
    },
  ],
  rows: regions.flatMap((region, r) => products.map((product, p) => ({
    id: r * 3 + p + 1,
    region,
    product,
    q1: 40 + r * 7 + p * 5,
    q2: 55 + r * 3 + p * 9,
    q3: 48 + r * 11 + p * 4,
    q4: 70 + r * 5 + p * 6,
  }))),
  rowKey: 'id',
  grouping: { by: ['region'], column: 'region', expandDepth: 0 },
  summary: { label: 'Total' },
};
export function setup(_api, log) {
  log('Totals by region at a glance. Click a region to expand the product rows.');
}
export const actions = {
  // Also switch the column that shows the group name. Otherwise product names would appear in the "Region" column.
  by: (api, value) => {
    if (value === 'none')
      return api.setGroupBy([]);
    const id = value === 'product' ? 'product' : 'region';
    api.setOptions({ grouping: { by: [id], column: id, expandDepth: 0 } });
  },
  expandAll: (api) => api.expandAll(),
  collapseAll: (api) => api.collapseAll(),
};