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.
코드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)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<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>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));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())),
};<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>100만 건도 서버 페이징으로 가볍게</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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'));
});
});
}
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,
},
};
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)`,
});
});
}
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())),
};
options.ariaLabel = "100만 건도 서버 페이징으로 가볍게";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('first').addEventListener('click', () => actions.first(api));
document.getElementById('middle').addEventListener('click', () => actions.middle(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.first(api)}
>첫 페이지로</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.middle(api)}
>중간 페이지로</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.last(api)}
>맨 끝으로</button>
</div>
<Grid style={{ height: 840 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions, PageQuery, PageResult } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
orderNo: string;
amount: number;
}
const TOTAL = 1_000_000;
// 100만 행은 브라우저에 오지 않아요. 서버가 요청받은 페이지만 보내 주고, 그리드는 전체 건수만 알고 있어요.
function fetchPage({ page, pageSize, signal }: PageQuery): Promise<PageResult<Row>> {
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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => api.setPage(1),
middle: (api: GridApi<Row>) => api.setPage(Math.ceil(TOTAL / api.getPageSize() / 2)),
last: (api: GridApi<Row>) => api.setPage(Math.ceil(TOTAL / api.getPageSize())),
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.first(api)">첫 페이지로</button>
<button type="button" :disabled="!api" @click="api && actions.middle(api)">중간 페이지로</button>
<button type="button" :disabled="!api" @click="api && actions.last(api)">맨 끝으로</button>
</div>
<Grid style="height: 840px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions, PageQuery, PageResult } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
orderNo: string;
amount: number;
}
const TOTAL = 1_000_000;
// 100만 행은 브라우저에 오지 않아요. 서버가 요청받은 페이지만 보내 주고, 그리드는 전체 건수만 알고 있어요.
function fetchPage({ page, pageSize, signal }: PageQuery): Promise<PageResult<Row>> {
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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => api.setPage(1),
middle: (api: GridApi<Row>) => api.setPage(Math.ceil(TOTAL / api.getPageSize() / 2)),
last: (api: GridApi<Row>) => api.setPage(Math.ceil(TOTAL / api.getPageSize())),
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.first(api)"
>첫 페이지로</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.middle(api)"
>중간 페이지로</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.last(api)"
>맨 끝으로</button>
</div>
<mx-grid style="height: 840px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions, PageQuery, PageResult } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
orderNo: string;
amount: number;
}
const TOTAL = 1_000_000;
// 100만 행은 브라우저에 오지 않아요. 서버가 요청받은 페이지만 보내 주고, 그리드는 전체 건수만 알고 있어요.
function fetchPage({ page, pageSize, signal }: PageQuery): Promise<PageResult<Row>> {
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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => api.setPage(1),
middle: (api: GridApi<Row>) => api.setPage(Math.ceil(TOTAL / api.getPageSize() / 2)),
last: (api: GridApi<Row>) => api.setPage(Math.ceil(TOTAL / api.getPageSize())),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<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>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));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())),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>One million rows with server paging</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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'));
});
});
}
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,
},
};
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)`);
});
}
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())),
};
options.ariaLabel = "One million rows with server paging";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('first').addEventListener('click', () => actions.first(api));
document.getElementById('middle').addEventListener('click', () => actions.middle(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.first(api)}
>First page</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.middle(api)}
>Middle page</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.last(api)}
>Last page</button>
</div>
<Grid style={{ height: 840 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions, PageQuery, PageResult } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
orderNo: string;
amount: number;
}
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 }: PageQuery): Promise<PageResult<Row>> {
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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => api.setPage(1),
middle: (api: GridApi<Row>) => api.setPage(Math.ceil(TOTAL / api.getPageSize() / 2)),
last: (api: GridApi<Row>) => api.setPage(Math.ceil(TOTAL / api.getPageSize())),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.first(api)">First page</button>
<button type="button" :disabled="!api" @click="api && actions.middle(api)">Middle page</button>
<button type="button" :disabled="!api" @click="api && actions.last(api)">Last page</button>
</div>
<Grid style="height: 840px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions, PageQuery, PageResult } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
orderNo: string;
amount: number;
}
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 }: PageQuery): Promise<PageResult<Row>> {
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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => api.setPage(1),
middle: (api: GridApi<Row>) => api.setPage(Math.ceil(TOTAL / api.getPageSize() / 2)),
last: (api: GridApi<Row>) => api.setPage(Math.ceil(TOTAL / api.getPageSize())),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.first(api)"
>First page</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.middle(api)"
>Middle page</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.last(api)"
>Last page</button>
</div>
<mx-grid style="height: 840px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions, PageQuery, PageResult } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
orderNo: string;
amount: number;
}
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 }: PageQuery): Promise<PageResult<Row>> {
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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => api.setPage(1),
middle: (api: GridApi<Row>) => api.setPage(Math.ceil(TOTAL / api.getPageSize() / 2)),
last: (api: GridApi<Row>) => api.setPage(Math.ceil(TOTAL / api.getPageSize())),
};