React Data GridFloating Filter Component

Floating Filter Components allow you to add your own floating filter types to AG Grid. You can create a Custom Floating Filter Component to work alongside one of the grid's Provided Filters, or alongside a Custom Filter.

React Floating Filters thumbnail

Example: Custom Floating Filter

In the following example you can see how the Gold, Silver, Bronze and Total columns have a custom floating filter NumberFloatingFilter. This filter substitutes the standard floating filter for an input box that the user can change to adjust how many medals of each column to filter by based on a greater than filter.

Implementing a Floating Filter Component

Custom floating filter components are controlled components, which receive a filter model as part of the props, and pass model updates back to the grid via the onModelChange callback. A filter model of null means that no filter is applied (the filter displays as inactive). Note that the filter is applied immediately when onModelChange is called.

export default ({ model, onModelChange }) => {
   return (
       <div>
           <input
               type="text"
               value={model || ''}
               onChange={({ target: { value }}) => onModelChange(value === '' ? null : value)}
           />
       </div>
   );
}

In previous versions of the grid, custom components were declared in an imperative way. See Migrating to Use reactiveCustomComponents for details on how to migrate to the current format.

Custom Floating Filter Parameters

Floating Filter Props

The following props are passed to the custom floating filter components (CustomFloatingFilterProps interface). If custom props are provided via the colDef.floatingFilterParams property, these will be additionally added to the props object, overriding items of the same name if a name clash exists.

model
TModel | null
The current filter model for the component.
onModelChange
Function
Callback that should be called every time the model in the component changes.
column
The column this filter is for.
filterParams
IFilterParams
The params object passed to the filter. This is to allow the floating filter access to the configuration of the parent filter. For example, the provided filters use debounceMs from the parent filter params.
currentParentModel
Function
This is a shortcut to invoke getModel on the parent filter. If the parent filter doesn't exist (filters are lazily created as needed) then it returns null rather than calling getModel() on the parent filter.
parentFilterInstance
Function
Gets a reference to the parent filter. The result is returned asynchronously via a callback as the parent filter may not exist yet. If it does not exist, it is created and asynchronously returned (AG Grid itself does not create components asynchronously, however if providing a framework provided filter e.g. React, it might be). The floating filter can then call any method it likes on the parent filter. The parent filter will typically provide its own method for the floating filter to call to set the filter. For example, if creating custom filter A, it should have a method your floating A can call to set the state when the user updates via the floating filter.
showParentFilter
Function
Shows the parent filter popup.
The grid api.
context
TContext
Application context as set on gridOptions.context.

Floating Filter Callbacks

The following callbacks can be passed to the useGridFloatingFilter hook (CustomFloatingFilterCallbacks interface). All the callbacks are optional, and the hook only needs to be used if callbacks are provided.

afterGuiAttached
Function
Optional: A hook to perform any necessary operation just after the GUI for this component has been rendered on the screen. If a parent popup is closed and reopened (e.g. for filters), this method is called each time the component is shown. This is useful for any logic that requires attachment before executing, such as putting focus on a particular DOM element.

Floating Filter Lifecycle

Floating filters do not contain filter logic themselves, they are just an additional UI component for the underlying filter component. For this reason, the floating filters lifecycle is bound to the visibility of the column; if you hide a column (either set not visible, or horizontally scroll the column out of view) then the floating filter UI component is destroyed. If the column comes back into view, it is created again. This is different to column filters, where the column filter will exist as long as the column exists, regardless of the column's visibility.

To see examples of the different ways to implement floating filters please refer to the examples below.

Example: Custom Filter And Custom Floating Filter

This example extends the previous example by also providing its own custom filter NumberFilter in the Gold, Silver, Bronze and Total columns.

Example: Custom Filter And Read-Only Floating Filter

If you want to provide a custom filter but don't want to provide an equivalent custom floating filter, you can implement getModelAsString() and you will get a read-only floating filter for free.

This example uses the previous custom filter but implements getModelAsString(). Note how there are no custom floating filters and yet each column using NumberFilter (Gold, Silver, Bronze and Total) has a read-only floating filter that gets updated as you change the values from the main filter.

Sliding Floating Filters

The below example shows how to create a custom floating filter re-using the out-of-the-box Number Filter .