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

맨 아래에 합계와 평균 두 줄 붙이기Show two rows at the bottom: total and average

summary에 rows를 주면 맨 아래에 줄이 여러 개 생겨요. 줄마다 이름과 "어느 컬럼을 어떻게 계산할지"를 따로 적어요.Give summary a rows list and several rows appear at the bottom. Each row has its own label and says which columns to calculate and how.

입문Beginner
직접 써 보기Live preview맨 아래에 합계와 평균 두 줄 붙이기Show two rows at the bottom: total and average

코드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="position">
      <option value="bottom" selected>맨 아래</option>
      <option value="top">맨 위</option>
    </select>
  </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('position').addEventListener('change', (event) => actions.position(api, event.target.value));
example.js
const branches = ['서울 본점', '강남점', '부산점', '대구점', '인천점', '광주점'];
// 단위는 백만원이에요. 지점마다 1~3월 매출이 있어요.
const sales = (i, k) => 20 + ((i * 13 + k * 7) % 70);
// 합계는 정수 그대로, 평균은 소수 첫째 자리까지만 보여 줘요(47.833 -> 47.8).
const aggregateText = (value) => Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 1 });
const months = ['m1', 'm2', 'm3'];
// rows의 줄마다 이름(label)과 "어느 컬럼을 어떻게 계산할지"(aggregate)를 적어요.
// aggregate에 적은 컬럼만 계산하고, 적지 않은 컬럼은 빈 칸이에요.
const summary = (position) => ({
  position,
  rows: [
    { id: 'sum', label: '합계', aggregate: { m1: 'sum', m2: 'sum', m3: 'sum' } },
    { id: 'avg', label: '평균', aggregate: { m1: 'avg', m2: 'avg', m3: 'avg' } },
  ],
});
export const options = {
  columns: [
    { id: 'branch', title: '지점', width: 120 },
    ...months.map((id, k) => ({
      id,
      title: `${k + 1}월`,
      kind: 'number',
      width: 90,
      align: 'end',
      aggregateFormat: aggregateText,
    })),
  ],
  rows: branches.map((branch, i) => ({
    id: i + 1,
    branch,
    m1: sales(i, 1),
    m2: sales(i, 2),
    m3: sales(i, 3),
  })),
  rowKey: 'id',
  summary: summary('bottom'),
};
export const actions = {
  position: (api, value) => api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
};

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="Row position" id="position">
      <option value="bottom" selected>Bottom</option>
      <option value="top">Top</option>
    </select>
  </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('position').addEventListener('change', (event) => actions.position(api, event.target.value));
example.js
const branches = ['Seoul HQ', 'Gangnam', 'Busan', 'Daegu', 'Incheon', 'Gwangju'];
// Units are $ thousands. Each branch has sales for January to March.
const sales = (i, k) => 20 + ((i * 13 + k * 7) % 70);
// The total is shown as an integer, the average to one decimal place (47.833 -> 47.8).
const aggregateText = (value) => Number(value).toLocaleString('en-US', { maximumFractionDigits: 1 });
const months = ['m1', 'm2', 'm3'];
// Each entry in rows gets a name (label) and "which column to calculate and how" (aggregate).
// Only the columns listed in aggregate are calculated; the others are left blank.
const summary = (position) => ({
  position,
  rows: [
    { id: 'sum', label: 'Total', aggregate: { m1: 'sum', m2: 'sum', m3: 'sum' } },
    { id: 'avg', label: 'Average', aggregate: { m1: 'avg', m2: 'avg', m3: 'avg' } },
  ],
});
export const options = {
  columns: [
    { id: 'branch', title: 'Branch', width: 120 },
    ...months.map((id, k) => ({
      id,
      title: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'][k],
      kind: 'number',
      width: 90,
      align: 'end',
      aggregateFormat: aggregateText,
    })),
  ],
  rows: branches.map((branch, i) => ({
    id: i + 1,
    branch,
    m1: sales(i, 1),
    m2: sales(i, 2),
    m3: sales(i, 3),
  })),
  rowKey: 'id',
  summary: summary('bottom'),
};
export const actions = {
  position: (api, value) => api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
};