Skip to content

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.

ts
createGrid(el, {
  columns,
  rows,
  rowKey: 'id',
  filterMenu: true,
  filterBuilder: true, // or { maxDepth: 3, columns: ['name', 'dept'], button: true }
});
OptionMeaning
maxDepthHow many levels groups can be nested (the top group is 1). 1 to 6, default 4
columnsColumn ids the builder offers. If omitted, every column that can be filtered
buttonWhether 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 Esc closes 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 ​

KeyAction
Tab / Shift+TabCycles inside the dialog only
Enter (value box)Apply
EscCancel 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.

ts
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 not on).
  • A tree with an unknown column, a wrong operator or value, or one that exceeds a limit is rejected with invalid-option before any state changes. Try it in the gallery example Set a filter tree from code.
  • With a tree set, getFilterModel() returns { match, items: [], tree }. items is empty and match follows the top group, so older code that reads the model still makes sense. When tree is present it is the source of truth and columnMatch is not used.
  • Flat models (items) work as before. A flat model passed to setFilterModel removes the tree, and you can also pass a tree inside the model: setFilterModel({ match, items: [], tree }).

Events, hooks and saved state ​

  • filterChanged: { filterModel, tree? }. tree is present only while a tree is set.
  • beforeFilter: { model, tree?, source }. Return false to block it.
  • getState()/setState() (save and restore) and v-model:filterModel (Vue) carry the tree inside 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 filterChanged fires.
  • 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.