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

정렬·필터·묶기 전에 막기Block sort, filter and grouping

beforeSort, beforeFilter, beforeGroup은 모델이 바뀌기 직전에 불려요. false를 돌려주면 취소되고 이벤트도 나가지 않아요. 훅이 받는 source로 화면 조작('user')과 코드 호출('api')을 구분하고, model이나 columns로 바뀔 값을 미리 볼 수 있어요.beforeSort, beforeFilter and beforeGroup run just before the model changes. Returning false cancels it and no event is sent. The source tells a gesture ('user') from a code call ('api'), and model or columns shows the value about to be applied.

활용Intermediate
직접 써 보기Live preview정렬·필터·묶기 전에 막기Block sort, filter and grouping

코드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="toggle">잠금 켜기·끄기</button>
    <button type="button" id="sort">코드로 정렬</button>
    <button type="button" id="filter">코드로 필터</button>
    <button type="button" id="group">코드로 묶기</button>
  </div>
  <div id="grid" style="height: 400px"></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('toggle').addEventListener('click', () => actions.toggle(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('filter').addEventListener('click', () => actions.filter(api));
document.getElementById('group').addEventListener('click', () => actions.group(api));
example.js
const en = document.documentElement.dataset.lang === 'en';
const groups = en ? ['Fruit', 'Veg', 'Dairy'] : ['과일', '채소', '유제품'];
const items = en
  ? ['Apple', 'Pear', 'Carrot', 'Onion', 'Milk', 'Cheese', 'Grape', 'Kale']
  : ['사과', '배', '당근', '양파', '우유', '치즈', '포도', '케일'];
let locked = true;
let say = () => { };
const ask = (ko, enText) => (ctx) => {
  say({
    ko: `${ko} 요청 (${ctx.source === 'user' ? '화면 조작' : '코드'}) → ${locked ? '막음' : '허용'}`,
    en: `${enText} requested (${ctx.source === 'user' ? 'gesture' : 'code'}) -> ${locked ? 'blocked' : 'allowed'}`,
  });
  return !locked;
};
export const options = {
  columns: [
    { id: 'item', title: en ? 'Item' : '품목', width: 120, sortable: true },
    { id: 'group', title: en ? 'Group' : '분류', width: 110, sortable: true, groupable: true },
    { id: 'qty', title: en ? 'Qty' : '수량', kind: 'number', width: 90, sortable: true },
  ],
  rows: items.map((item, i) => ({
    id: i + 1,
    item,
    group: groups[i % 3],
    qty: 10 + i * 3,
  })),
  rowKey: 'id',
  hooks: {
    beforeSort: ask('정렬', 'Sort'),
    beforeFilter: ask('필터', 'Filter'),
    beforeGroup: ask('묶기', 'Grouping'),
  },
};
export function setup(_api, log) {
  say = log;
}
export const actions = {
  toggle: () => {
    locked = !locked;
    say({
      ko: locked ? '잠금: 모두 막아요' : '잠금 해제: 모두 허용해요',
      en: locked ? 'Locked: everything is blocked' : 'Unlocked: everything is allowed',
    });
  },
  sort: (api) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
  filter: (api) => api.setFilterModel({ match: 'all', items: [{ columnId: 'qty', operator: 'gt', value: 20 }] }),
  group: (api) => api.setGroupBy(['group']),
};

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="toggle">Toggle lock</button>
    <button type="button" id="sort">Sort from code</button>
    <button type="button" id="filter">Filter from code</button>
    <button type="button" id="group">Group from code</button>
  </div>
  <div id="grid" style="height: 400px"></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('toggle').addEventListener('click', () => actions.toggle(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('filter').addEventListener('click', () => actions.filter(api));
document.getElementById('group').addEventListener('click', () => actions.group(api));
example.js
const groups = ['Fruit', 'Veg', 'Dairy'];
const items = ['Apple', 'Pear', 'Carrot', 'Onion', 'Milk', 'Cheese', 'Grape', 'Kale'];
let locked = true;
let say = () => { };
const ask = (label) => (ctx) => {
  say(`${label} requested (${ctx.source === 'user' ? 'gesture' : 'code'}) -> ${locked ? 'blocked' : 'allowed'}`);
  return !locked;
};
export const options = {
  columns: [
    { id: 'item', title: 'Item', width: 120, sortable: true },
    { id: 'group', title: 'Group', width: 110, sortable: true, groupable: true },
    { id: 'qty', title: 'Qty', kind: 'number', width: 90, sortable: true },
  ],
  rows: items.map((item, i) => ({
    id: i + 1,
    item,
    group: groups[i % 3],
    qty: 10 + i * 3,
  })),
  rowKey: 'id',
  hooks: {
    beforeSort: ask('Sort'),
    beforeFilter: ask('Filter'),
    beforeGroup: ask('Grouping'),
  },
};
export function setup(_api, log) {
  say = log;
}
export const actions = {
  toggle: () => {
    locked = !locked;
    say(locked ? 'Locked: everything is blocked' : 'Unlocked: everything is allowed');
  },
  sort: (api) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
  filter: (api) => api.setFilterModel({ match: 'all', items: [{ columnId: 'qty', operator: 'gt', value: 20 }] }),
  group: (api) => api.setGroupBy(['group']),
};