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

합계 줄에 원하는 글자 넣기Put your own wording in the total row

합계 줄의 숫자 앞뒤에 "합계", "평균", "곳" 같은 말을 붙이고, 맨 앞 칸의 문구도 바꿀 수 있어요. 컬럼마다 다른 계산을 쓸 수도 있어요.Add words such as "Sum", "Average" or a unit before or after the totals, and change the label in the first cell. Each column can use a different calculation.

입문Beginner
직접 써 보기Live preview합계 줄에 원하는 글자 넣기Put your own wording in the total row

코드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="decorate">
      <option value="on" selected>켜기</option>
      <option value="off">끄기</option>
    </select>
    <input
      type="text"
      aria-label="맨 앞 문구"
      placeholder="맨 앞 문구 (예: ∑ 합계)"
      id="first"
    />
  </div>
  <div id="grid" style="height: 360px"></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('decorate').addEventListener('change', (event) => actions.decorate(api, event.target.value));
document.getElementById('first').addEventListener('input', (event) => actions.first(api, event.target.value));
example.js
const branches = [
  '서울 본점',
  '강남점',
  '부산점',
  '대구점',
  '인천점',
  '광주점',
  '대전점',
  '제주점',
];
const regions = ['수도권', '수도권', '영남', '영남', '수도권', '호남', '충청', '제주'];
// 단위는 백만원이에요. 지점마다 1~6월 매출이 있어요.
const sales = (i, k) => 20 + ((i * 13 + k * 7) % 70);
const rows = branches.map((branch, i) => ({
  id: i + 1,
  branch,
  region: regions[i],
  m1: sales(i, 1),
  m2: sales(i, 2),
  m3: sales(i, 3),
  m4: sales(i, 4),
  m5: sales(i, 5),
  m6: sales(i, 6),
}));
const money = (value) => Number(value).toLocaleString('ko-KR');
// 합계 줄의 글자는 컬럼마다 aggregateFormat으로 정해요. 계산 결과를 받아서 원하는 문장으로 돌려주면 돼요.
// 계산(aggregate)은 그대로 두고 글자만 꾸미는 거라, 앞에 "약", 뒤에 "곳" 같은 말도 붙일 수 있어요.
let decorate = true;
let first = '∑ 합계';
const plain = (value) => Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 1 });
const text = (make) => (value) => decorate ? make(plain(value)) : plain(value);
const columns = () => [
  { id: 'branch', title: '지점', width: 120 },
  {
    id: 'm1',
    title: '1월',
    kind: 'number',
    width: 110,
    align: 'end',
    aggregate: 'sum',
    formatValue: money,
    aggregateFormat: text((v) => `합계 ${v}`),
  },
  {
    id: 'm2',
    title: '2월',
    kind: 'number',
    width: 110,
    align: 'end',
    aggregate: 'avg',
    formatValue: money,
    aggregateFormat: text((v) => `평균 ${v}`),
  },
  {
    id: 'm3',
    title: '3월',
    kind: 'number',
    width: 110,
    align: 'end',
    aggregate: 'max',
    formatValue: money,
    aggregateFormat: text((v) => `최대 ${v}`),
  },
  {
    id: 'm4',
    title: '4월',
    kind: 'number',
    width: 110,
    align: 'end',
    aggregate: 'min',
    formatValue: money,
    aggregateFormat: text((v) => `최소 ${v}`),
  },
  {
    id: 'm5',
    title: '5월',
    kind: 'number',
    width: 110,
    align: 'end',
    aggregate: 'count',
    formatValue: money,
    aggregateFormat: text((v) => `${v}곳`),
  },
  {
    id: 'm6',
    title: '6월',
    kind: 'number',
    width: 130,
    align: 'end',
    aggregate: 'sum',
    formatValue: money,
    aggregateFormat: text((v) => `약 ${v}백만원`),
  },
];
export const options = {
  columns: columns(),
  rows,
  rowKey: 'id',
  summary: { label: first },
};
export const actions = {
  decorate: (api, value) => {
    decorate = value !== 'off';
    api.setColumns(columns());
  },
  // 합계 줄 맨 앞 칸(첫 컬럼)의 문구예요. 비우면 기본 글자("합계")가 나와요.
  first: (api, value) => {
    first = (value ?? '').trim();
    api.setOptions({ summary: first ? { label: first } : true });
  },
};

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="Decorations" id="decorate">
      <option value="on" selected>On</option>
      <option value="off">Off</option>
    </select>
    <input
      type="text"
      aria-label="First label"
      placeholder="First label (e.g. Total)"
      id="first"
    />
  </div>
  <div id="grid" style="height: 360px"></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('decorate').addEventListener('change', (event) => actions.decorate(api, event.target.value));
