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

페이지를 나눠도 합계·평균 두 줄은 그대로Two summary rows that stay put across pages

45건을 10건씩 나눠 보여 주면서 맨 아래에 합계와 평균 두 줄을 붙였어요. 두 줄은 이 페이지가 아니라 전체 행으로 계산하고, 페이지를 넘겨도 그 자리에 있어요.45 rows are shown 10 at a time with two summary rows at the bottom: total and average. Both are calculated over all rows, not just this page, and stay in place as you change pages.

입문Beginner
직접 써 보기Live preview페이지를 나눠도 합계·평균 두 줄은 그대로Two summary rows that stay put 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">
    <button type="button" id="next">다음 페이지</button>
    <button type="button" id="last">마지막 페이지</button>
    <select aria-label="줄 위치" id="position">
      <option value="bottom" selected>맨 아래</option>
      <option value="top">맨 위</option>
    </select>
  </div>
  <div id="grid" style="height: 520px"></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('next').addEventListener('click', () => actions.next(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
document.getElementById('position').addEventListener('change', (event) => actions.position(api, event.target.value));
example.js
const products = ['무선 이어폰', '스마트 워치', '노트북 거치대', '보조배터리', '블루투스 스피커'];
const won = (value) => Number(value).toLocaleString('ko-KR', { maximumFractionDigits: 0 });
// 요약 줄 두 개(합계, 평균)는 "페이지에 보이는 행"이 아니라 조건에 맞는 모든 행으로 계산해요.
// 그래서 페이지를 넘겨도 두 줄의 값은 그대로이고, 줄은 늘 표 맨 아래(또는 맨 위)에 붙어 있어요.
const summary = (position) => ({
  position,
  rows: [
    { id: 'sum', label: '전체 합계', aggregate: { quantity: 'sum', amount: 'sum' } },
    { id: 'avg', label: '전체 평균', aggregate: { quantity: 'avg', amount: 'avg' } },
  ],
});
export const options = {
  columns: [
    { id: 'product', title: '상품', width: 170 },
    {
      id: 'quantity',
      title: '수량',
      kind: 'number',
      width: 110,
      align: 'end',
      aggregateFormat: won,
    },
    {
      id: 'amount',
      title: '금액(원)',
      kind: 'number',
      width: 140,
      align: 'end',
      formatValue: won,
      aggregateFormat: won,
    },
  ],
  rows: Array.from({ length: 45 }, (_, i) => ({
    id: i + 1,
    product: products[(i * 3) % 5],
    quantity: 1 + ((i * 7) % 20),
    amount: 10000 + ((i * 7919) % 90000),
  })),
  rowKey: 'id',
  showRowNumbers: true,
  summary: summary('bottom'),
  pagination: { mode: 'client', pageSize: 10 },
};
export function setup(api, log) {
  const off = api.on('pageChanged', ({ page, pageCount }) => {
    const total = api.getSummary().amount;
    log({
      ko: `${page} / ${pageCount} 페이지 · 전체 금액 합계 ${won(total)}`,
      en: `Page ${page} / ${pageCount} · total of all amounts ${won(total)}`,
    });
  });
  return () => off();
}
export const actions = {
  position: (api, value) => api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
  next: (api) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
  last: (api) => api.setPage(api.getPageCount()),
};

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">
    <button type="button" id="next">Next page</button>
    <button type="button" id="last">Last page</button>
    <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: 520px"></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('next').addEventListener('click', () => actions.next(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
document.getElementById('position').addEventListener('change', (event) => actions.position(api, event.target.value));
example.js
const products = [
  'Wireless earbuds',
  'Smart watch',
  'Laptop stand',
  'Power bank',
  'Bluetooth speaker',
];
const won = (value) => Number(value).toLocaleString('en-US', { maximumFractionDigits: 0 });
// The two summary rows (total, average) are computed from all rows that match, not from the rows visible on the page.
// So the values of both rows stay the same as you change pages, and the rows always stick to the bottom (or top) of the table.
const summary = (position) => ({
  position,
  rows: [
    { id: 'sum', label: 'Overall total', aggregate: { quantity: 'sum', amount: 'sum' } },
    { id: 'avg', label: 'Overall average', aggregate: { quantity: 'avg', amount: 'avg' } },
  ],
});
export const options = {
  columns: [
    { id: 'product', title: 'Product', width: 170 },
    {
      id: 'quantity',
      title: 'Qty',
      kind: 'number',
      width: 110,
      align: 'end',
      aggregateFormat: won,
    },
    {
      id: 'amount',
      title: 'Amount ($)',
      kind: 'number',
      width: 140,
      align: 'end',
      formatValue: won,
      aggregateFormat: won,
    },
  ],
  rows: Array.from({ length: 45 }, (_, i) => ({
    id: i + 1,
    product: products[(i * 3) % 5],
    quantity: 1 + ((i * 7) % 20),
    amount: 10 + Math.round(((i * 7919) % 90000) / 1000),
  })),
  rowKey: 'id',
  showRowNumbers: true,
  summary: summary('bottom'),
  pagination: { mode: 'client', pageSize: 10 },
};
export function setup(api, log) {
  const off = api.on('pageChanged', ({ page, pageCount }) => {
    const total = api.getSummary().amount;
    log(`Page ${page} / ${pageCount} · total of all amounts ${won(total)}`);
  });
  return () => off();
}
export const actions = {
  position: (api, value) => api.setOptions({ summary: summary(value === 'top' ? 'top' : 'bottom') }),
  next: (api) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
  last: (api) => api.setPage(api.getPageCount()),
};