A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.ag-grid.com/javascript-data-grid/filter-api/ below:

JavaScript Grid: Filter API | AG Grid

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.


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 Link

You 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.

(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.

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.


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.

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).


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.

(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 gridOptions = {
    columnDefs: [
        {
            field: 'age',
            filter: true,
            filterParams: {
                readOnly: true
            }
        }
    ],

    
}

The following example demonstrates all of the Provided Filters with readOnly: true enabled:

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:

Filter Events Copy Link

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).

Next Up Copy Link

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