document.getElementById('first').addEventListener('input', (event) => actions.first(api, event.target.value));
example.js
const branches = ['Seoul HQ', 'Gangnam', 'Busan', 'Daegu', 'Incheon', 'Gwangju', 'Daejeon', 'Jeju'];
const regions = [
  'Capital',
  'Capital',
  'Yeongnam',
  'Yeongnam',
  'Capital',
  'Honam',
  'Chungcheong',
  'Jeju',
];
// Units are $ thousands. Each branch has sales for January to June.
const sales = (i, k) => 20 + ((i * 13 + k * 7) % 70);
const rows = branches.map((branch, i) => ({
  id: i + 1,
  branch,
  region: regions[i],
  m1: sales(i, 1),
  m2: sales(i, 2),
  m3: sales(i, 3),
  m4: sales(i, 4),
  m5: sales(i, 5),
  m6: sales(i, 6),
}));
const money = (value) => Number(value).toLocaleString('en-US');
// The text of the summary row is set per column with aggregateFormat. Take the calculated result and return any sentence you like.
// The calculation (aggregate) stays the same and only the text is decorated, so you can add words such as "about" before or "branches" after.
let decorate = true;
let first = '∑ Total';
const plain = (value) => Number(value).toLocaleString('en-US', { maximumFractionDigits: 1 });
const text = (make) => (value) => decorate ? make(plain(value)) : plain(value);
const columns = () => [
  { id: 'branch', title: 'Branch', width: 120 },
  {
    id: 'm1',
    title: 'Jan',
    kind: 'number',
    width: 110,
    align: 'end',
    aggregate: 'sum',
    formatValue: money,
    aggregateFormat: text((v) => `Total ${v}`),
  },
  {
    id: 'm2',
    title: 'Feb',
    kind: 'number',
    width: 110,
    align: 'end',
    aggregate: 'avg',
    formatValue: money,
    aggregateFormat: text((v) => `Avg ${v}`),
  },
  {
    id: 'm3',
    title: 'Mar',
    kind: 'number',
    width: 110,
    align: 'end',
    aggregate: 'max',
    formatValue: money,
    aggregateFormat: text((v) => `Max ${v}`),
  },
  {
    id: 'm4',
    title: 'Apr',
    kind: 'number',
    width: 110,
    align: 'end',
    aggregate: 'min',
    formatValue: money,
    aggregateFormat: text((v) => `Min ${v}`),
  },
  {
    id: 'm5',
    title: 'May',
    kind: 'number',
    width: 110,
    align: 'end',
    aggregate: 'count',
    formatValue: money,
    aggregateFormat: text((v) => `${v} branches`),
  },
  {
    id: 'm6',
    title: 'Jun',
    kind: 'number',
    width: 130,
    align: 'end',
    aggregate: 'sum',
    formatValue: money,
    aggregateFormat: text((v) => `About $${v}K`),
  },
];
export const options = {
  columns: columns(),
  rows,
  rowKey: 'id',
  summary: { label: first },
};
export const actions = {
  decorate: (api, value) => {
    decorate = value !== 'off';
    api.setColumns(columns());
  },
  // The text of the first cell (first column) of the summary row. If empty, the default text ("Total") is shown.
  first: (api, value) => {
    first = (value ?? '').trim();
    api.setOptions({ summary: first ? { label: first } : true });
  },
};