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

겹친 조건(묶음 안의 묶음)과 깊이 한도Nested conditions (groups inside groups) and the depth limit

묶음 안에 묶음을 겹쳐 "A 그리고 (B 또는 C)" 같은 조건을 만들어요. filterBuilder.maxDepth로 겹칠 수 있는 단계(기본 4, 최대 6)를 줄일 수 있고, 한도에 닿으면 "+ 묶음" 버튼이 꺼져요.Nest groups inside groups to build conditions like "A and (B or C)". filterBuilder.maxDepth lowers the number of levels (default 4, maximum 6); at the limit the "+ Group" button is disabled.

심화Advanced
직접 써 보기Live preview겹친 조건(묶음 안의 묶음)과 깊이 한도Nested conditions (groups inside groups) and the depth limit

코드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="sample">예제 조건 걸기</button>
    <button type="button" id="open">필터 빌더 열기</button>
    <button type="button" id="clear">필터 지우기</button>
  </div>
  <div id="grid" style="height: 500px"></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('sample').addEventListener('click', () => actions.sample(api));
document.getElementById('open').addEventListener('click', () => actions.open(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
example.js
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희'];
const cities = ['서울', '부산', '대전'];
const depts = ['개발', '디자인', '영업', '인사'];
// 묶음을 3단계까지만 겹칠 수 있게 줄여 봤어요(기본은 4단계, 최대 6단계). 한도에 닿으면 "+ 묶음" 버튼이 꺼져요.
export const options = {
  filterBuilder: { maxDepth: 3 },
  filterMenu: true,
  columns: [
    { id: 'name', title: '이름', width: 110 },
    { id: 'city', title: '도시', width: 90 },
    { id: 'dept', title: '부서', width: 100 },
    { id: 'salary', title: '연봉(만원)', kind: 'number', width: 130, align: 'end' },
  ],
  rows: Array.from({ length: 30 }, (_, i) => ({
    id: i + 1,
    name: names[i % 6],
    city: cities[i % 3],
    dept: depts[(i * 3) % 4],
    salary: 3600 + ((i * 530) % 4600),
  })),
  rowKey: 'id',
};
// "도시가 서울 이면서, (부서가 개발 이거나 연봉이 7000 이상) 이면서, 아님(연봉 4000 미만)" 같은 겹친 조건이에요.
const SAMPLE = {
  match: 'all',
  children: [
    { columnId: 'city', operator: 'equals', value: '서울' },
    {
      match: 'any',
      children: [
        { columnId: 'dept', operator: 'equals', value: '개발' },
        { columnId: 'salary', operator: 'gte', value: 7000 },
      ],
    },
    { match: 'all', not: true, children: [{ columnId: 'salary', operator: 'lt', value: 4000 }] },
  ],
};
const title = (id) => ({ name: '이름', city: '도시', dept: '부서', salary: '연봉' })[id] ?? id;
const OPS = {
  equals: '=',
  gte: '≥',
  lt: '<',
  contains: '포함',
  gt: '>',
  lte: '≤',
};
/** 트리를 "A 그리고 (B 또는 C)" 같은 글로 풀어 써요. */
function describe(node) {
  if (!('children' in node))
    return `${title(node.columnId)} ${OPS[node.operator] ?? node.operator} ${String(node.value ?? '')}`;
  const text = node.children.map(describe).join(node.match === 'all' ? ' 그리고 ' : ' 또는 ');
  return node.not === true ? `아님(${text})` : node.children.length > 1 ? `(${text})` : text;
}
export function setup(api, log) {
  log({
    ko: '"예제 조건 걸기"로 겹친 조건을 걸고, "필터 빌더 열기"에서 고쳐 보세요.',
    en: 'Press "Apply sample" to set a nested filter, then edit it with "Open filter builder".',
  });
  const off = api.on('filterChanged', () => {
    const tree = api.getFilterTree();
    log({
      ko: `${tree === null ? '필터 없음' : describe(tree)} → ${api.getVisibleRowCount()}행`,
      en: `${tree === null ? 'No filter' : describe(tree)} → ${api.getVisibleRowCount()} rows`,
    });
  });
  return off;
}
export const actions = {
  sample: (api) => api.setFilterTree(SAMPLE),
  open: (api) => void api.openFilterBuilder(),
  clear: (api) => api.setFilterTree(null),
};

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="sample">Apply sample</button>
    <button type="button" id="open">Open filter builder</button>
    <button type="button" id="clear">Clear filter</button>
  </div>
  <div id="grid" style="height: 500px"></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('sample').addEventListener('click', () => actions.sample(api));
document.getElementById('open').addEventListener('click', () => actions.open(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
example.js
const names = [
  'Liam Carter',
  'Emma Stone',
  'Noah Brooks',
  'Olivia Reed',
  'Ethan Hayes',
  'Sophia Lane',
];
const cities = ['Seattle', 'Austin', 'Denver'];
const depts = ['Dev', 'Design', 'Sales', 'HR'];
// Groups can be nested only 3 levels deep here (the default is 4, the maximum 6). At the limit the "+ Group" button is disabled.
export const options = {
  filterBuilder: { maxDepth: 3 },
  filterMenu: true,
  columns: [
    { id: 'name', title: 'Name', width: 130 },
    { id: 'city', title: 'City', width: 100 },
    { id: 'dept', title: 'Dept', width: 100 },
    {
      id: 'salary',
      title: 'Salary (USD)',
      kind: 'number',
      width: 140,
      align: 'end',
      formatValue: (value) => `$${Number(value).toLocaleString('en-US')}`,
    },
  ],
  rows: Array.from({ length: 30 }, (_, i) => ({
    id: i + 1,
    name: names[i % 6],
    city: cities[i % 3],
    dept: depts[(i * 3) % 4],
    salary: 36000 + ((i * 5300) % 46000),
  })),
  rowKey: 'id',
};
// A nested condition: city is Seattle AND (dept is Dev OR salary is at least 70,000) AND NOT (salary below 40,000).
const SAMPLE = {
  match: 'all',
  children: [
    { columnId: 'city', operator: 'equals', value: 'Seattle' },
    {
      match: 'any',
      children: [
        { columnId: 'dept', operator: 'equals', value: 'Dev' },
        { columnId: 'salary', operator: 'gte', value: 70000 },
      ],
    },
    { match: 'all', not: true, children: [{ columnId: 'salary', operator: 'lt', value: 40000 }] },
  ],
};
const title = (id) => ({ name: 'Name', city: 'City', dept: 'Dept', salary: 'Salary' })[id] ?? id;
const OPS = {
  equals: '=',
  gte: '≥',
  lt: '<',
  contains: 'contains',
  gt: '>',
  lte: '≤',
};
/** Spells a tree out as "A and (B or C)". */
function describe(node) {
  if (!('children' in node))
    return `${title(node.columnId)} ${OPS[node.operator] ?? node.operator} ${String(node.value ?? '')}`;
  const text = node.children.map(describe).join(node.match === 'all' ? ' and ' : ' or ');
  return node.not === true ? `not(${text})` : node.children.length > 1 ? `(${text})` : text;
}
export function setup(api, log) {
  log('Press "Apply sample" to set a nested filter, then edit it with "Open filter builder".');
  const off = api.on('filterChanged', () => {
    const tree = api.getFilterTree();
    log(`${tree === null ? 'No filter' : describe(tree)} → ${api.getVisibleRowCount()} rows`);
  });
  return off;
}
export const actions = {
  sample: (api) => api.setFilterTree(SAMPLE),
  open: (api) => void api.openFilterBuilder(),
  clear: (api) => api.setFilterTree(null),
};