서버 페이징에서 빠른 필터 보내기Send the quick filter to the server
서버 페이징에서는 빠른 필터를 그리드가 걸러내지 않고 loadPage 요청의 quickFilter로 보내요. 글자가 바뀌면 다시 요청해요.With server paging the grid does not filter by itself; the quick filter goes in the loadPage request and a change requests again.
코드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">
<input type="text" aria-label="빠른 필터" placeholder="예: 서울" id="search" />
<button type="button" id="reset">지우기</button>
</div>
<div id="grid" style="height: 360px"></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('search').addEventListener('input', (event) => actions.search(api, event.target.value));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));const ALL = Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `상품 ${i + 1}`,
city: ['서울', '부산', '대구'][i % 3],
}));
let say = () => undefined;
// 서버 페이징에서는 빠른 필터를 그리드가 거르지 않고 loadPage 요청의 quickFilter로 서버(여기서는 모의 서버)에 보내요.
async function loadPage(query) {
const quick = query.quickFilter;
say({
ko: `요청: ${query.page}쪽 빠른 필터 ${quick === undefined ? '(없음)' : `"${quick.text}"`}`,
en: `Request: page ${query.page}, quick filter ${quick === undefined ? '(none)' : `"${quick.text}"`}`,
});
const words = (quick?.text ?? '').split(/\s+/).filter((w) => w !== '');
const hit = ALL.filter((r) => words.every((w) => `${r.name} ${r.city}`.includes(w)));
await new Promise((resolve) => setTimeout(resolve, 200));
return {
rows: hit.slice((query.page - 1) * query.pageSize, query.page * query.pageSize),
totalRows: hit.length,
};
}
export const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80 },
{ id: 'name', title: '이름', width: 140 },
{ id: 'city', title: '도시', width: 90 },
],
rows: [],
rowKey: 'id',
pagination: { mode: 'server', pageSize: 8, loadPage },
};
export function setup(_api, log) {
say = log;
return () => {
say = () => undefined;
};
}
export const actions = {
search: (api, value) => api.setQuickFilter(value ?? ''),
reset: (api) => api.resetQuickFilter(),
};<!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">
<input type="text" aria-label="빠른 필터" placeholder="예: 서울" id="search" />
<button type="button" id="reset">지우기</button>
</div>
<div id="grid" style="height: 360px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const ALL = Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `상품 ${i + 1}`,
city: ['서울', '부산', '대구'][i % 3],
}));
let say = () => undefined;
// 서버 페이징에서는 빠른 필터를 그리드가 거르지 않고 loadPage 요청의 quickFilter로 서버(여기서는 모의 서버)에 보내요.
async function loadPage(query) {
const quick = query.quickFilter;
say({
ko: `요청: ${query.page}쪽 빠른 필터 ${quick === undefined ? '(없음)' : `"${quick.text}"`}`,
en: `Request: page ${query.page}, quick filter ${quick === undefined ? '(none)' : `"${quick.text}"`}`,
});
const words = (quick?.text ?? '').split(/\s+/).filter((w) => w !== '');
const hit = ALL.filter((r) => words.every((w) => `${r.name} ${r.city}`.includes(w)));
await new Promise((resolve) => setTimeout(resolve, 200));
return {
rows: hit.slice((query.page - 1) * query.pageSize, query.page * query.pageSize),
totalRows: hit.length,
};
}
const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80 },
{ id: 'name', title: '이름', width: 140 },
{ id: 'city', title: '도시', width: 90 },
],
rows: [],
rowKey: 'id',
pagination: { mode: 'server', pageSize: 8, loadPage },
};
function setup(_api, log) {
say = log;
return () => {
say = () => undefined;
};
}
const actions = {
search: (api, value) => api.setQuickFilter(value ?? ''),
reset: (api) => api.resetQuickFilter(),
};
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('search').addEventListener('input', (event) => actions.search(api, event.target.value));
document.getElementById('reset').addEventListener('click', () => actions.reset(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">
<input
type="text"
aria-label="빠른 필터"
placeholder="예: 서울"
disabled={!api}
onChange={(event) => api && actions.search(api, event.target.value)}
/>
<button
type="button"
disabled={!api}
onClick={() => api && actions.reset(api)}
>지우기</button>
</div>
<Grid style={{ height: 360 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions, PageQuery } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
city: string;
}
const ALL: Row[] = Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `상품 ${i + 1}`,
city: ['서울', '부산', '대구'][i % 3] as string,
}));
let say: (message: LogText) => void = () => undefined;
// 서버 페이징에서는 빠른 필터를 그리드가 거르지 않고 loadPage 요청의 quickFilter로 서버(여기서는 모의 서버)에 보내요.
async function loadPage(query: PageQuery) {
const quick = query.quickFilter;
say({
ko: `요청: ${query.page}쪽 빠른 필터 ${quick === undefined ? '(없음)' : `"${quick.text}"`}`,
en: `Request: page ${query.page}, quick filter ${quick === undefined ? '(none)' : `"${quick.text}"`}`,
});
const words = (quick?.text ?? '').split(/\s+/).filter((w) => w !== '');
const hit = ALL.filter((r) => words.every((w) => `${r.name} ${r.city}`.includes(w)));
await new Promise((resolve) => setTimeout(resolve, 200));
return {
rows: hit.slice((query.page - 1) * query.pageSize, query.page * query.pageSize),
totalRows: hit.length,
};
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80 },
{ id: 'name', title: '이름', width: 140 },
{ id: 'city', title: '도시', width: 90 },
],
rows: [],
rowKey: 'id',
pagination: { mode: 'server', pageSize: 8, loadPage },
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {
say = () => undefined;
};
}
export const actions = {
search: (api: GridApi<Row>, value?: string) => api.setQuickFilter(value ?? ''),
reset: (api: GridApi<Row>) => api.resetQuickFilter(),
};버튼을 누르면 이 예제의 코드가 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">
<input
type="text"
aria-label="빠른 필터"
placeholder="예: 서울"
:disabled="!api"
@input="api && actions.search(api, ($event.target as HTMLInputElement).value)"
/>
<button type="button" :disabled="!api" @click="api && actions.reset(api)">지우기</button>
</div>
<Grid style="height: 360px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions, PageQuery } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
city: string;
}
const ALL: Row[] = Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `상품 ${i + 1}`,
city: ['서울', '부산', '대구'][i % 3] as string,
}));
let say: (message: LogText) => void = () => undefined;
// 서버 페이징에서는 빠른 필터를 그리드가 거르지 않고 loadPage 요청의 quickFilter로 서버(여기서는 모의 서버)에 보내요.
async function loadPage(query: PageQuery) {
const quick = query.quickFilter;
say({
ko: `요청: ${query.page}쪽 빠른 필터 ${quick === undefined ? '(없음)' : `"${quick.text}"`}`,
en: `Request: page ${query.page}, quick filter ${quick === undefined ? '(none)' : `"${quick.text}"`}`,
});
const words = (quick?.text ?? '').split(/\s+/).filter((w) => w !== '');
const hit = ALL.filter((r) => words.every((w) => `${r.name} ${r.city}`.includes(w)));
await new Promise((resolve) => setTimeout(resolve, 200));
return {
rows: hit.slice((query.page - 1) * query.pageSize, query.page * query.pageSize),
totalRows: hit.length,
};
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80 },
{ id: 'name', title: '이름', width: 140 },
{ id: 'city', title: '도시', width: 90 },
],
rows: [],
rowKey: 'id',
pagination: { mode: 'server', pageSize: 8, loadPage },
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {
say = () => undefined;
};
}
export const actions = {
search: (api: GridApi<Row>, value?: string) => api.setQuickFilter(value ?? ''),
reset: (api: GridApi<Row>) => api.resetQuickFilter(),
};버튼을 누르면 이 예제의 코드가 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">
<input
type="text"
aria-label="빠른 필터"
placeholder="예: 서울"
[disabled]="!api"
(input)="api && actions.search(api, $any($event.target).value)"
/>
<button
type="button"
[disabled]="!api"
(click)="api && actions.reset(api)"
>지우기</button>
</div>
<mx-grid style="height: 360px" [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 } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
city: string;
}
const ALL: Row[] = Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `상품 ${i + 1}`,
city: ['서울', '부산', '대구'][i % 3] as string,
}));
let say: (message: LogText) => void = () => undefined;
// 서버 페이징에서는 빠른 필터를 그리드가 거르지 않고 loadPage 요청의 quickFilter로 서버(여기서는 모의 서버)에 보내요.
async function loadPage(query: PageQuery) {
const quick = query.quickFilter;
say({
ko: `요청: ${query.page}쪽 빠른 필터 ${quick === undefined ? '(없음)' : `"${quick.text}"`}`,
en: `Request: page ${query.page}, quick filter ${quick === undefined ? '(none)' : `"${quick.text}"`}`,
});
const words = (quick?.text ?? '').split(/\s+/).filter((w) => w !== '');
const hit = ALL.filter((r) => words.every((w) => `${r.name} ${r.city}`.includes(w)));
await new Promise((resolve) => setTimeout(resolve, 200));
return {
rows: hit.slice((query.page - 1) * query.pageSize, query.page * query.pageSize),
totalRows: hit.length,
};
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80 },
{ id: 'name', title: '이름', width: 140 },
{ id: 'city', title: '도시', width: 90 },
],
rows: [],
rowKey: 'id',
pagination: { mode: 'server', pageSize: 8, loadPage },
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {
say = () => undefined;
};
}
export const actions = {
search: (api: GridApi<Row>, value?: string) => api.setQuickFilter(value ?? ''),
reset: (api: GridApi<Row>) => api.resetQuickFilter(),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div class="toolbar">
<input
type="text"
aria-label="Quick filter"
placeholder="e.g. New York"
id="search"
/>
<button type="button" id="reset">Clear</button>
</div>
<div id="grid" style="height: 360px"></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('search').addEventListener('input', (event) => actions.search(api, event.target.value));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));const ALL = Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `Product ${i + 1}`,
city: ['New York', 'Boston', 'Austin'][i % 3],
}));
let say = () => undefined;
// With server paging, the grid does not filter the quick filter itself. It sends it to the server (a mock server here) as quickFilter in the loadPage request.
async function loadPage(query) {
const quick = query.quickFilter;
say(`Request: page ${query.page}, quick filter ${quick === undefined ? '(none)' : `"${quick.text}"`}`);
const words = (quick?.text ?? '').split(/\s+/).filter((w) => w !== '');
const hit = ALL.filter((r) => words.every((w) => `${r.name} ${r.city}`.includes(w)));
await new Promise((resolve) => setTimeout(resolve, 200));
return {
rows: hit.slice((query.page - 1) * query.pageSize, query.page * query.pageSize),
totalRows: hit.length,
};
}
export const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80 },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'city', title: 'City', width: 110 },
],
rows: [],
rowKey: 'id',
pagination: { mode: 'server', pageSize: 8, loadPage },
};
export function setup(_api, log) {
say = log;
return () => {
say = () => undefined;
};
}
export const actions = {
search: (api, value) => api.setQuickFilter(value ?? ''),
reset: (api) => api.resetQuickFilter(),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Send the quick filter to the server</title>
</head>
<body>
<div class="toolbar">
<input
type="text"
aria-label="Quick filter"
placeholder="e.g. New York"
id="search"
/>
<button type="button" id="reset">Clear</button>
</div>
<div id="grid" style="height: 360px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const ALL = Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `Product ${i + 1}`,
city: ['New York', 'Boston', 'Austin'][i % 3],
}));
let say = () => undefined;
// With server paging, the grid does not filter the quick filter itself. It sends it to the server (a mock server here) as quickFilter in the loadPage request.
async function loadPage(query) {
const quick = query.quickFilter;
say(`Request: page ${query.page}, quick filter ${quick === undefined ? '(none)' : `"${quick.text}"`}`);
const words = (quick?.text ?? '').split(/\s+/).filter((w) => w !== '');
const hit = ALL.filter((r) => words.every((w) => `${r.name} ${r.city}`.includes(w)));
await new Promise((resolve) => setTimeout(resolve, 200));
return {
rows: hit.slice((query.page - 1) * query.pageSize, query.page * query.pageSize),
totalRows: hit.length,
};
}
const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80 },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'city', title: 'City', width: 110 },
],
rows: [],
rowKey: 'id',
pagination: { mode: 'server', pageSize: 8, loadPage },
};
function setup(_api, log) {
say = log;
return () => {
say = () => undefined;
};
}
const actions = {
search: (api, value) => api.setQuickFilter(value ?? ''),
reset: (api) => api.resetQuickFilter(),
};
options.ariaLabel = "Send the quick filter to the server";
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('search').addEventListener('input', (event) => actions.search(api, event.target.value));
document.getElementById('reset').addEventListener('click', () => actions.reset(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">
<input
type="text"
aria-label="Quick filter"
placeholder="e.g. New York"
disabled={!api}
onChange={(event) => api && actions.search(api, event.target.value)}
/>
<button
type="button"
disabled={!api}
onClick={() => api && actions.reset(api)}
>Clear</button>
</div>
<Grid style={{ height: 360 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions, PageQuery } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
name: string;
city: string;
}
const ALL: Row[] = Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `Product ${i + 1}`,
city: ['New York', 'Boston', 'Austin'][i % 3] as string,
}));
let say: (message: LogText) => void = () => undefined;
// With server paging, the grid does not filter the quick filter itself. It sends it to the server (a mock server here) as quickFilter in the loadPage request.
async function loadPage(query: PageQuery) {
const quick = query.quickFilter;
say(
`Request: page ${query.page}, quick filter ${quick === undefined ? '(none)' : `"${quick.text}"`}`,
);
const words = (quick?.text ?? '').split(/\s+/).filter((w) => w !== '');
const hit = ALL.filter((r) => words.every((w) => `${r.name} ${r.city}`.includes(w)));
await new Promise((resolve) => setTimeout(resolve, 200));
return {
rows: hit.slice((query.page - 1) * query.pageSize, query.page * query.pageSize),
totalRows: hit.length,
};
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80 },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'city', title: 'City', width: 110 },
],
rows: [],
rowKey: 'id',
pagination: { mode: 'server', pageSize: 8, loadPage },
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {
say = () => undefined;
};
}
export const actions = {
search: (api: GridApi<Row>, value?: string) => api.setQuickFilter(value ?? ''),
reset: (api: GridApi<Row>) => api.resetQuickFilter(),
};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">
<input
type="text"
aria-label="Quick filter"
placeholder="e.g. New York"
:disabled="!api"
@input="api && actions.search(api, ($event.target as HTMLInputElement).value)"
/>
<button type="button" :disabled="!api" @click="api && actions.reset(api)">Clear</button>
</div>
<Grid style="height: 360px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions, PageQuery } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
name: string;
city: string;
}
const ALL: Row[] = Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `Product ${i + 1}`,
city: ['New York', 'Boston', 'Austin'][i % 3] as string,
}));
let say: (message: LogText) => void = () => undefined;
// With server paging, the grid does not filter the quick filter itself. It sends it to the server (a mock server here) as quickFilter in the loadPage request.
async function loadPage(query: PageQuery) {
const quick = query.quickFilter;
say(
`Request: page ${query.page}, quick filter ${quick === undefined ? '(none)' : `"${quick.text}"`}`,
);
const words = (quick?.text ?? '').split(/\s+/).filter((w) => w !== '');
const hit = ALL.filter((r) => words.every((w) => `${r.name} ${r.city}`.includes(w)));
await new Promise((resolve) => setTimeout(resolve, 200));
return {
rows: hit.slice((query.page - 1) * query.pageSize, query.page * query.pageSize),
totalRows: hit.length,
};
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80 },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'city', title: 'City', width: 110 },
],
rows: [],
rowKey: 'id',
pagination: { mode: 'server', pageSize: 8, loadPage },
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {
say = () => undefined;
};
}
export const actions = {
search: (api: GridApi<Row>, value?: string) => api.setQuickFilter(value ?? ''),
reset: (api: GridApi<Row>) => api.resetQuickFilter(),
};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">
<input
type="text"
aria-label="Quick filter"
placeholder="e.g. New York"
[disabled]="!api"
(input)="api && actions.search(api, $any($event.target).value)"
/>
<button
type="button"
[disabled]="!api"
(click)="api && actions.reset(api)"
>Clear</button>
</div>
<mx-grid style="height: 360px" [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 } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
name: string;
city: string;
}
const ALL: Row[] = Array.from({ length: 60 }, (_, i) => ({
id: i + 1,
name: `Product ${i + 1}`,
city: ['New York', 'Boston', 'Austin'][i % 3] as string,
}));
let say: (message: LogText) => void = () => undefined;
// With server paging, the grid does not filter the quick filter itself. It sends it to the server (a mock server here) as quickFilter in the loadPage request.
async function loadPage(query: PageQuery) {
const quick = query.quickFilter;
say(
`Request: page ${query.page}, quick filter ${quick === undefined ? '(none)' : `"${quick.text}"`}`,
);
const words = (quick?.text ?? '').split(/\s+/).filter((w) => w !== '');
const hit = ALL.filter((r) => words.every((w) => `${r.name} ${r.city}`.includes(w)));
await new Promise((resolve) => setTimeout(resolve, 200));
return {
rows: hit.slice((query.page - 1) * query.pageSize, query.page * query.pageSize),
totalRows: hit.length,
};
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80 },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'city', title: 'City', width: 110 },
],
rows: [],
rowKey: 'id',
pagination: { mode: 'server', pageSize: 8, loadPage },
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {
say = () => undefined;
};
}
export const actions = {
search: (api: GridApi<Row>, value?: string) => api.setQuickFilter(value ?? ''),
reset: (api: GridApi<Row>) => api.resetQuickFilter(),
};