Filter API

You can interact either with filters directly by getting a reference to the filter instance, or indirectly by getting and setting all filter models through the grid API. This page details how to do both.

Accessing Filter Component Instances

It is possible to access the filter components directly if you want to interact with the specific filter. This also works for your own custom filters, where you can get a reference to the underlying filtering instance (ie what was created after ag-Grid called 'new' on your filter). You get a reference to the filter instance by calling api.getFilterInstance(colKey).

// Get a reference to the name filter instance var nameFilterInstance = api.getFilterInstance('name');

All of the methods of the filter are present. If using a custom filter then any other methods you have added will also be present, allowing bespoke behaviour to be added to your filter.

Example Filter API

The example below shows controlling the country and age filters via the API.

The example also shows 'gridApi.destroyFilter(col)' which completely destroys a filter. Use this is if you want a filter to be created again with new initialisation values.

(Note: the example uses the enterprise set filter).

Reset Individual Filters

You can reset a filter to its original state by getting the filter instance and then performing the action that makes sense for the filter type.

For all the filter types the sequence would be:

  • var filterComponent = gridOptions.api.getFilterInstance('filter_name');
  • perform reset action for filter type
  • gridOptions.api.onFilterChanged();

The following are the appropriate methods for the corresponding filter types:

Filter Type Action
number filterComponent.setModel(null);
text filterComponent.setModel(null);
set filterComponent.selectEverything();

Reset All Filters

You can reset all filters by doing the following:


Get / Set All Filter Models

It is possible to get and set the state of all the filters via the api methods api.getFilterModel() and api.setFilterModel(). These methods manage the filters states via the getModel() and setModel() methods of the individual filters.

This is useful if you want to save the filter state and apply it at a later state. It is also useful for server side filtering, where you want to pass the filter state to the server.

Example - Get / Set All Filter Models

The example below shows getting and setting all the filter models in action. The 'save' and 'restore' buttons mimic what you would do to save and restore the state of the filters. The big button (Name = 'Mich%'... etc) shows how you can hand craft a model and then set that into the filters.

(Note: the example uses the enterprise set filter).