Column filter popup and sort marks
Instead of a row of filter inputs under the headers (Header filter row), you can let users filter each column from a popup that opens from a funnel button in the header, as in Excel. The popup only calls setFilterModel and setSortModel for the user, so the model and events are the same as for filters set in code.
Open it in the gallery example Excel-style filter popup.
Try it in the demo below.
Turn it on
Turn on filterMenu. It is off by default. It needs the filter feature bundle (Pick features). The all-in-one bundle (maxgrid) already includes it.
import { createGrid } from 'maxgrid/core';
import { filter } from 'maxgrid/filter';
createGrid(el, {
columns,
rows,
rowKey: 'id',
filterMenu: true,
features: [filter],
});Columns that can be filtered get a funnel button in the header. It stays hidden until you hover or focus the header, and is always visible on touch screens. A filtered column shows a filled funnel with a dot and a thick line under the header. Leave a column out with filter: false.
You can use it together with filterRow. Both read and write the same filter model.
Inside the popup
The popup has two tabs. A column does not use both at once, so applying one clears the other tab's conditions for that column.
| Tab | What it does |
|---|---|
| Conditions | Up to two conditions that fit the column type, joined with and/or. Text, number and date columns offer different conditions |
| Values | Tick the values found in the column. Empty values can be ticked too, and a search box narrows the list |
At the top there are buttons that sort only this column ascending or descending. Sorting on other columns stays as it is.
- The filter applies when you press Apply. Clear removes this column's filter.
- Ticking every value removes the filter instead of adding one. Ticking none cannot be applied and a message appears. Press Clear to remove the filter.
- Date conditions include "is not empty". "Between" asks for a minimum and maximum for numbers, and a start and end date for dates.
- Filters on other columns and the overall
match(all or any) are left as they are.
The values list shows up to 500 values. If there are more, narrow it with search.
Apply right away
With applyOnChange: true, ticking a value applies after a short pause (about 0.2 seconds) without pressing Apply. The conditions tab still needs Apply.
filterMenu: { applyOnChange: true },Size and value list options
Without them the popup looks as it does today. Pass filterMenu as an object and set only what you need.
| Option | Value | Description |
|---|---|---|
width | integer 200-800 (px) | Popup width. It shrinks to fit a narrower screen. Not applied to the small-screen sheet |
height | integer 120-800 (px) | Maximum height of the value list |
blankPosition | 'first', 'last' | Where the (Blanks) entry goes. Leave it out to keep the current place (bottom) |
valueTree | true | Show the value list of date and datetime columns as a year > month > day tree. Other kinds stay flat |
With valueTree, ticking a year or month selects or clears every date under it and shows a mixed state when only some are selected. Searching expands everything that matches. The filter model is the same set of values (an in item) as without the tree.
A column's filter: { menu: false } hides only that column's funnel; its filter row cell stays. Alt+F and openFilterMenu do not open it either. The filter row height is the top-level filterRowHeight option (24-80 px).
filterMenu: { width: 420, height: 300, blankPosition: 'first', valueTree: true },
filterRowHeight: 40,
columns: [{ id: 'memo', filter: { menu: false } }],Try the settings in the gallery example Size the filter popup and shape the value list.
Open and close from code
| Method | What it does |
|---|---|
openFilterMenu(id) | Opens the same popup as pressing the funnel. Returns true if opened, false if the menu is off, the column has no filter or does not exist. Any other popup closes |
closeFilterMenu() | Closes the open popup without applying and returns focus to the funnel |
isFilterMenuOpen() | Whether a popup is open |
A column scrolled out of view is scrolled into view first. filterMenuOpened { columnId } fires on open and filterMenuClosed { columnId, applied } on close; applied tells whether the filter model changed while that popup was open.
Work with one column's filter
| Method | What it does |
|---|---|
getColumnFilter(id) | The conditions of that column. An empty array if there are none |
setColumnFilter(id, items) | Replace only that column's conditions. null or an empty array clears them |
clearColumnFilter(id) | Clear that column's conditions |
isColumnFiltered(id), isFilterActive() | Whether that column, or any column, has a condition |
Other columns' conditions and the overall match stay as they are. Validation and filterChanged are the same as setFilterModel, and nothing fires when nothing changes. An invalid condition fails with invalid-option and leaves the state unchanged. See the gallery example Open the filter popup from code.
What the value list sees
The value list uses the values chosen by the column's filterValue and valueKey (details in Header filter row). The picker button of the date conditions can be dropped with the column's filter.dateInput: 'text'.
Keyboard and small screens
- Select a cell and press Alt+F to open the filter popup for that column.
- Inside the popup Tab cycles and does not leave the popup. Esc closes it without applying and returns focus to the funnel button.
- At 480px wide or less the popup becomes a bottom sheet with larger targets and 16px text.
Change the text
Override popup text with the filterMessages keys that start with menu. Keys you leave out use the locale defaults. {title} stands for the column title and {n} for a number.
filterMessages: { menuTitle: 'Filter {title}' },Sort marks
When a user clicks a header to sort, a thin arrow appears right after the column title. An arrow pointing up means ascending and pointing down means descending. With multi-sort (adding keys with Shift+click) a priority number appears to the left of the arrow, where 1 is the first key compared. The number is decoration. Screen readers get the direction from aria-sort on the header cell and do not read the number.
Setting the sort in code and the sort rules are in Sorting and filtering. You can see the numbers in the gallery example Click a header to sort.