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

페이지마다 합계 줄과 강조 색이 따라와요Totals and highlights follow you across pages

페이지를 넘겨도 맨 아래 합계 줄은 전체 합계로 늘 붙어 있고, 서울 행이나 노트북 칸 같은 강조도 페이지마다 그대로 적용돼요. 이 페이지만의 합계는 기록 창에 따로 보여 줘요.A total row stays at the bottom of every page and shows the grand total, and row and cell highlights apply on each page. The current page's own sum appears in the log.

활용Intermediate
직접 써 보기Live preview페이지마다 합계 줄과 강조 색이 따라와요Totals and highlights follow you across pages

코드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="style">
      <option value="on" selected>켜기</option>
      <option value="off">끄기</option>
    </select>
  </div>
  <div id="grid" style="height: 680px"></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('style').addEventListener('change', (event) => actions.style(api, event.target.value));
example.js
const regions = ['서울', '부산', '대구', '제주'];
const products = ['노트북', '모니터', '무선 키보드', '웹캠', '도킹 스테이션'];
let styled = true;
// 페이지를 나눠도 합계 줄, 행 스타일, 칸 스타일이 그대로 따라와요.
//  - 합계 줄은 모든 페이지의 합계를 한 줄로 아래에 붙여요(필터를 걸면 걸러진 행만의 합계예요).
//  - rowClass / cellClass는 보이는 줄마다 불려서 조건에 맞는 곳에 클래스를 붙여요.
export const options = {
  columns: [
    { id: 'region', title: '지역', width: 110 },
    {
      id: 'product',
      title: '상품',
      width: 150,
      cellClass: (ctx) => (styled && ctx.value === '노트북' ? 'pf-product' : undefined),
    },
    { id: 'quantity', title: '수량', kind: 'number', width: 90, align: 'end', aggregate: 'sum' },
    {
      id: 'amount',
      title: '금액(원)',
      kind: 'number',
      width: 140,
      align: 'end',
      aggregate: 'sum',
      formatValue: (value) => Number(value).toLocaleString('ko-KR'),
      cellClass: () => (styled ? 'pf-amount' : undefined),
    },
  ],
  rows: Array.from({ length: 60 }, (_, i) => ({
    id: i + 1,
    region: regions[(i * 3) % 4],
    product: products[(i * 7) % 5],
    quantity: 1 + ((i * 5) % 9),
    amount: 59000 + ((i * 47000) % 900000),
  })),
  rowKey: 'id',
  rowClass: (row) => (styled && row.region === '서울' ? 'pf-seoul' : undefined),
  summary: { position: 'bottom', label: '전체 합계' },
  pagination: { mode: 'client', pageSize: 10 },
};
// 클래스의 색은 테마 색으로 만들어서 밝은 화면과 어두운 화면 모두에서 읽혀요.
const CSS = `
.pf-seoul { box-shadow: inset 3px 0 0 var(--mxg-accent); }
.pf-product { color: var(--mxg-tone-danger-fg); font-weight: 700; }
.pf-amount { color: var(--mxg-tone-info-fg); }
`;
let style;
export function setup(api, log) {
  styled = true;
  style = document.createElement('style');
  style.textContent = CSS;
  document.head.append(style);
  const report = () => {
    const pageSum = api.getVisibleRows().reduce((sum, row) => sum + row.amount, 0);
    log({
      ko: `${api.getPage()}페이지 금액 합계 ${pageSum.toLocaleString('ko-KR')}원 · 전체 ${Number(api.getSummary().amount).toLocaleString('ko-KR')}원`,
      en: `Page ${api.getPage()} amount total ${pageSum.toLocaleString('en-US')} KRW · all pages ${Number(api.getSummary().amount).toLocaleString('en-US')} KRW`,
    });
  };
  report();
  const off = api.on('pageChanged', report);
  return () => {
    off();
    style?.remove();
    style = undefined;
  };
}
export const actions = {
  style: (api, value) => {
    styled = value !== 'off';
    api.refresh();
  },
};

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="Highlights" id="style">
      <option value="on" selected>On</option>
      <option value="off">Off</option>
    </select>
  </div>
  <div id="grid" style="height: 680px"></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('style').addEventListener('change', (event) => actions.style(api, event.target.value));
example.js
const regions = ['New York', 'Boston', 'Austin', 'Seattle'];
const products = ['Laptop', 'Monitor', 'Wireless keyboard', 'Webcam', 'Docking station'];
let styled = true;
// Splitting into pages keeps the total row, row styles and cell styles working.
//  - The total row attaches one line below with the totals of all pages (with a filter, the total of the filtered rows only).
//  - rowClass / cellClass are called for every visible line and add a class where the condition matches.
export const options = {
  columns: [
    { id: 'region', title: 'Region', width: 110 },
    {
      id: 'product',
      title: 'Product',
      width: 170,
      cellClass: (ctx) => (styled && ctx.value === 'Laptop' ? 'pf-product' : undefined),
    },
    { id: 'quantity', title: 'Qty', kind: 'number', width: 90, align: 'end', aggregate: 'sum' },
    {
      id: 'amount',
      title: 'Amount ($)',
      kind: 'number',
      width: 140,
      align: 'end',
      aggregate: 'sum',
      formatValue: (value) => Number(value).toLocaleString('en-US'),
      cellClass: () => (styled ? 'pf-amount' : undefined),
    },
  ],
  rows: Array.from({ length: 60 }, (_, i) => ({
    id: i + 1,
    region: regions[(i * 3) % 4],
    product: products[(i * 7) % 5],
    quantity: 1 + ((i * 5) % 9),
    amount: 59 + ((i * 47) % 900),
  })),
  rowKey: 'id',
  rowClass: (row) => (styled && row.region === 'New York' ? 'pf-seoul' : undefined),
  summary: { position: 'bottom', label: 'Grand total' },
  pagination: { mode: 'client', pageSize: 10 },
};
// The class colors come from theme colors, so they stay readable on both light and dark screens.
const CSS = `
.pf-seoul { box-shadow: inset 3px 0 0 var(--mxg-accent); }
.pf-product { color: var(--mxg-tone-danger-fg); font-weight: 700; }
.pf-amount { color: var(--mxg-tone-info-fg); }
`;
let style;
export function setup(api, log) {
  styled = true;
  style = document.createElement('style');
  style.textContent = CSS;
  document.head.append(style);
  const report = () => {
    const pageSum = api.getVisibleRows().reduce((sum, row) => sum + row.amount, 0);
    log(`Page ${api.getPage()} amount total $${pageSum.toLocaleString('en-US')} · all pages $${Number(api.getSummary().amount).toLocaleString('en-US')}`);
  };
  report();
  const off = api.on('pageChanged', report);
  return () => {
    off();
    style?.remove();
    style = undefined;
  };
}
export const actions = {
  style: (api, value) => {
    styled = value !== 'off';
    api.refresh();
  },
};