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

검색 상자 하나로 모든 컬럼 찾기One search box across all columns

그리드 위의 검색 상자에 입력하면 이름·이메일·근무지·부서를 한꺼번에 찾아요. 대소문자와 악센트(é, ö)를 무시하고, 헤더 아래 필터 행과 같이 써도 서로의 조건을 지우지 않아요.Type in a search box above the grid to search name, email, city and department at once. Case and accents are ignored, and the box works with the header filter row without erasing its conditions.

활용Intermediate
직접 써 보기Live preview검색 상자 하나로 모든 컬럼 찾기One search box across all columns

코드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="검색 (예: jose, 서울)"
      id="search"
    />
  </div>
  <div id="grid" style="height: 440px"></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));
example.js
// 검색은 대소문자와 악센트(é, ñ, ö 등)를 무시해요. 글자를 소문자로 바꾸고 악센트를 떼어 낸 '검색용 글자'를
// 보이지 않는 컬럼(key)에 담아 두고, 입력한 글자도 같은 방식으로 바꿔서 비교해요.
const fold = (text) => text.normalize('NFD').replace(/\p{M}/gu, '').toLocaleLowerCase();
export const options = {
  filterRow: true,
  columns: [
    { id: 'name', title: '이름', width: 130, filter: false },
    { id: 'email', title: '이메일', width: 200, filter: false },
    { id: 'city', title: '근무지', width: 120 },
    { id: 'dept', title: '부서', width: 120 },
    {
      id: 'key',
      title: '검색용',
      hidden: true,
      getValue: (row) => fold(`${row.name} ${row.email} ${row.city} ${row.dept}`),
    },
  ],
  rows: [
    { id: 1, name: '김민준', email: 'minjun.kim@maxmine.com', city: '서울', dept: '개발팀' },
    { id: 2, name: '이서연', email: 'seoyeon.lee@maxmine.com', city: '부산', dept: '디자인팀' },
    {
      id: 3,
      name: 'José García',
      email: 'jose.garcia@maxmine.com',
      city: 'Málaga',
      dept: '영업팀',
    },
    { id: 4, name: '박지호', email: 'jiho.park@maxmine.com', city: '서울', dept: '영업팀' },
    {
      id: 5,
      name: 'Zoë Visser',
      email: 'zoe.visser@maxmine.com',
      city: 'Utrecht',
      dept: '디자인팀',
    },
    { id: 6, name: '최유나', email: 'yuna.choi@maxmine.com', city: '대구', dept: '개발팀' },
    {
      id: 7,
      name: 'Élodie Martin',
      email: 'elodie.martin@maxmine.com',
      city: 'Lyon',
      dept: '개발팀',
    },
    { id: 8, name: 'Jose Smith', email: 'jose.smith@maxmine.com', city: 'Austin', dept: '영업팀' },
  ],
  rowKey: 'id',
};
let timer;
export function setup(api, log) {
  // 검색 칸을 search 종류로 바꿔서 Esc로 비우고, 브라우저의 지우기(x) 버튼을 쓸 수 있게 해요.
  const box = document.getElementById('search');
  if (box instanceof HTMLInputElement)
    box.type = 'search';
  const off = api.on('filterChanged', () => log({
    ko: `${api.getRowCount()}행 중 ${api.getVisibleRowCount()}행`,
    en: `${api.getVisibleRowCount()} of ${api.getRowCount()} rows`,
  }));
  return () => {
    clearTimeout(timer);
    off();
  };
}
// 검색 상자와 헤더 아래 필터 행(근무지·부서)은 같은 필터 모델을 함께 써요. 검색 상자는 'key' 컬럼의 조건만 바꾸고
// 나머지 조건은 그대로 둬서, 둘을 같이 쓰면 두 조건을 모두 만족하는 행만 남아요.
// 입력이 120ms 멈춘 뒤에 걸어서 글자 하나마다 다시 거르지 않아요.
export const actions = {
  search: (api, value) => {
    clearTimeout(timer);
    timer = setTimeout(() => {
      if (api.isDestroyed())
        return;
      const text = fold((value ?? '').trim());
      const rest = (api.getFilterModel()?.items ?? []).filter((i) => i.columnId !== 'key');
      const items = text
        ? [...rest, { columnId: 'key', operator: 'contains', value: text }]
        : rest;
      api.setFilterModel(items.length ? { match: 'all', items } : null);
    }, 120);
  },
};

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="Search"
      placeholder="Search (e.g. jose, New York)"
      id="search"
    />
  </div>
  <div id="grid" style="height: 440px"></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));
