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

서버 페이징에서 빠른 필터 보내기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.

활용Intermediate
직접 써 보기Live preview서버 페이징에서 빠른 필터 보내기Send the quick filter to the server

코드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">
    <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>
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('search').addEventListener('input', (event) => actions.search(api, event.target.value));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
example.js
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(),
};

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">
    <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>
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('search').addEventListener('input', (event) => actions.search(api, event.target.value));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
example.js
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(),
};