겹친 조건(묶음 안의 묶음)과 깊이 한도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.
코드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)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<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>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));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),
};<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>겹친 조건(묶음 안의 묶음)과 깊이 한도</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희'];
const cities = ['서울', '부산', '대전'];
const depts = ['개발', '디자인', '영업', '인사'];
// 묶음을 3단계까지만 겹칠 수 있게 줄여 봤어요(기본은 4단계, 최대 6단계). 한도에 닿으면 "+ 묶음" 버튼이 꺼져요.
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;
}
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;
}
const actions = {
sample: (api) => api.setFilterTree(SAMPLE),
open: (api) => void api.openFilterBuilder(),
clear: (api) => api.setFilterTree(null),
};
options.ariaLabel = "겹친 조건(묶음 안의 묶음)과 깊이 한도";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('sample').addEventListener('click', () => actions.sample(api));
document.getElementById('open').addEventListener('click', () => actions.open(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.sample(api)}
>예제 조건 걸기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.open(api)}
>필터 빌더 열기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.clear(api)}
>필터 지우기</button>
</div>
<Grid style={{ height: 500 }} ref={gridRef} options={options} />
</>
);
}import type { FilterGroup, FilterNode, GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
city: string;
dept: string;
salary: number;
}
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희'];
const cities = ['서울', '부산', '대전'];
const depts = ['개발', '디자인', '영업', '인사'];
// 묶음을 3단계까지만 겹칠 수 있게 줄여 봤어요(기본은 4단계, 최대 6단계). 한도에 닿으면 "+ 묶음" 버튼이 꺼져요.
export const options: GridOptions<Row> = {
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] as string,
city: cities[i % 3] as string,
dept: depts[(i * 3) % 4] as string,
salary: 3600 + ((i * 530) % 4600),
})),
rowKey: 'id',
};
// "도시가 서울 이면서, (부서가 개발 이거나 연봉이 7000 이상) 이면서, 아님(연봉 4000 미만)" 같은 겹친 조건이에요.
const SAMPLE: FilterGroup = {
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: string): string =>
({ name: '이름', city: '도시', dept: '부서', salary: '연봉' })[id] ?? id;
const OPS: Record<string, string> = {
equals: '=',
gte: '≥',
lt: '<',
contains: '포함',
gt: '>',
lte: '≤',
};
/** 트리를 "A 그리고 (B 또는 C)" 같은 글로 풀어 써요. */
function describe(node: FilterNode): string {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => api.setFilterTree(SAMPLE),
open: (api: GridApi<Row>) => void api.openFilterBuilder(),
clear: (api: GridApi<Row>) => api.setFilterTree(null),
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.sample(api)">예제 조건 걸기</button>
<button type="button" :disabled="!api" @click="api && actions.open(api)">필터 빌더 열기</button>
<button type="button" :disabled="!api" @click="api && actions.clear(api)">필터 지우기</button>
</div>
<Grid style="height: 500px" :options="options" @ready="onReady" />
</template>import type { FilterGroup, FilterNode, GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
city: string;
dept: string;
salary: number;
}
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희'];
const cities = ['서울', '부산', '대전'];
const depts = ['개발', '디자인', '영업', '인사'];
// 묶음을 3단계까지만 겹칠 수 있게 줄여 봤어요(기본은 4단계, 최대 6단계). 한도에 닿으면 "+ 묶음" 버튼이 꺼져요.
export const options: GridOptions<Row> = {
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] as string,
city: cities[i % 3] as string,
dept: depts[(i * 3) % 4] as string,
salary: 3600 + ((i * 530) % 4600),
})),
rowKey: 'id',
};
// "도시가 서울 이면서, (부서가 개발 이거나 연봉이 7000 이상) 이면서, 아님(연봉 4000 미만)" 같은 겹친 조건이에요.
const SAMPLE: FilterGroup = {
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: string): string =>
({ name: '이름', city: '도시', dept: '부서', salary: '연봉' })[id] ?? id;
const OPS: Record<string, string> = {
equals: '=',
gte: '≥',
lt: '<',
contains: '포함',
gt: '>',
lte: '≤',
};
/** 트리를 "A 그리고 (B 또는 C)" 같은 글로 풀어 써요. */
function describe(node: FilterNode): string {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => api.setFilterTree(SAMPLE),
open: (api: GridApi<Row>) => void api.openFilterBuilder(),
clear: (api: GridApi<Row>) => api.setFilterTree(null),
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.sample(api)"
>예제 조건 걸기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.open(api)"
>필터 빌더 열기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.clear(api)"
>필터 지우기</button>
</div>
<mx-grid style="height: 500px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { FilterGroup, FilterNode, GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
city: string;
dept: string;
salary: number;
}
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희'];
const cities = ['서울', '부산', '대전'];
const depts = ['개발', '디자인', '영업', '인사'];
// 묶음을 3단계까지만 겹칠 수 있게 줄여 봤어요(기본은 4단계, 최대 6단계). 한도에 닿으면 "+ 묶음" 버튼이 꺼져요.
export const options: GridOptions<Row> = {
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] as string,
city: cities[i % 3] as string,
dept: depts[(i * 3) % 4] as string,
salary: 3600 + ((i * 530) % 4600),
})),
rowKey: 'id',
};
// "도시가 서울 이면서, (부서가 개발 이거나 연봉이 7000 이상) 이면서, 아님(연봉 4000 미만)" 같은 겹친 조건이에요.
const SAMPLE: FilterGroup = {
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: string): string =>
({ name: '이름', city: '도시', dept: '부서', salary: '연봉' })[id] ?? id;
const OPS: Record<string, string> = {
equals: '=',
gte: '≥',
lt: '<',
contains: '포함',
gt: '>',
lte: '≤',
};
/** 트리를 "A 그리고 (B 또는 C)" 같은 글로 풀어 써요. */
function describe(node: FilterNode): string {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => api.setFilterTree(SAMPLE),
open: (api: GridApi<Row>) => void api.openFilterBuilder(),
clear: (api: GridApi<Row>) => api.setFilterTree(null),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<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>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));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),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Nested conditions (groups inside groups) and the depth limit</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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.
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;
}
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;
}
const actions = {
sample: (api) => api.setFilterTree(SAMPLE),
open: (api) => void api.openFilterBuilder(),
clear: (api) => api.setFilterTree(null),
};
options.ariaLabel = "Nested conditions (groups inside groups) and the depth limit";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('sample').addEventListener('click', () => actions.sample(api));
document.getElementById('open').addEventListener('click', () => actions.open(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.sample(api)}
>Apply sample</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.open(api)}
>Open filter builder</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.clear(api)}
>Clear filter</button>
</div>
<Grid style={{ height: 500 }} ref={gridRef} options={options} />
</>
);
}import type { FilterGroup, FilterNode, GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
city: string;
dept: string;
salary: number;
}
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: GridOptions<Row> = {
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] as string,
city: cities[i % 3] as string,
dept: depts[(i * 3) % 4] as string,
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: FilterGroup = {
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: string): string =>
({ name: 'Name', city: 'City', dept: 'Dept', salary: 'Salary' })[id] ?? id;
const OPS: Record<string, string> = {
equals: '=',
gte: '≥',
lt: '<',
contains: 'contains',
gt: '>',
lte: '≤',
};
/** Spells a tree out as "A and (B or C)". */
function describe(node: FilterNode): string {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => api.setFilterTree(SAMPLE),
open: (api: GridApi<Row>) => void api.openFilterBuilder(),
clear: (api: GridApi<Row>) => api.setFilterTree(null),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.sample(api)">Apply sample</button>
<button type="button" :disabled="!api" @click="api && actions.open(api)">Open filter builder</button>
<button type="button" :disabled="!api" @click="api && actions.clear(api)">Clear filter</button>
</div>
<Grid style="height: 500px" :options="options" @ready="onReady" />
</template>import type { FilterGroup, FilterNode, GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
city: string;
dept: string;
salary: number;
}
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: GridOptions<Row> = {
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] as string,
city: cities[i % 3] as string,
dept: depts[(i * 3) % 4] as string,
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: FilterGroup = {
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: string): string =>
({ name: 'Name', city: 'City', dept: 'Dept', salary: 'Salary' })[id] ?? id;
const OPS: Record<string, string> = {
equals: '=',
gte: '≥',
lt: '<',
contains: 'contains',
gt: '>',
lte: '≤',
};
/** Spells a tree out as "A and (B or C)". */
function describe(node: FilterNode): string {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => api.setFilterTree(SAMPLE),
open: (api: GridApi<Row>) => void api.openFilterBuilder(),
clear: (api: GridApi<Row>) => api.setFilterTree(null),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.sample(api)"
>Apply sample</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.open(api)"
>Open filter builder</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.clear(api)"
>Clear filter</button>
</div>
<mx-grid style="height: 500px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { FilterGroup, FilterNode, GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
city: string;
dept: string;
salary: number;
}
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: GridOptions<Row> = {
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] as string,
city: cities[i % 3] as string,
dept: depts[(i * 3) % 4] as string,
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: FilterGroup = {
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: string): string =>
({ name: 'Name', city: 'City', dept: 'Dept', salary: 'Salary' })[id] ?? id;
const OPS: Record<string, string> = {
equals: '=',
gte: '≥',
lt: '<',
contains: 'contains',
gt: '>',
lte: '≤',
};
/** Spells a tree out as "A and (B or C)". */
function describe(node: FilterNode): string {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => api.setFilterTree(SAMPLE),
open: (api: GridApi<Row>) => void api.openFilterBuilder(),
clear: (api: GridApi<Row>) => api.setFilterTree(null),
};