페이지를 넘겨도 선택 유지Keep the selection across pages
선택은 행마다 붙은 고유 번호로 기억해서 다른 페이지로 넘어가도 고른 행이 그대로 남아요. 머리글 체크박스는 지금 보이는 페이지의 행만 다루고, 전체 개수는 기록 창에 알려 줘요.Selection is remembered by each row's unique number, so chosen rows stay chosen when you change page. The header checkbox only covers the visible page, and the log reports the running total.
코드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="prevPage">이전 페이지</button>
<button type="button" id="nextPage">다음 페이지</button>
<button type="button" id="clear">선택 지우기</button>
</div>
<div id="grid" style="height: 500px"></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('prevPage').addEventListener('click', () => actions.prevPage(api));
document.getElementById('nextPage').addEventListener('click', () => actions.nextPage(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));const tasks = [
'견적서 작성',
'계약서 검토',
'샘플 발송',
'시연 준비',
'재고 점검',
'세금계산서 발행',
];
export const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '업무', width: 170 },
{ id: 'status', title: '상태', width: 90 },
],
rows: Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `${tasks[i % 6]} ${Math.floor(i / 6) + 1}`,
status: i % 3 === 0 ? '진행' : '완료',
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
pagination: { mode: 'client', pageSize: 10 },
};
// 선택은 행마다 붙은 고유 번호로 기억해서 페이지를 넘겨도 그대로 남아요. 머리글 체크박스는 지금 보이는 페이지만 다뤄요.
export function setup(api, log) {
const offs = [
api.on('selectionChanged', ({ selectedKeys }) => log({
ko: `전체 ${selectedKeys.length}행 선택`,
en: `${selectedKeys.length} rows selected in all`,
})),
api.on('pageChanged', ({ page }) => log({
ko: `${page}페이지로 이동 · 전체 ${api.getSelectedKeys().length}행 선택 유지`,
en: `Moved to page ${page} · ${api.getSelectedKeys().length} rows still selected in all`,
})),
];
return () => offs.forEach((off) => off());
}
export const actions = {
nextPage: (api) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
prevPage: (api) => api.setPage(Math.max(api.getPage() - 1, 1)),
clear: (api) => api.clearSelection(),
};<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>페이지를 넘겨도 선택 유지</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="prevPage">이전 페이지</button>
<button type="button" id="nextPage">다음 페이지</button>
<button type="button" id="clear">선택 지우기</button>
</div>
<div id="grid" style="height: 500px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const tasks = [
'견적서 작성',
'계약서 검토',
'샘플 발송',
'시연 준비',
'재고 점검',
'세금계산서 발행',
];
const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '업무', width: 170 },
{ id: 'status', title: '상태', width: 90 },
],
rows: Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `${tasks[i % 6]} ${Math.floor(i / 6) + 1}`,
status: i % 3 === 0 ? '진행' : '완료',
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
pagination: { mode: 'client', pageSize: 10 },
};
// 선택은 행마다 붙은 고유 번호로 기억해서 페이지를 넘겨도 그대로 남아요. 머리글 체크박스는 지금 보이는 페이지만 다뤄요.
function setup(api, log) {
const offs = [
api.on('selectionChanged', ({ selectedKeys }) => log({
ko: `전체 ${selectedKeys.length}행 선택`,
en: `${selectedKeys.length} rows selected in all`,
})),
api.on('pageChanged', ({ page }) => log({
ko: `${page}페이지로 이동 · 전체 ${api.getSelectedKeys().length}행 선택 유지`,
en: `Moved to page ${page} · ${api.getSelectedKeys().length} rows still selected in all`,
})),
];
return () => offs.forEach((off) => off());
}
const actions = {
nextPage: (api) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
prevPage: (api) => api.setPage(Math.max(api.getPage() - 1, 1)),
clear: (api) => api.clearSelection(),
};
options.ariaLabel = "페이지를 넘겨도 선택 유지";
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('prevPage').addEventListener('click', () => actions.prevPage(api));
document.getElementById('nextPage').addEventListener('click', () => actions.nextPage(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(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.prevPage(api)}
>이전 페이지</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.nextPage(api)}
>다음 페이지</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.clear(api)}
>선택 지우기</button>
</div>
<Grid style={{ height: 500 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
status: string;
}
const tasks = [
'견적서 작성',
'계약서 검토',
'샘플 발송',
'시연 준비',
'재고 점검',
'세금계산서 발행',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '업무', width: 170 },
{ id: 'status', title: '상태', width: 90 },
],
rows: Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `${tasks[i % 6]} ${Math.floor(i / 6) + 1}`,
status: i % 3 === 0 ? '진행' : '완료',
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
pagination: { mode: 'client', pageSize: 10 },
};
// 선택은 행마다 붙은 고유 번호로 기억해서 페이지를 넘겨도 그대로 남아요. 머리글 체크박스는 지금 보이는 페이지만 다뤄요.
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const offs = [
api.on('selectionChanged', ({ selectedKeys }) =>
log({
ko: `전체 ${selectedKeys.length}행 선택`,
en: `${selectedKeys.length} rows selected in all`,
}),
),
api.on('pageChanged', ({ page }) =>
log({
ko: `${page}페이지로 이동 · 전체 ${api.getSelectedKeys().length}행 선택 유지`,
en: `Moved to page ${page} · ${api.getSelectedKeys().length} rows still selected in all`,
}),
),
];
return () => offs.forEach((off) => off());
}
export const actions = {
nextPage: (api: GridApi<Row>) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
prevPage: (api: GridApi<Row>) => api.setPage(Math.max(api.getPage() - 1, 1)),
clear: (api: GridApi<Row>) => api.clearSelection(),
};버튼을 누르면 이 예제의 코드가 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.prevPage(api)"
>이전 페이지</button>
<button
type="button"
:disabled="!api"
@click="api && actions.nextPage(api)"
>다음 페이지</button>
<button type="button" :disabled="!api" @click="api && actions.clear(api)">선택 지우기</button>
</div>
<Grid style="height: 500px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
status: string;
}
const tasks = [
'견적서 작성',
'계약서 검토',
'샘플 발송',
'시연 준비',
'재고 점검',
'세금계산서 발행',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '업무', width: 170 },
{ id: 'status', title: '상태', width: 90 },
],
rows: Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `${tasks[i % 6]} ${Math.floor(i / 6) + 1}`,
status: i % 3 === 0 ? '진행' : '완료',
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
pagination: { mode: 'client', pageSize: 10 },
};
// 선택은 행마다 붙은 고유 번호로 기억해서 페이지를 넘겨도 그대로 남아요. 머리글 체크박스는 지금 보이는 페이지만 다뤄요.
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const offs = [
api.on('selectionChanged', ({ selectedKeys }) =>
log({
ko: `전체 ${selectedKeys.length}행 선택`,
en: `${selectedKeys.length} rows selected in all`,
}),
),
api.on('pageChanged', ({ page }) =>
log({
ko: `${page}페이지로 이동 · 전체 ${api.getSelectedKeys().length}행 선택 유지`,
en: `Moved to page ${page} · ${api.getSelectedKeys().length} rows still selected in all`,
}),
),
];
return () => offs.forEach((off) => off());
}
export const actions = {
nextPage: (api: GridApi<Row>) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
prevPage: (api: GridApi<Row>) => api.setPage(Math.max(api.getPage() - 1, 1)),
clear: (api: GridApi<Row>) => api.clearSelection(),
};버튼을 누르면 이 예제의 코드가 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.prevPage(api)"
>이전 페이지</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.nextPage(api)"
>다음 페이지</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.clear(api)"
>선택 지우기</button>
</div>
<mx-grid style="height: 500px" [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 } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
status: string;
}
const tasks = [
'견적서 작성',
'계약서 검토',
'샘플 발송',
'시연 준비',
'재고 점검',
'세금계산서 발행',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '업무', width: 170 },
{ id: 'status', title: '상태', width: 90 },
],
rows: Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `${tasks[i % 6]} ${Math.floor(i / 6) + 1}`,
status: i % 3 === 0 ? '진행' : '완료',
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
pagination: { mode: 'client', pageSize: 10 },
};
// 선택은 행마다 붙은 고유 번호로 기억해서 페이지를 넘겨도 그대로 남아요. 머리글 체크박스는 지금 보이는 페이지만 다뤄요.
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const offs = [
api.on('selectionChanged', ({ selectedKeys }) =>
log({
ko: `전체 ${selectedKeys.length}행 선택`,
en: `${selectedKeys.length} rows selected in all`,
}),
),
api.on('pageChanged', ({ page }) =>
log({
ko: `${page}페이지로 이동 · 전체 ${api.getSelectedKeys().length}행 선택 유지`,
en: `Moved to page ${page} · ${api.getSelectedKeys().length} rows still selected in all`,
}),
),
];
return () => offs.forEach((off) => off());
}
export const actions = {
nextPage: (api: GridApi<Row>) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
prevPage: (api: GridApi<Row>) => api.setPage(Math.max(api.getPage() - 1, 1)),
clear: (api: GridApi<Row>) => api.clearSelection(),
};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="prevPage">Previous page</button>
<button type="button" id="nextPage">Next page</button>
<button type="button" id="clear">Clear selection</button>
</div>
<div id="grid" style="height: 500px"></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('prevPage').addEventListener('click', () => actions.prevPage(api));
document.getElementById('nextPage').addEventListener('click', () => actions.nextPage(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));const tasks = [
'Write quote',
'Review contract',
'Ship samples',
'Prepare demo',
'Check inventory',
'Issue invoice',
];
export const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Task', width: 190 },
{ id: 'status', title: 'Status', width: 100 },
],
rows: Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `${tasks[i % 6]} ${Math.floor(i / 6) + 1}`,
status: i % 3 === 0 ? 'In progress' : 'Done',
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
pagination: { mode: 'client', pageSize: 10 },
};
// Selection is remembered by each row's unique key, so it stays when you change pages. The header checkbox only handles the page you see now.
export function setup(api, log) {
const offs = [
api.on('selectionChanged', ({ selectedKeys }) => log(`${selectedKeys.length} rows selected in all`)),
api.on('pageChanged', ({ page }) => log(`Moved to page ${page} · ${api.getSelectedKeys().length} rows still selected in all`)),
];
return () => offs.forEach((off) => off());
}
export const actions = {
nextPage: (api) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
prevPage: (api) => api.setPage(Math.max(api.getPage() - 1, 1)),
clear: (api) => api.clearSelection(),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Keep the selection across pages</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="prevPage">Previous page</button>
<button type="button" id="nextPage">Next page</button>
<button type="button" id="clear">Clear selection</button>
</div>
<div id="grid" style="height: 500px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const tasks = [
'Write quote',
'Review contract',
'Ship samples',
'Prepare demo',
'Check inventory',
'Issue invoice',
];
const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Task', width: 190 },
{ id: 'status', title: 'Status', width: 100 },
],
rows: Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `${tasks[i % 6]} ${Math.floor(i / 6) + 1}`,
status: i % 3 === 0 ? 'In progress' : 'Done',
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
pagination: { mode: 'client', pageSize: 10 },
};
// Selection is remembered by each row's unique key, so it stays when you change pages. The header checkbox only handles the page you see now.
function setup(api, log) {
const offs = [
api.on('selectionChanged', ({ selectedKeys }) => log(`${selectedKeys.length} rows selected in all`)),
api.on('pageChanged', ({ page }) => log(`Moved to page ${page} · ${api.getSelectedKeys().length} rows still selected in all`)),
];
return () => offs.forEach((off) => off());
}
const actions = {
nextPage: (api) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
prevPage: (api) => api.setPage(Math.max(api.getPage() - 1, 1)),
clear: (api) => api.clearSelection(),
};
options.ariaLabel = "Keep the selection across pages";
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('prevPage').addEventListener('click', () => actions.prevPage(api));
document.getElementById('nextPage').addEventListener('click', () => actions.nextPage(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(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.prevPage(api)}
>Previous page</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.nextPage(api)}
>Next page</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.clear(api)}
>Clear selection</button>
</div>
<Grid style={{ height: 500 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
name: string;
status: string;
}
const tasks = [
'Write quote',
'Review contract',
'Ship samples',
'Prepare demo',
'Check inventory',
'Issue invoice',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Task', width: 190 },
{ id: 'status', title: 'Status', width: 100 },
],
rows: Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `${tasks[i % 6]} ${Math.floor(i / 6) + 1}`,
status: i % 3 === 0 ? 'In progress' : 'Done',
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
pagination: { mode: 'client', pageSize: 10 },
};
// Selection is remembered by each row's unique key, so it stays when you change pages. The header checkbox only handles the page you see now.
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const offs = [
api.on('selectionChanged', ({ selectedKeys }) =>
log(`${selectedKeys.length} rows selected in all`),
),
api.on('pageChanged', ({ page }) =>
log(`Moved to page ${page} · ${api.getSelectedKeys().length} rows still selected in all`),
),
];
return () => offs.forEach((off) => off());
}
export const actions = {
nextPage: (api: GridApi<Row>) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
prevPage: (api: GridApi<Row>) => api.setPage(Math.max(api.getPage() - 1, 1)),
clear: (api: GridApi<Row>) => api.clearSelection(),
};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.prevPage(api)"
>Previous page</button>
<button
type="button"
:disabled="!api"
@click="api && actions.nextPage(api)"
>Next page</button>
<button type="button" :disabled="!api" @click="api && actions.clear(api)">Clear selection</button>
</div>
<Grid style="height: 500px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
name: string;
status: string;
}
const tasks = [
'Write quote',
'Review contract',
'Ship samples',
'Prepare demo',
'Check inventory',
'Issue invoice',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Task', width: 190 },
{ id: 'status', title: 'Status', width: 100 },
],
rows: Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `${tasks[i % 6]} ${Math.floor(i / 6) + 1}`,
status: i % 3 === 0 ? 'In progress' : 'Done',
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
pagination: { mode: 'client', pageSize: 10 },
};
// Selection is remembered by each row's unique key, so it stays when you change pages. The header checkbox only handles the page you see now.
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const offs = [
api.on('selectionChanged', ({ selectedKeys }) =>
log(`${selectedKeys.length} rows selected in all`),
),
api.on('pageChanged', ({ page }) =>
log(`Moved to page ${page} · ${api.getSelectedKeys().length} rows still selected in all`),
),
];
return () => offs.forEach((off) => off());
}
export const actions = {
nextPage: (api: GridApi<Row>) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
prevPage: (api: GridApi<Row>) => api.setPage(Math.max(api.getPage() - 1, 1)),
clear: (api: GridApi<Row>) => api.clearSelection(),
};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.prevPage(api)"
>Previous page</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.nextPage(api)"
>Next page</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.clear(api)"
>Clear selection</button>
</div>
<mx-grid style="height: 500px" [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 } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
name: string;
status: string;
}
const tasks = [
'Write quote',
'Review contract',
'Ship samples',
'Prepare demo',
'Check inventory',
'Issue invoice',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Task', width: 190 },
{ id: 'status', title: 'Status', width: 100 },
],
rows: Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `${tasks[i % 6]} ${Math.floor(i / 6) + 1}`,
status: i % 3 === 0 ? 'In progress' : 'Done',
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
pagination: { mode: 'client', pageSize: 10 },
};
// Selection is remembered by each row's unique key, so it stays when you change pages. The header checkbox only handles the page you see now.
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const offs = [
api.on('selectionChanged', ({ selectedKeys }) =>
log(`${selectedKeys.length} rows selected in all`),
),
api.on('pageChanged', ({ page }) =>
log(`Moved to page ${page} · ${api.getSelectedKeys().length} rows still selected in all`),
),
];
return () => offs.forEach((off) => off());
}
export const actions = {
nextPage: (api: GridApi<Row>) => api.setPage(Math.min(api.getPage() + 1, api.getPageCount())),
prevPage: (api: GridApi<Row>) => api.setPage(Math.max(api.getPage() - 1, 1)),
clear: (api: GridApi<Row>) => api.clearSelection(),
};