example.js
// Search ignores case and accents (é, ñ, ö and so on). A "search text" that is lowercased and stripped of accents
// is kept in a hidden column (key), and the typed text is converted the same way before comparing.
const fold = (text) => text.normalize('NFD').replace(/\p{M}/gu, '').toLocaleLowerCase();
export const options = {
  filterRow: true,
  columns: [
    { id: 'name', title: 'Name', width: 140, filter: false },
    { id: 'email', title: 'Email', width: 220, filter: false },
    { id: 'city', title: 'Office', width: 120 },
    { id: 'dept', title: 'Dept', width: 120 },
    {
      id: 'key',
      title: 'Search text',
      hidden: true,
      getValue: (row) => fold(`${row.name} ${row.email} ${row.city} ${row.dept}`),
    },
  ],
  rows: [
    { id: 1, name: 'Liam Carter', email: 'liam.carter@maxmine.com', city: 'New York', dept: 'Dev' },
    { id: 2, name: 'Emma Stone', email: 'emma.stone@maxmine.com', city: 'Boston', dept: 'Design' },
    {
      id: 3,
      name: 'José García',
      email: 'jose.garcia@maxmine.com',
      city: 'Málaga',
      dept: 'Sales',
    },
    { id: 4, name: 'Noah Kim', email: 'noah.kim@maxmine.com', city: 'New York', dept: 'Sales' },
    {
      id: 5,
      name: 'Zoë Visser',
      email: 'zoe.visser@maxmine.com',
      city: 'Utrecht',
      dept: 'Design',
    },
    { id: 6, name: 'Olivia Park', email: 'olivia.park@maxmine.com', city: 'Seattle', dept: 'Dev' },
    {
      id: 7,
      name: 'Élodie Martin',
      email: 'elodie.martin@maxmine.com',
      city: 'Lyon',
      dept: 'Dev',
    },
    { id: 8, name: 'Jose Smith', email: 'jose.smith@maxmine.com', city: 'Austin', dept: 'Sales' },
  ],
  rowKey: 'id',
};
let timer;
export function setup(api, log) {
  // Turn the search box into a search input so Esc clears it and the browser's clear (x) button works.
  const box = document.getElementById('search');
  if (box instanceof HTMLInputElement)
    box.type = 'search';
  const off = api.on('filterChanged', () => log(`${api.getVisibleRowCount()} of ${api.getRowCount()} rows`));
  return () => {
    clearTimeout(timer);
    off();
  };
}
// The search box and the filter row under the header (Office, Dept) share the same filter model. The search box changes only the 'key' column's
// condition and leaves the rest alone, so using both keeps only rows that satisfy both conditions.
// It applies 120ms after typing stops, so the grid does not refilter on every keystroke.
export const actions = {
  search: (api, value) => {
    clearTimeout(timer);
    timer = setTimeout(() => {
      if (api.isDestroyed())
        return;
      const text = fold((value ?? '').trim());
      const rest = (api.getFilterModel()?.items ?? []).filter((i) => i.columnId !== 'key');
      const items = text
        ? [...rest, { columnId: 'key', operator: 'contains', value: text }]
        : rest;
      api.setFilterModel(items.length ? { match: 'all', items } : null);
    }, 120);
  },
};