Filter builder (nested AND / OR / NOT conditions)
Since 1.6.0
The filterBuilder option and setFilterTree, getFilterTree, openFilterBuilder and closeFilterBuilder are available from 1.6.0.
The filter popup and the header filter row allow two conditions per column, so they cannot express something like "Seattle and (Dev or a salary of at least 70000)". The filter builder puts conditions into groups and lets you nest groups inside groups to build such conditions.
Try it in the gallery examples Stack conditions in the filter builder and Nested conditions and the depth limit.
Turn it on
Set filterBuilder. It is off by default. It needs the filter feature bundle (Pick the features you need); the all-in-one bundle (maxgrid) already includes it.
createGrid(el, {
columns,
rows,
rowKey: 'id',
filterMenu: true,
filterBuilder: true, // or { maxDepth: 3, columns: ['name', 'dept'], button: true }
});| Option | Meaning |
|---|---|
maxDepth | How many levels groups can be nested (the top group is 1). 1 to 6, default 4 |
columns | Column ids the builder offers. If omitted, every column that can be filtered |
button | Whether to show the Advanced… button under the filter popup and the Filter builder… column menu item. Default true |
A wrong value throws invalid-option. A filter can have at most 50 conditions in total.
Using the dialog
Open it with Advanced… (under the filter popup), Filter builder… in the column menu, or api.openFilterBuilder() in code. If a filter is already set, its conditions show as the starting point. A flat filter appears as one "All of the following" group.
- The top of the dialog is one group. In the group header choose All of the following (AND) or Any of the following (OR).
- Tick NOT to flip the result of that group.
- + Condition adds a
[column] [operator] [value]row. The operators and the value input follow the column kind (text, number, date, time, boolean), and date and time columns get a picker button. Between has a minimum and a maximum box. - + Group adds an inner group. At the nesting limit (
maxDepth) this button is disabled, and at 50 conditions both add buttons are. - Use the × at the end of a row or group to remove it. Conditions with an empty value are skipped when you apply.
- The filter is set only when you press Apply. A field with a wrong value is marked and the dialog stays open. Clear removes all filters. Cancel or
Esccloses without changing anything.
Conditions this dialog cannot edit, such as a value list (in), show as read-only rows and stay as they are when you apply.
Keyboard and accessibility
| Key | Action |
|---|---|
Tab / Shift+Tab | Cycles inside the dialog only |
Enter (value box) | Apply |
Esc | Cancel and return focus to where you came from |
The dialog has role="dialog". Each group is a role="group" named by its level, such as "Condition group, level 2". Every input and button has a name, and the condition count ("3 conditions") is announced through a role="status" region. The dialog is centered and becomes a bottom sheet on phones (480px and narrower). Dark theme and right-to-left layouts are followed.
Change the text with filterMessages or a language pack (Korean, English, Japanese and Chinese are built in). The keys start with builder.
Using it from code
You can set a tree without the dialog, so no option is needed.
api.setFilterTree({
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 }] },
],
});
api.getFilterTree(); // the tree that is set (a copy); null for a flat filter or no filter
api.setFilterTree(null); // remove the tree- A node is either a condition (
FilterItem:columnId,operator,value) or a group ({ match: 'all' | 'any', not?, children }). Conditions can use every existing operator. - A group with no children does not filter anything (even with
noton). - A tree with an unknown column, a wrong operator or value, or one that exceeds a limit is rejected with
invalid-optionbefore any state changes. Try it in the gallery example Set a filter tree from code. - With a tree set,
getFilterModel()returns{ match, items: [], tree }.itemsis empty andmatchfollows the top group, so older code that reads the model still makes sense. Whentreeis present it is the source of truth andcolumnMatchis not used. - Flat models (
items) work as before. A flat model passed tosetFilterModelremoves the tree, and you can also pass a tree inside the model:setFilterModel({ match, items: [], tree }).
Events, hooks and saved state
filterChanged:{ filterModel, tree? }.treeis present only while a tree is set.beforeFilter:{ model, tree?, source }. Returnfalseto block it.getState()/setState()(save and restore) andv-model:filterModel(Vue) carry thetreeinside the model as it is.- If a column is removed or changed so that a condition no longer fits, that condition and any group left empty are dropped and
filterChangedfires. - A header funnel shows as active for every column that has a condition in the tree.
Using it with column filters
While a tree is set, applying a condition in the column filter popup or the header filter row replaces the tree. The popup shows a note about this.
Server-side filtering
With server paging (pagination.mode: 'server') and infinite server rows, the grid does not filter rows and sends the model to the server. The tree is in PageQuery.filter.tree, so you can send it as JSON. The server walks the tree and turns it into a SQL WHERE: a group all is AND, any is OR, and not is NOT (...). The gallery example Send the filter tree with server paging has a mock server.