Text filters allow you to filter string data.
The Text Filter is the default filter used in AG Grid Community, but it can also be explicitly configured as shown
below:
columnDefs = [
{
'field': 'athlete',
# Text Filter is used by default in Community version
'filter': True,
'filterParams': {
# pass in additional parameters to the Text Filter
},
},
{
'field': 'country',
# explicitly configure column to use the Text Filter
'filter': 'agTextColumnFilter',
'filterParams': {
# pass in additional parameters to the Text Filter
},
},
]
The example below shows the Text Filter in action:
For the Athlete column:
filterOptions = ['contains', 'notContains']
debounceMs = 200
).maxNumConditions = 1
)For the Country column:
filterOptions = ['contains']
trimInput = True
)debounceMs = 1000
)For the Sport column:
defaultOption = 'startsWith'
)caseSensitive = True
)Text Filters are configured though the filterParams
attribute of the column definition:
buttons
Specifies the buttons to be shown in the filter, in the order they should be displayed in. The options are:'apply'
: If the Apply button is present, the filter is only applied after the user hits the Apply button.'clear'
: The Clear button will clear the (form) details of the filter without removing any active filters on the'reset'
: The Reset button will clear the details of the filter and any active filters on that column.'cancel'
: The Cancel button will discard any changes that have been made to the filter in the UI, restoring thecaseSensitive
(boolean, default: False) By default, text filtering is case-insensitive. Set this to True
to makecloseOnApply
(boolean, default: False) If t Apply button is present, the filter popup will be closed immediatelyTrue
.debounceMs
(number) Overrides the default debounce time in milliseconds for the filter. Defaults are:TextFilter
and NumberFilter
: 500ms. (These filters have text field inputs, so a short delay before the inputDateFilter
and SetFilter
: 0msdefaultJoinOperator
By default, the two conditions are combined using AND
. You can change this default by settingAND
, OR
defaultOption
(string) The default filter option to be selected.filterOptions
Array of filter options to present to the user.filterPlaceholder
Placeholder text for the filter textbox.maxNumConditions
(number, default: 2) Maximum number of conditions allowed in the filter.numAlwaysVisibleConditions
(number, default: 1) By default only one condition is shown, and additional conditionsmaxNumConditions
). To have more conditions shown bymaxNumConditions
- anything larger will be ignored.readOnly
(boolean, default: False) If set to True
, disables controls in the filter to mutate its state. NormallytextFormatter
(Function) Formats the text before applying the filter compare logic. Useful if you want to substitutetextMatcher
(Function) Used to override how to filter based on the user input. Returns True
if the value passesFalse
.trimInput
(boolean, default: False) If True
, the input that the user enters will be trimmed when the filter istrimInput
, it is best to also increase the debounceMs
to give users more time to enter text.By default, the grid compares the text filter with the values in a case-insensitive way, by converting both the filter
text and the values to lower-case and comparing them; for example, ‘o’ will match ‘Olivia’ and ‘Salmon’. If you instead
want to have case-sensitive matches, you can set caseSensitive = True
in the filterParams
, so that no lower-casing
is performed. In this case, ‘o’ would no longer match ‘Olivia’.
You might have more advanced requirements, for example to ignore accented characters. In this case, you can provide your
own textFormatter
(See
the example below).
The Text Formatter is applied to both the filter text and the values before they are compared.
textFormatter
(Function) Formats the text before applying the filter compare logic. Useful if you want to substituteNote that when providing a Text Formatter, the caseSensitive
parameter is ignored. In this situation, if you want to
do a case-insensitive comparison, you will need to perform case conversion inside the textFormatter
function.
In most cases, you can customise the Text Filter matching logic by providing your
own Text Formatter, for example to remove or replace
characters in the filter text and values.
The Text Formatter is applied to both the filter text and values before the filter comparison is performed.
For more advanced use cases, you can provide your own textMatcher
to decide when to include a row in the filtered
results. For example, you might want to apply different logic for the filter option equals
than for contains
.
textMatcher
(Function) Used to override how to filter based on the user input. Returns True
if the value passesFalse
.The following is an example of a textMatcher
that mimics the current implementation of AG Grid. This can be used as a
template to create your own.
This JavaScript function must be added to the dashAgGridFunctions.js
file in the assets folder.
See JavaScript Functions
for more information and
the example below.
var dagfuncs = (window.dashAgGridFunctions = window.dashAgGridFunctions || {});
dagfuncs.customTextMatcher = ({filterOption, value, filterText}) => {
if (filterText == null) {
return false;
}
switch (filterOption) {
case 'contains':
return value.indexOf(filterText) >= 0;
case 'notContains':
return value.indexOf(filterText) < 0;
case 'equals':
return value === filterText;
case 'notEqual':
return value != filterText;
case 'startsWith':
return value.indexOf(filterText) === 0;
case 'endsWith':
const index = value.lastIndexOf(filterText);
return index >= 0 && index === (value.length - filterText.length);
default:
// should never happen
console.warn('invalid filter type ' + filter);
return false;
}
}
See AG Grid Docs for the
available inputs for the textMatcher
function.
This example has all the same features as in
the Text Filter example above, plus the following:
Note that it is possible either to define all the filterParams
parameters in JavaScript, like the column Athlete, or
only define the JavaScript functions for textFormatter
and textMatcher
, like the column Country.
View the JavaScript functions used for this example
These JavaScript functions must be added to the dashAgGridFunctions.js
file in the assets folder.
See JavaScript Functions
for more information.
var dagfuncs = (window.dashAgGridFunctions = window.dashAgGridFunctions || {});
dagfuncs.athleteFilterParams = {
filterOptions: ["contains", "notContains"],
textFormatter: (r) => {
if (r == null) return null;
return r
.toLowerCase()
.replace(/[àáâãäå]/g, "a")
.replace(/æ/g, "ae")
.replace(/ç/g, "c")
.replace(/[èéêë]/g, "e")
.replace(/[ìíîï]/g, "i")
.replace(/ñ/g, "n")
.replace(/[òóôõö]/g, "o")
.replace(/œ/g, "oe")
.replace(/[ùúûü]/g, "u")
.replace(/[ýÿ]/g, "y");
},
debounceMs: 200,
maxNumConditions: 1,
};
function contains(target, lookingFor) {
return target && target.indexOf(lookingFor) >= 0;
}
dagfuncs.countryTextMatcher = ({value, filterText}) => {
const aliases = {
usa: "united states",
holland: "netherlands",
niall: "ireland",
sean: "south africa",
alberto: "mexico",
john: "australia",
xi: "china",
};
const literalMatch = contains(value, filterText || "");
return literalMatch || contains(value, aliases[filterText || ""]);
};
See the Filter Model & Dash Callbacks section for
examples.
The Filter Model describes the current state of the applied Text Filter. If only
one Filter Condition is set, this will
be a TextFilterModel
:
filterType
(‘text’) Filter type is always ‘text’filter
(string or None) The text value associated with the filter. It’s optional as custom filters may not have afilterTo
(string or None) The 2nd text value associated with the filter, if supported.type
One of the filter options: ‘empty’, ‘equals’, ‘notEqual’, ‘lessThan’, ‘lessThanOrEqual’, ‘greaterThan’,If more than one Filter Condition is set, then multiple instances of the model are created and wrapped inside a Combined
Model.
Note that in AG Grid versions prior to 29.2, only two Filter Conditions were supported. These appeared in the Combined
Model as properties condition1
and condition2
. The grid will still accept and supply models using these properties,
but this behaviour is deprecated. The conditions
property should be used instead.
Here is an example of a Filter Model with three conditions:
filterModel = {
"sport": {
"filterType": "text",
"operator": "OR",
"conditions": [
{
"filterType": "text",
"type": "contains",
"filter": "Swim",
},
{
"filterType": "text",
"type": "contains",
"filter": "Ski",
},
{
"filterType": "text",
"type": "contains",
"filter": "Rowing",
},
],
}
}
The Text Filter presents a list
of Filter Options to the user.
The list of options are as follows:
Option Name | Option Key | Included by Default |
---|---|---|
Contains | <p>contains <p> |
Yes |
Does not contain | <p>notContains <p> |
Yes |
Equals | <p>equals <p> |
Yes |
Does not equal | <p>notEqual <p> |
Yes |
Begins with | <p>startsWith <p> |
Yes |
Ends with | <p>endsWith <p> |
Yes |
Blank | <p>blank <p> |
Yes |
Not blank | <p>notBlank <p> |
Yes |
Choose one | <p>empty <p> |
No |
Note that the empty filter option is primarily used when
creating Custom Filter Options.
When ‘Choose one’ is displayed, the filter is not active.
The default option for the Text Filter is contains
.
By default, the values supplied to the Text Filter are retrieved from the data based on the field
attribute. This can
be overridden by providing a filterValueGetter
in the Column Definition. This is similar to using
a Value Getter, but is specific to the filter.
filterValueGetter
(string | function) Function or expression. Gets the value for filtering purposes.To include Apply, Clear, Recent and Cancel buttons to the filter menu
see Applying Filters section.
The Text Filter is not affected by data changes. When the grid data is updated, the filter value will remain unchanged
and the filter will be re-applied based on the updated data (for example, the displayed rows will update if necessary).
As of Dash AG Grid V31, boolean values will be rendered as check boxes and the filter will display a single dropdown
with 'True'
/'False'
. See more information
in Cell Data Types section.
filterParams
is set to display a single dropdown'True'
/'False'
(or equivalents with Localisation).filterParams.valueFormatter
is set to show 'True'
/'False'
(or equivalentsTo disable this feature, set cellDataType='text'