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

필터를 이름 붙여 저장하고 다시 쓰기Save a filter under a name and reuse it

필터 팝업 아래쪽 '프리셋' 영역에서 지금 필터를 이름 붙여 저장하고, 목록에서 골라 다시 적용하거나 이름을 바꾸고 지울 수 있어요. 처음 목록은 '개발팀만'과 '연봉 6,000 이상' 두 개예요.The Presets area at the bottom of the filter popup saves the current filter under a name, then applies, renames or deletes it from a list. It starts with two presets: 'Dev only' and 'Salary $60,000+'.

입문Beginner
직접 써 보기Live preview필터를 이름 붙여 저장하고 다시 쓰기Save a filter under a name and reuse it

코드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="save">현재 필터를 '내 필터'로 저장</button>
    <button type="button" id="applyFirst">첫 프리셋 적용</button>
    <button type="button" id="rename">첫 프리셋 이름 바꾸기</button>
    <button type="button" id="dev">코드로 걸기: 개발팀만</button>
    <button type="button" id="clear">필터 지우기</button>
  </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('save').addEventListener('click', () => actions.save(api));
document.getElementById('applyFirst').addEventListener('click', () => actions.applyFirst(api));
document.getElementById('rename').addEventListener('click', () => actions.rename(api));
document.getElementById('dev').addEventListener('click', () => actions.dev(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
example.js
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희', '오세훈', '윤지아'];
const depts = ['개발팀', '디자인팀', '영업팀', '인사팀', null];
// filterPresets에 처음 목록(items)을 주면 팝업 아래쪽 "프리셋" 영역에 나와요. storageKey가 없으면 메모리에만 있어요.
export const options = {
  filterMenu: true,
  filterPresets: {
    items: [
      {
        id: 'seed-dev',
        name: '개발팀만',
        model: { match: 'all', items: [{ columnId: 'dept', operator: 'equals', value: '개발팀' }] },
        createdAt: 1,
      },
      {
        id: 'seed-high',
        name: '연봉 6,000 이상',
        model: { match: 'all', items: [{ columnId: 'salary', operator: 'gte', value: 6000 }] },
        createdAt: 2,
      },
    ],
  },
  columns: [
    { id: 'name', title: '이름', width: 120 },
    { id: 'dept', title: '부서', width: 130 },
    {
      id: 'salary',
      title: '연봉(만원)',
      kind: 'number',
      width: 160,
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('ko-KR'),
    },
    {
      id: 'hired',
      title: '입사일',
      kind: 'date',
      width: 130,
      formatValue: (value) => (value instanceof Date ? value.toISOString().slice(0, 10) : ''),
    },
  ],
  rows: Array.from({ length: 24 }, (_, i) => ({
    id: i + 1,
    name: names[i % 8],
    dept: depts[(i * 3) % 5] ?? null,
    salary: 3600 + ((i * 530) % 4600),
    hired: new Date(2019 + (i % 6), (i * 5) % 12, 3 + ((i * 7) % 24)),
  })),
  rowKey: 'id',
};
export function setup(api, log) {
  const offList = api.on('filterPresetsChanged', ({ presets }) => {
    const list = presets.map((p) => p.name).join(', ');
    log({ ko: `프리셋 ${presets.length}개: ${list}`, en: `${presets.length} presets: ${list}` });
  });
  const offApplied = api.on('filterPresetApplied', ({ preset }) => {
    log({
      ko: `프리셋 "${preset.name}" 적용 · ${api.getVisibleRowCount()}행 보임`,
      en: `Preset "${preset.name}" applied · ${api.getVisibleRowCount()} rows visible`,
    });
  });
  return () => {
    offList();
    offApplied();
  };
}
export const actions = {
  // 지금 걸린 필터를 이름을 붙여 저장해요. 같은 이름이면 덮어써요.
  save: (api) => api.saveFilterPreset('내 필터'),
  applyFirst: (api) => {
    const first = api.getFilterPresets()[0];
    if (first)
      api.applyFilterPreset(first.id);
  },
  rename: (api) => {
    const first = api.getFilterPresets()[0];
    if (first)
      api.renameFilterPreset(first.id, `${first.name} (수정)`.slice(0, 40));
  },
  clear: (api) => api.setFilterModel(null),
  dev: (api) => api.setFilterModel({
    match: 'all',
    items: [{ columnId: 'dept', operator: 'equals', value: '개발팀' }],
  }),
};

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="save">Save current filter as 'My filter'</button>
    <button type="button" id="applyFirst">Apply first preset</button>
    <button type="button" id="rename">Rename first preset</button>
    <button type="button" id="dev">Set in code: Dev only</button>
    <button type="button" id="clear">Clear filter</button>
  </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('save').addEventListener('click', () => actions.save(api));
document.getElementById('applyFirst').addEventListener('click', () => actions.applyFirst(api));
document.getElementById('rename').addEventListener('click', () => actions.rename(api));
document.getElementById('dev').addEventListener('click', () => actions.dev(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
example.js
const names = [
  'Liam Carter',
  'Emma Stone',
  'Noah Reed',
  'Olivia Hayes',
  'Ethan Brooks',
  'Sophia Lane',
  'Mason Clark',
  'Ava Morgan',
];
const depts = ['Dev', 'Design', 'Sales', 'HR', null];
// Giving filterPresets an initial list (items) shows it in the "Presets" area at the bottom of the popup. Without storageKey it lives in memory only.
export const options = {
  filterMenu: true,
  filterPresets: {
    items: [
      {
        id: 'seed-dev',
        name: 'Dev only',
        model: { match: 'all', items: [{ columnId: 'dept', operator: 'equals', value: 'Dev' }] },
        createdAt: 1,
      },
      {
        id: 'seed-high',
        name: 'Salary $60,000+',
        model: { match: 'all', items: [{ columnId: 'salary', operator: 'gte', value: 60000 }] },
        createdAt: 2,
      },
    ],
  },
  columns: [
    { id: 'name', title: 'Name', width: 130 },
    { id: 'dept', title: 'Dept', width: 130 },
    {
      id: 'salary',
      title: 'Salary ($)',
      kind: 'number',
      width: 160,
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('en-US'),
    },
    {
      id: 'hired',
      title: 'Hired',
      kind: 'date',
      width: 130,
      formatValue: (value) => (value instanceof Date ? value.toISOString().slice(0, 10) : ''),
    },
  ],
  rows: Array.from({ length: 24 }, (_, i) => ({
    id: i + 1,
    name: names[i % 8],
    dept: depts[(i * 3) % 5] ?? null,
    salary: 36000 + ((i * 5300) % 46000),
    hired: new Date(2019 + (i % 6), (i * 5) % 12, 3 + ((i * 7) % 24)),
  })),
  rowKey: 'id',
};
export function setup(api, log) {
  const offList = api.on('filterPresetsChanged', ({ presets }) => {
    const list = presets.map((p) => p.name).join(', ');
    log(`${presets.length} presets: ${list}`);
  });
  const offApplied = api.on('filterPresetApplied', ({ preset }) => {
    log(`Preset "${preset.name}" applied · ${api.getVisibleRowCount()} rows visible`);
  });
  return () => {
    offList();
    offApplied();
  };
}
export const actions = {
  // Save the current filter under a name. The same name overwrites it.
  save: (api) => api.saveFilterPreset('My filter'),
  applyFirst: (api) => {
    const first = api.getFilterPresets()[0];
    if (first)
      api.applyFilterPreset(first.id);
  },
  rename: (api) => {
    const first = api.getFilterPresets()[0];
    if (first)
      api.renameFilterPreset(first.id, `${first.name} (edited)`.slice(0, 40));
  },
  clear: (api) => api.setFilterModel(null),
  dev: (api) => api.setFilterModel({
    match: 'all',
    items: [{ columnId: 'dept', operator: 'equals', value: 'Dev' }],
  }),
};