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

검색하고 걸러도 페이지가 알아서 맞춰져요Search and filter, and the pages adjust themselves

상품 이름을 검색하거나 지역으로 걸러 보세요. 조건에 맞는 행만으로 페이지가 다시 나뉘고, 조건을 바꾸면 늘 1페이지로 돌아가요. 정렬도 걸러진 결과 전체를 기준으로 해요.Search product names or filter by region. Pages are rebuilt from the matching rows only, and any change sends you back to page 1. Sorting covers the whole filtered result.

활용Intermediate
직접 써 보기Live preview검색하고 걸러도 페이지가 알아서 맞춰져요Search and filter, and the pages adjust themselves

코드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="page3">3페이지로</button>
    <input type="text" aria-label="상품 검색" placeholder="예: 무선" id="query" />
    <select aria-label="지역" id="region">
      <option value="all" selected>전체</option>
      <option value="r1">서울</option>
      <option value="r2">부산</option>
      <option value="r3">대구</option>
      <option value="r4">제주</option>
    </select>
  </div>
  <div id="grid" style="height: 520px"></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('page3').addEventListener('click', () => actions.page3(api));
document.getElementById('query').addEventListener('input', (event) => actions.query(api, event.target.value));
document.getElementById('region').addEventListener('change', (event) => actions.region(api, event.target.value));
example.js
const customers = ['한빛물류', '푸른하늘식품', '북극성무역', '다온상사', '새벽농원', '모아패션'];
const regions = ['서울', '부산', '대구', '제주'];
const products = ['무선 이어폰', '무선 키보드', '스마트 워치', '노트북 거치대', '보조배터리'];
export const options = {
  columns: [
    { id: 'customer', title: '고객사', width: 130 },
    { id: 'region', title: '지역', width: 90 },
    { id: 'product', title: '상품', width: 160 },
    {
      id: 'amount',
      title: '금액(원)',
      kind: 'number',
      width: 130,
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('ko-KR'),
    },
  ],
  rows: Array.from({ length: 120 }, (_, i) => ({
    id: i + 1,
    customer: customers[i % 6],
    region: regions[(i * 3) % 4],
    product: products[(i * 7) % 5],
    amount: 29000 + ((i * 7919) % 480000),
  })),
  rowKey: 'id',
  showRowNumbers: true,
  pagination: { mode: 'client', pageSize: 10 },
};
// 검색 글자와 지역 선택을 모아 필터 하나(둘 다 만족)로 만들어요. 페이징은 걸러진 결과 전체를 기준으로 나눠요.
const state = { query: '', region: 'all' };
function apply(api) {
  const items = [];
  if (state.query)
    items.push({ columnId: 'product', operator: 'contains', value: state.query });
  if (state.region !== 'all')
    items.push({ columnId: 'region', operator: 'equals', value: state.region });
  api.setFilterModel(items.length ? { match: 'all', items } : null);
}
export function setup(api, log) {
  state.query = '';
  state.region = 'all';
  const off = api.on('pageChanged', ({ page, pageCount, totalRows, reason }) => {
    const labels = {
      filter: ['필터가 바뀌어', 'Filter changed'],
      sort: ['정렬이 바뀌어', 'Sort changed'],
      user: ['이동', 'Moved'],
      api: ['이동', 'Moved'],
      size: ['개수 변경', 'Page size changed'],
    };
    const why = labels[reason] ?? ['이동', 'Moved'];
    log({
      ko: `${why[0]} ${page} / ${pageCount} 페이지 · 조건에 맞는 ${totalRows}건`,
      en: `${why[1]}: page ${page} / ${pageCount} · ${totalRows} matching rows`,
    });
  });
  return () => off();
}
// 조작 칸의 값은 언어와 상관없는 키라서, 키를 표의 값으로 바꿔 써요.
const REGION_BY_KEY = { r1: '서울', r2: '부산', r3: '대구', r4: '제주' };
export const actions = {
  query: (api, value) => {
    state.query = (value ?? '').trim();
    apply(api);
  },
  region: (api, value) => {
    state.region = value && value !== 'all' ? (REGION_BY_KEY[value] ?? 'all') : 'all';
    apply(api);
  },
  page3: (api) => api.setPage(3),
};

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="page3">Go to page 3</button>
    <input
      type="text"
      aria-label="Product search"
      placeholder="e.g. Wireless"
      id="query"
    />
    <select aria-label="Region" id="region">
      <option value="all" selected>All</option>
      <option value="r1">New York</option>
      <option value="r2">Boston</option>
      <option value="r3">Austin</option>
      <option value="r4">Seattle</option>
    </select>
  </div>
  <div id="grid" style="height: 520px"></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('page3').addEventListener('click', () => actions.page3(api));
document.getElementById('query').addEventListener('input', (event) => actions.query(api, event.target.value));
document.getElementById('region').addEventListener('change', (event) => actions.region(api, event.target.value));
example.js
const customers = [
  'Harbor Logistics',
  'Blue Sky Foods',
  'Polaris Trading',
  'Dawn Commerce',
  'Sunrise Farm',
  'Moda Fashion',
];
const regions = ['New York', 'Boston', 'Austin', 'Seattle'];
const products = [
  'Wireless earbuds',
  'Wireless keyboard',
  'Smart watch',
  'Laptop stand',
  'Power bank',
];
export const options = {
  columns: [
    { id: 'customer', title: 'Customer', width: 150 },
    { id: 'region', title: 'Region', width: 100 },
    { id: 'product', title: 'Product', width: 170 },
    {
      id: 'amount',
      title: 'Amount ($)',
      kind: 'number',
      width: 130,
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('en-US'),
    },
  ],
  rows: Array.from({ length: 120 }, (_, i) => ({
    id: i + 1,
    customer: customers[i % 6],
    region: regions[(i * 3) % 4],
    product: products[(i * 7) % 5],
    amount: 29 + Math.round(((i * 7919) % 480000) / 1000),
  })),
  rowKey: 'id',
  showRowNumbers: true,
  pagination: { mode: 'client', pageSize: 10 },
};
// Combine the search text and the region choice into one filter (both must match). Paging splits the whole filtered result.
const state = { query: '', region: 'all' };
function apply(api) {
  const items = [];
  if (state.query)
    items.push({ columnId: 'product', operator: 'contains', value: state.query });
  if (state.region !== 'all')
    items.push({ columnId: 'region', operator: 'equals', value: state.region });
  api.setFilterModel(items.length ? { match: 'all', items } : null);
}
export function setup(api, log) {
  state.query = '';
  state.region = 'all';
  const off = api.on('pageChanged', ({ page, pageCount, totalRows, reason }) => {
    const labels = {
      filter: 'Filter changed',
      sort: 'Sort changed',
      user: 'Moved',
      api: 'Moved',
      size: 'Page size changed',
    };
    const why = labels[reason] ?? 'Moved';
    log(`${why}: page ${page} / ${pageCount} · ${totalRows} matching rows`);
  });
  return () => off();
}
// The control values are language-neutral keys; map them to the values in the table.
const REGION_BY_KEY = {
  r1: 'New York',
  r2: 'Boston',
  r3: 'Austin',
  r4: 'Seattle',
};
export const actions = {
  query: (api, value) => {
    state.query = (value ?? '').trim();
    apply(api);
  },
  region: (api, value) => {
    state.region = value && value !== 'all' ? (REGION_BY_KEY[value] ?? 'all') : 'all';
    apply(api);
  },
  page3: (api) => api.setPage(3),
};