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 Link
It 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.
Gets the current state of all the column filters. Used for saving filter state. |
Sets the state of all the column filters. Provide it with what you get from getFilterModel() to restore filter state.
If inferring cell data types, and row data is initially empty or yet to be set,
the filter model will be applied asynchronously after row data is added.
To always perform this synchronously, set cellDataType = false on the default column definition,
or provide cell data types for every column. |
// Gets filter model via the grid API
const model = gridApi.getFilterModel();
// Sets the filter model via the grid API
gridApi.setFilterModel(model);
The filter model represents the state of filters for all columns and has the following structure:
// Sample filter model via getFilterModel()
{
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 Link
You can reset all filters by doing the following:
gridApi.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 callinggridApi.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 Link
It is also possible to get or set the filter model for a specific filter, including your own custom filters.
Gets the current filter model for the specified column.
Will return null if no active filter.
useUnapplied : If enableFilterHandlers = true and value is true , will return the unapplied filter model. |
Sets the filter model for the specified column.
Setting a model of null will reset the filter (make inactive).
Must wait on the response before calling api.onFilterChanged() . |
Re-running Grid Filtering Copy Link
After 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.
// Set a filter model
await api.setColumnFilterModel('name', {
filterType: 'text',
type: 'startsWith',
filter: 'abc',
});
// Tell grid to run filter operation again
api.onFilterChanged();
Reset Individual Filters Copy Link
You can reset a filter to its original state by setting the model to null
.
// Set the model to null
await api.setColumnFilterModel('name', null);
// Tell grid to run filter operation again
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.
Accessing Individual Filters Copy Link
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)
.
Returns the filter component instance for a column.
For getting/setting models for individual column filters, use getColumnFilterModel and setColumnFilterModel instead of this.
key can be a column ID or a Column object. |
Returns the filter handler instance for a column.
Used when enableFilterHandlers = true , or when using a grid-provided filter.
If using a SimpleColumnFilter , this will be an object containing the provided doesFilterPass callback.
key can be a column ID or a Column object. |
// Get a reference to the 'name' filter UI instance
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 Link
The 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 Link
Sometimes 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 [columnDefs, setColumnDefs] = useState([
{
field: 'age',
filter: true,
filterParams: {
readOnly: true
}
}
]);
<AgGridReact columnDefs={columnDefs} />
The following example demonstrates all of the Provided Filters with readOnly: true
enabled:
- Simple Filters have a read-only display with no buttons; if there is no 2nd condition set then the join operator and 2nd condition are hidden:
athlete
column demonstrates Text Filter.age
andyear
columns demonstrate Number Filter.date
column demonstrates Date Filter.
- Set Filter allows Mini Filter searching of values, but value inclusion/exclusion cannot be toggled; buttons are also hidden, and pressing enter in the Mini Filter input has no effect:
country
,gold
,silver
andbronze
columns demonstrate Set Filter.
- Multi Filter has no direct behaviour change, sub-filters need to be individually made read-only.
readOnly: true
is needed to affect any associated Floating Filters.sport
column demonstrates Multi Filter.
- Floating Filters are enabled and inherit
readOnly: true
from their parent, disabling any UI input. - Buttons above the grid provide API interactions to configure the filters.
Print Country
button prints the country model to the developer console.
Launching Filters Copy Link
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:
- The Athlete column has a filter button in the header to launch the filter.
- The Age column has a floating filter, so the header button is automatically hidden.
- The Country column has the filter button hidden via
colDef.suppressHeaderFilterButton
. The filter can still be opened via the API by clicking theOpen Country Filter
button. - The Year column has a floating filter and the header button is also suppressed, so has a slightly different display style when the filter is active.
Filter Events Copy Link
Filtering causes the following events to be emitted:
Filter has been opened. |
Filter has been modified and applied. |
Filter was modified but not applied (when using enableFilterHandlers = false ). Used when filters have 'Apply' buttons. |
Filter UI was modified (when using enableFilterHandlers = true ). |
Floating filter UI modified (when using enableFilterHandlers = true ). |
Next Up Copy Link
Continue to the next section to learn about Custom Column Filters.