You can access and set the models for filters through the grid API, or access individual filter instances directly for more control. This page details how to do both.
The filter model can be saved and restored as part of Grid State.
Get / Set All Filter Models Copy LinkIt is possible to get the state of all filters using the grid API method getFilterModel()
, and to set the state using setFilterModel()
. These methods manage the filters states via the getModel()
and setModel()
methods of the individual filters.
const model = api.getFilterModel();
api.setFilterModel(model);
The filter model represents the state of filters for all columns and has the following structure:
{
athlete: {
filterType: 'text',
type: 'startsWith',
filter: 'mich'
},
age: {
filterType: 'number',
type: 'lessThan',
filter: 30
}
}
This is useful if you want to save the global filter state and apply it at a later stage. It is also useful for server-side filtering, where you want to pass the filter state to the server.
Reset All Filters Copy LinkYou can reset all filters by doing the following:
api.setFilterModel(null);
Example: Get / Set All Filter Models Copy Link
The example below shows getting and setting all the filter models in action.
Save Filter Model
saves the current filter state, which will then be displayed.Restore Saved Filter Model
restores the saved filter state back into the grid.Set Custom Filter Model
takes a custom hard-coded filter model and applies it to the grid.Reset Filters
will clear all active filters.Destroy Filter
destroys the filter for the Athlete column by calling gridApi.destroyFilter('athlete')
. This removes any active filter from that column, and will cause the filter to be created with new initialisation values the next time it is interacted with.(Note: the example uses the Enterprise-only Set Filter).
Get / Set Individual Filter Model Copy LinkIt is also possible to get or set the filter model for a specific filter, including your own custom filters.
Re-running Grid Filtering Copy LinkAfter filters have been changed via their API, you must ensure the method gridApi.onFilterChanged()
is called to tell the grid to filter the rows again. If gridApi.onFilterChanged()
is not called, the grid will still show the data relevant to the filters before they were updated through the API.
await api.setColumnFilterModel('name', {
filterType: 'text',
type: 'startsWith',
filter: 'abc',
});
api.onFilterChanged();
Reset Individual Filters Copy Link
You can reset a filter to its original state by setting the model to null
.
await api.setColumnFilterModel('name', null);
api.onFilterChanged();
Example: Get / Set Individual Filter Model Copy Link
The example below shows getting and setting an individual filter model in action.
Save Filter Model
saves the Athlete filter state, which will then be displayed.Restore Saved Filter Model
restores the saved Athlete filter state back into the grid.Set Custom Filter Model
takes a custom hard-coded Athlete filter model and applies it to the grid.Reset Filter
will clear the Athlete filter.It certain cases, it may be needed to interact directly with a specific filter. For instance, Refreshing Values on the Set Filter.
Grid-provided filters are split into two parts - the filter UI component and the filter handler (which performs the filter logic).
When enableFilterHandlers = true
, Custom Filter Components are also split into two parts.
Note that the Multi Filter will only have a filter handler when enableFilterHandlers = true
.
To access the filter UI component, use api.getColumnFilterInstance(colKey)
.
To access the filter hander, use api.getColumnFilterHandler(colKey)
.
const filterInstance = await api.getColumnFilterInstance('name');
If using a custom filter, any other methods you have added will also be present, allowing bespoke behaviour to be added to your filter.
Example: Accessing Individual Filters Copy LinkThe example below shows how you can interact with an individual filter instance, using the Set Filter as an example.
Get Mini Filter Text
will print the text from the Set Filter's Mini Filter to the console.Save Mini Filter Text
will save the Mini Filter text.Restore Mini Filter Text
will restore the Mini Filter text from the saved state.(Note: the example uses the Enterprise-only Set Filter).
Read-only Filter UI Copy LinkSometimes it maybe useful to strictly control the filters used by the grid via API, whilst still exposing filter settings in-use to users. The readOnly
filter parameter changes the behaviour of all provided column filters so their UI is read-only. In this mode, API filter changes are still honoured and reflected in the UI:
const gridOptions = {
columnDefs: [
{
field: 'age',
filter: true,
filterParams: {
readOnly: true
}
}
],
}
The following example demonstrates all of the Provided Filters with readOnly: true
enabled:
athlete
column demonstrates Text Filter.age
and year
columns demonstrate Number Filter.date
column demonstrates Date Filter.country
, gold
, silver
and bronze
columns demonstrate Set Filter.readOnly: true
is needed to affect any associated Floating Filters.
sport
column demonstrates Multi Filter.readOnly: true
from their parent, disabling any UI input.Print Country
button prints the country model to the developer console.How filters are launched can be customised (unless grid option columnMenu = 'legacy'
).
colDef.suppressHeaderFilterButton = true
can be used to disable the button in the header that opens the filter.
The filter can also be launched via api.showColumnFilter(columnKey)
and hidden via api.hideColumnFilter()
.
The following example demonstrates launching the filter:
colDef.suppressHeaderFilterButton
. The filter can still be opened via the API by clicking the Open Country Filter
button.Filtering causes the following events to be emitted:
FilterOpenedEvent
Filter has been opened.
FilterChangedEvent
Filter has been modified and applied.
FilterModifiedEvent
Filter was modified but not applied (when using enableFilterHandlers = false
). Used when filters have 'Apply' buttons.
FilterUiChangedEvent
Filter UI was modified (when using enableFilterHandlers = true
).
FloatingFilterUiChangedEvent
Floating filter UI modified (when using enableFilterHandlers = true
).
Continue to the next section to learn about Custom Column Filters.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4