Header filter row
A row of inputs under the headers, one per column. Type in a box and the filter applies; a column that is being filtered gets a colored box with a thick bottom border and a funnel button at its right end. The funnel clears only that column's filter and leaves the others; the × in the leftmost cell clears all filters. The row is not a new way of filtering. It is a screen for setFilterModel: a filter set from code shows in the boxes, and a filter typed in a box is what getFilterModel() returns.
Turn it on
const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Name' },
{ id: 'salary', title: 'Salary', kind: 'number' },
],
rows,
rowKey: 'id',
filterRow: true,
};| Value | Meaning |
|---|---|
false | No filter row (default) |
true | An input for every column that can be filtered |
'auto' | The row shows only if at least one column has a filter setting |
A column with filterable: false or filter: false gets an empty cell. The row is as tall as headerHeight and stays pinned with the header when you scroll vertically. With the row on, the body is one line shorter, and aria-rowindex and aria-rowcount grow by one.
With the modular bundle, register the feature. The all-in-one bundle and the React, Vue and Angular wrappers already include it.
import { createGrid } from 'maxgrid/core';
import { filter } from 'maxgrid/filter';
createGrid(el, { columns, rows, rowKey: 'id', filterRow: true, features: [filter] });Passing filterRow or a column filter setting without the feature fails at creation with an invalid-option error that names the missing feature.
Choose the input per column
The input type follows the column kind. Use filter.type to change it.
| kind | Input | Condition it makes |
|---|---|---|
text, omitted, custom | Text box | contains by default; filter.operator for startsWith, endsWith, equals |
number | Number expression box | See the table below |
date | Date expression box | See the table below; compares calendar days |
boolean | Select (All, Yes, No) | equals true or false |
select | Checklist button | in |
columns: [
{ id: 'name', filter: { operator: 'startsWith', placeholder: 'Starts with' } },
{
id: 'status',
kind: 'select',
filter: { options: [{ value: 'on', label: 'Enabled' }, { value: null }] },
},
{ id: 'memo', filter: false }, // no box
];filter setting | Meaning |
|---|---|
type | 'text', 'number', 'date', 'time', 'datetime', 'month', 'week', 'boolean' or 'set' |
operator | Only when the type is text. Default 'contains' |
placeholder | Hint shown in the empty box |
debounce | Milliseconds to wait after typing. 0 to 150, default 120; anything else is invalid-option |
options | Choices for the checklist ({ value, label? }). Without it, the values come from the rows |
Numbers and dates take expressions
Number and date boxes take a short expression so one box can express every comparison. Spaces are ignored.
| Input | Meaning |
|---|---|
5, =5 | Equal |
!=5, <>5 | Not equal |
>5 >=5 <5 <=5 | Greater, at least, less, at most |
5..9 | 5 to 9, both ends included |
5.., ..9 | At least 5, at most 9 |
- | Only empty values |
For dates, write the value as 2024-03-01. Dates compare as calendar days in the local time zone, so any time on that day counts as the day. An expression that cannot be read, or a date that does not exist (2024-02-30), is not applied; the box gets aria-invalid and a hint, and the filter that was already active stays.
Checklist
The cell of a kind: 'select' column is a button. Press it to open a checklist; each check applies one in condition right away, and unchecking everything removes it. A search box above the list narrows the values, and a list of more than 500 values shows the first 500. If the rows are not all loaded, as with server-side filtering, give the values with filter.options. An empty value is { value: null }.
The list closes when you press outside it, press Esc or scroll the grid. Closing with Esc returns focus to the button.
Working with the filter model
- Changing a box replaces only that column's conditions in the model. Conditions on other columns stay, even ones set from code.
- Everything the row makes is AND. If the model was
match: 'any', it becomes'all'after a box changes. - When
setFilterModelchanges the model, the boxes follow. A box you are typing in is left alone if it still matches what the row last applied, and overwritten if code changed it. - A condition a box cannot show (for example
gton a text column) appears asFilter set. Typing in the box replaces it. - The × clear-all button sits in the row-number or checkbox cell if there is one, and otherwise at the right end of the first column's cell. It clears every condition, including ones set from code, and is disabled when nothing is filtered.
filterChangedfires once when a box changes, exactly as it does forsetFilterModel.
When you also have a search box, set filter: false on the column the search box owns, and have the search box replace only its own column's condition instead of the whole model.
The model is plain JSON, so you can save and restore it. Restoring fills the boxes too.
Attach your own filter input
Give a column filterAdapter and your own input replaces the filter cell and the filter popup body for that column (1.3.6). It is a separate key from filter (the built-in input settings).
{
id: 'price',
filterAdapter: () => {
let input: HTMLInputElement;
return {
mount(el, { columnId, getModel, setModel }) {
input = Object.assign(document.createElement('input'), { type: 'range', min: 0, max: 5000 });
input.addEventListener('input', () =>
setModel({ columnId, operator: 'gte', value: Number(input.value) }),
);
el.append(input);
},
destroy() {},
focus: () => input.focus(),
};
},
}- The adapter writes one filter condition (
FilterItem) of this column withsetModel(item)and reads it withgetModel(). It is the same filter model, so server mode andgetViewState()stay serializable. Passingnullclears the condition. - It is mounted inside the cell in the filter row and in the body of the filter popup. The popup applies live, so it has no apply button.
- If
mountordestroythrows, it is reported ascallback-errorand the cell stays empty.destroy()is called when the filter row or popup is redrawn or the grid is destroyed. - When the model changes from outside (clear all and so on) the adapter listens to
filterChangedand updates itself.
Change the value the filter sees
By default the filter looks at the value getValue returns. To filter by something other than the visible text, or to filter object values, give the column the options below. Sorting, export and display keep using getValue.
| Option | Value | Effect |
|---|---|---|
filterValue | ({ row, value, rowKey, columnId }) => value | The value the filter test and the value list see; return a string, number, boolean, Date or null |
valueKey | (value) => string | A key for object values; the same key groups together and becomes one line in the value list |
valueEquals | (a, b) => boolean | Whether two values are equal, used by the no-change check of updateRows and by change tracking (default deepEqual) |
{ id: 'hired', kind: 'date', filter: { type: 'set' },
filterValue: ({ value }) => `${value.getMonth() + 1}`, // a value list by month
{ id: 'owner', filter: { type: 'set' }, valueKey: (v) => `u${v.id}`,
formatValue: (v) => v.label }, // object values grouped by id- For a column with
filterValue,filter.typedecides how it is filtered ('set'means text). Without it, date and time columns are filtered as text and other columns by their column kind. - If
filterValuereturns anything other than a string, number, boolean,Dateornull, it is compared as text. The text of the value list is the valuefilterValuemade. - With only
valueKey, the value list text is the visible cell text (theformatValueresult). The filter model holds the key text. - If a callback throws, or
valueKeydoes not return a string, it is reported through theerrorevent (callback-error) and the original value or the default key is used. filterValueandvalueKeyare called once per row for each filter pass. Keep them cheap.
Choose how dates are typed
The date, time and datetime filter inputs use typing and a picker button together by default. Change that with the column's filter.dateInput.
| Value | Effect |
|---|---|
omitted or 'picker' | Typing and a picker button (default) |
'text' | Typing only (no picker button) |
It applies to the filter row and to the date conditions in the filter popup. Giving it to a column that is not a date kind is an invalid-option.
Korean input and keyboard
The boxes read the value from the input event, so Korean text filters while it is still being composed (ㅁ, 모, 목) after the debounce (120 ms by default, 150 ms at most). They do not wait for composition to end (Sorting and filtering).
| Key or action | Behavior |
|---|---|
| Enter | Applies the pending input now |
| Esc | Clears that box and applies now |
| Enter or Esc while composing | Ignored: those keys confirm or cancel the composition |
| Leaving the box | Applies the pending input |
| Tab | Header, then the filter boxes in order, then the body |
Keys typed in a filter box do not reach the grid's move and select keys. Each box has an aria-label (Filter {column}) and the row has role="row".
Filter on the server
With server paging (pagination.mode: 'server') the filter typed in the boxes goes to loadPage as query.filter. One request goes out when typing pauses, and the rows are replaced when the response arrives. Give the checklist its choices with filter.options.
Other settings
- Messages (
filterMessages) default to Korean or English bylocale, and you can override some of them. - Colors use the existing tokens such as
--mxg-bg-header,--mxg-border,--mxg-accentand--mxg-control-*. Light, dark and the three densities need no extra setup. - With 24 or more columns the body draws only the visible columns, but the filter row builds every cell once, so the box you are typing in survives horizontal scrolling.
- Moving or resizing columns moves the row with them, and rebuilding it keeps the focus and cursor position.
Limits
- The row height is fixed to
headerHeight. - Number boxes take expressions only (
>=100,10..20). Date and time boxes get a calendar or clock button (from 1.2.0) and still accept expressions. - A text box has one operator per column. For a choice of operators, build your own screen and call
setFilterModel. - The popup style, opened with a funnel button in each header, is in Column filter popup and sort marks.