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

100만 건도 서버 페이징으로 가볍게One million rows with server paging

서버 페이징에서는 그리드가 전체 건수만 알고, 요청한 페이지의 행만 받아요. 100만 건이어도 브라우저에는 한 번에 한 페이지만 있어서 빠르고, 새 요청이 오면 이전 요청은 중단돼요.With server paging the grid only knows the total count and receives just the requested page. A million rows stay light because the browser holds one page at a time, and a new request aborts the previous one.

심화Advanced
직접 써 보기Live preview100만 건도 서버 페이징으로 가볍게One million rows with server paging

코드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="first">첫 페이지로</button>
    <button type="button" id="middle">중간 페이지로</button>
    <button type="button" id="last">맨 끝으로</button>
  </div>
  <div id="grid" style="height: 840px"></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('first').addEventListener('click', () => actions.first(api));
document.getElementById('middle').addEventListener('click', () => actions.middle(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
example.js
const TOTAL = 1_000_000;
// 100만 행은 브라우저에 오지 않아요. 서버가 요청받은 페이지만 보내 주고, 그리드는 전체 건수만 알고 있어요.
function fetchPage({ page, pageSize, signal }) {
  return new Promise((resolve, reject) => {
    const timer = setTimeout(() => {
      const first = (page - 1) * pageSize;
      const count = Math.max(0, Math.min(pageSize, TOTAL - first));
      resolve({
        rows: Array.from({ length: count }, (_, i) => ({
          id: first + i + 1,
          orderNo: `SO-${String(first + i + 1).padStart(7, '0')}`,
          amount: (((first + i) * 7919) % 100000) * 10,
        })),
        totalRows: TOTAL,
      });
    }, 150);
    // 새 요청이 오면 이전 요청은 중단돼요.
    signal.addEventListener('abort', () => {
      clearTimeout(timer);
      reject(new DOMException('aborted', 'AbortError'));
    });
  });
}
export const options = {
  showRowNumbers: true,
  columns: [
    { id: 'orderNo', title: '주문번호', width: 150 },
    {
      id: 'amount',
      title: '금액(원)',
      kind: 'number',
      width: 130,
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('ko-KR'),
    },
  ],
  rows: [],
  rowKey: 'id',
  pagination: {
    mode: 'server',
    pageSize: 20,
    pageSizes: [20, 50, 100],
    jump: true,
    loadPage: fetchPage,
  },
};
export function setup(api, log) {
  return api.on('pageChanged', ({ page, pageCount, totalRows }) => {
    log({
      ko: `${pageCount.toLocaleString('ko-KR')}페이지 중 ${page.toLocaleString('ko-KR')}페이지 (전체 ${totalRows.toLocaleString('ko-KR')}행)`,
      en: `Page ${page.toLocaleString('en-US')} of ${pageCount.toLocaleString('en-US')} (${totalRows.toLocaleString('en-US')} rows in total)`,
    });
  });
}
export const actions = {
  first: (api) => api.setPage(1),
  middle: (api) => api.setPage(Math.ceil(TOTAL / api.getPageSize() / 2)),
  last: (api) => api.setPage(Math.ceil(TOTAL / api.getPageSize())),
};

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="first">First page</button>
    <button type="button" id="middle">Middle page</button>
    <button type="button" id="last">Last page</button>
  </div>
  <div id="grid" style="height: 840px"></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('first').addEventListener('click', () => actions.first(api));
document.getElementById('middle').addEventListener('click', () => actions.middle(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
example.js
const TOTAL = 1_000_000;
// The 1,000,000 rows never reach the browser. The server sends only the requested page, and the grid knows just the total count.
function fetchPage({ page, pageSize, signal }) {
  return new Promise((resolve, reject) => {
    const timer = setTimeout(() => {
      const first = (page - 1) * pageSize;
      const count = Math.max(0, Math.min(pageSize, TOTAL - first));
      resolve({
        rows: Array.from({ length: count }, (_, i) => ({
          id: first + i + 1,
          orderNo: `SO-${String(first + i + 1).padStart(7, '0')}`,
          amount: Math.round((((first + i) * 7919) % 100000) / 100),
        })),
        totalRows: TOTAL,
      });
    }, 150);
    // A new request cancels the previous one.
    signal.addEventListener('abort', () => {
      clearTimeout(timer);
      reject(new DOMException('aborted', 'AbortError'));
    });
  });
}
export const options = {
  showRowNumbers: true,
  columns: [
    { id: 'orderNo', title: 'Order no.', width: 150 },
    {
      id: 'amount',
      title: 'Amount ($)',
      kind: 'number',
      width: 130,
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('en-US'),
    },
  ],
  rows: [],
  rowKey: 'id',
  pagination: {
    mode: 'server',
    pageSize: 20,
    pageSizes: [20, 50, 100],
    jump: true,
    loadPage: fetchPage,
  },
};
export function setup(api, log) {
  return api.on('pageChanged', ({ page, pageCount, totalRows }) => {
    log(`Page ${page.toLocaleString('en-US')} of ${pageCount.toLocaleString('en-US')} (${totalRows.toLocaleString('en-US')} rows in total)`);
  });
}
export const actions = {
  first: (api) => api.setPage(1),
  middle: (api) => api.setPage(Math.ceil(TOTAL / api.getPageSize() / 2)),
  last: (api) => api.setPage(Math.ceil(TOTAL / api.getPageSize())),
};