Expand All

  Getting Started



  Row Models




  Third Party


Github stars make projects look great. Please help, donate a star, it's free.
Read about ag-Grid's Partnership with webpack.
Get informed on releases and other ag-Grid news only - never spam.
Follow on Twitter

Filter Component

Filter components allow you to add your own filter types to ag-Grid. Use this when the provided filters do not meet your requirements.

To provide a custom filter, instead of providing a string for the filter in the column definition, provide a Filter Component in the form of a function. ag-Grid will call 'new' on this function and treat the generated class instance as a filter component. A filter component class can be any function / class that implements the following interface:

interface IFilterComp { // mandatory methods // The init(params) method is called on the filter once. See below for details on the parameters. init(params: IFilterParams): void; // Returns the GUI for this filter. The GUI can be a) a string of html or b) a DOM element or node. getGui(): any; // The grid calls this to know if the filter icon in the header should be shown. Return true to show. isFilterActive(): boolean; <span class="codeComment">// The grid will ask each active filter, in turn, whether each row in the grid passes. If any // filter fails, then the row will be excluded from the final set. The method is provided a // params object with attributes node (the rodNode the grid creates that wraps the data) and data // (the data object that you provided to the grid for that row).</span> doesFilterPass(params: IDoesFilterPassParams): boolean; // Gets the filter state for storing getModel(): any; <span class="codeComment">// Restores the filter state. Called either as a result of user calling // <i>gridApi.setSortModel</i> OR the floating filter changed (only if using floating filters).</span> setModel(model: any): void; // optional methods <span class="codeComment">// Gets called every time the popup is shown, after the gui returned in // getGui is attached to the DOM. If the filter popup is closed and reopened, this method is called // each time the filter is shown. This is useful for any logic that requires attachment before executing, // such as putting focus on a particular DOM element. The params has one callback method 'hidePopup', // which you can call at any later point to hide the popup - good if you have an 'Apply' button and // you want to hide the popup after it is pressed.</span> afterGuiAttached?(params?: {hidePopup?: Function}): void; <span class="codeComment">// Gets called when new rows are inserted into the grid. If the filter needs to change its state // after rows are loaded, it can do it here. For example the set filters uses this to update the list of // available values to select from (eg 'Ireland', 'UK' etc for Country filter).</span> onNewRowsLoaded?(): void; <span class="codeComment">// Gets called when the Column is destroyed. If your custom filter needs to do // any resource cleaning up, do it here. A filter is NOT destroyed when it is // made 'not visible', as the gui is kept to be shown again if the user selects // that filter again. The filter is destroyed when the column it is associated with is destroyed, // either new columns are set into the grid, or the grid itself is destroyed.</span> destroy?(): void; <span class="codeComment">// Only used in conjunction with floating filters. // // If floating filters are turned on for the grid, but you have no floating filter // configured for this column, then the grid will check for this method. If this // method exists, then the grid will provide a read only floating filter for you // and display the results of this method. For example, if your filter is a simple // filter with one string input value, you could just return the simple string // value here. // // If you are implementing a floating filter for your filter, then leave this method out.</span> getModelAsString?(model:any): string; <span class="codeComment">// Only used in conjunction with floating filters. // // When a floating filter changes and calls the <i>onFloatingFilterChanged(change)</i> callback then: // a) <i>filter.onFloatingFilterChanged(change)</i> gets called if it exists. // ELSE // b) <i>filter.setModel(model)</i> gets called. // // If <i>setModal(modal)</i> is used, then the change object you pass should be the model // object the filter is expecting. The grid will then continue and update the grids rows // based on the new filter state. // // If <i>onFloatingFilterChanged(change)</i> is used, then the change object you pass // can be anything you like, as long as it's expected by your filter. The grid will // update the grid rows for you, you will need to do this yourself by calling filter // <i>filterChangedCallback()</i> if you need. Use this if your need to do more in your floating // than <i>setModel()</i> does. For example ag-Grid out of the box filter components use // this to also consider logic for the Apply button (as if Apply button is active, then // the filter does not call <i>filterChangedCallback()</i>. </span> onFloatingFilterChanged?(change:any): void; }


The method init(params) takes a params object with the items listed below. If the user provides params via the colDef.filterParams attribute, these will be additionally added to the params object, overriding items of the same name if a name clash exists.

interface IFilterParams { // The column this filter is for column: Column; // The column definition for the column colDef: ColDef; <span class="codeComment">// The row model, helpful for looking up data values if needed. // If the filter needs to know which rows are a) in the table b) currently // visible (ie not already filtered), c) what groups d) what order - all of this // can be read from the rowModel.</span> rowModel: IRowModel; <span class="codeComment">// A function callback, to be called, when the filter changes, // to inform the grid to filter the data. The grid will respond by filtering the data.</span> filterChangedCallback: ()=> void; <span class="codeComment">// A function callback, to be optionally called, when the filter changes, // but before 'Apply' is pressed. The grid does nothing except call // gridOptions.filterModified(). This is useful if you are making use of // an 'Apply' button and want to inform the user the filters are not // longer in sync with the data (until you press 'Apply').</span> filterModifiedCallback: ()=> void; <span class="codeComment">// A function callback, call with a node to be given the value for that // filters column for that node. The callback takes care of selecting the right colDef // and deciding whether to use valueGetter or field etc. This is useful in, for example, // creating an Excel style filer, where the filter needs to lookup available values to // allow the user to select from.</span> valueGetter: (rowNode: RowNode) => any; <span class="codeComment">// A function callback, call with a node to be told whether // the node passes all filters except the current filter. This is useful if you want // to only present to the user values that this filter can filter given the status // of the other filters. The set filter uses this to remove from the list, items that // are no longer available due to the state of other filters (like Excel type filtering). </span> doesRowPassOtherFilter: (rowNode: RowNode) => boolean; // The context for this grid. See section on <a href="../javascript-grid-context/">Context</a> context: any; <span class="codeComment">// If the grid options angularCompileFilters is set to true, then a new child // scope is created for each column filter and provided here. Just ignore this if // you are not using Angular 1</span> $scope: any; // The grid API api: any; }


The method doesFilterPass(params) takes the following as a parameter:

interface IDoesFilterPassParams { // The row node in question node: RowNode; // The data part of the row node in question data: any }

Associating Floating Filter

If you create your own filter you have two options to get its floating filters working for that filter:

  1. You can create your own floating filter.
  2. You can implement the method getModelAsString() in your custom filter. If you implement this method and you don't provide a custom floating filter, ag-Grid will automatically provide a read-only version of a floating filter
If you don't provide any of these two options for your custom filter, the display area for the floating filter will be empty.

Custom Filter Example

The example below shows two custom filters. The first is on the Athlete column and the second is on the Year column.

Angular Filtering

This section explains how to create an ag-Grid Filter using Angular. You should read about how Filters Components work in ag-Grid first before trying to understand this section.

It is possible to provide a Angular Component filter for ag-Grid to use. All of the information above is relevant to Angular filters. This section explains how to apply this logic to your Angular component.

For an example on Angular filtering, see the ag-grid-angular-example on Github.

Specifying a Angular Filter

If you are using the ag-grid-angular component to create the ag-Grid instance, then you will have the option of additionally specifying the filters as Angular components.

// create your filter as a Angular component @Component({ selector: 'filter-cell', template: ` Filter: <input style="height: 10px" #input (ngModelChange)="onChange($event)" [ngModel]="text"> ` }) class PartialMatchFilterComponent implements AgFilterComponent { private params:IFilterParams; private valueGetter:(rowNode:RowNode) => any; private text:string = ''; @ViewChild('input', {read: ViewContainerRef}) private input; agInit(params:IFilterParams):void { this.params = params; this.valueGetter = params.valueGetter; } isFilterActive():boolean { return this.text !== null && this.text !== undefined && this.text !== ''; } doesFilterPass(params:IDoesFilterPassParams):boolean { return this.text.toLowerCase() .split(" ") .every((filterWord) => { return this.valueGetter(params.node).toString().toLowerCase().indexOf(filterWord) >= 0; }); } getModel():any { return {value: this.text}; } setModel(model:any):void { this.text = model.value; } afterGuiAttached(params:IAfterFilterGuiAttachedParams):void { this.input.element.nativeElement.focus(); } componentMethod(message:string) : void { alert(`Alert from PartialMatchFilterComponent ${message}`); } onChange(newValue):void { if (this.text !== newValue) { this.text = newValue; this.params.filterChangedCallback(); } } } // then reference the Component in your colDef like this colDef = { // we use cellRendererFramework instead of cellRenderer filterFramework: PartialMatchFilterComponent // specify all the other fields as normal headerName: 'Name', field: 'firstName', ... }

Your Angular components need to implement AgFilterComponent. The ag Framework expects to find the mandatory methods on the interface on the created component (and will call optional methods if they're present).

By using colDef.filterFramework (instead of colDef.filter) the grid will know it's a Angular component, based on the fact that you are using the Angular version of ag-Grid.

Angular Params

The ag Framework expects to find the agInit (on the AgFilterComponent interface) method on the created component, and uses it to supply the 'filter params'.

agInit(params:IFilterParams):void { this.params = params; this.valueGetter = params.valueGetter; }

Angular Methods / Lifecycle

All of the methods in the IFilter interface described above are applicable to the Angular Component with the following exceptions:

  • init() is not used. Instead implement the agInit method (on the AgRendererComponent interface).
  • destroy() is not used. Instead implement the AngularOnDestroy interface (ngOnDestroy) for any cleanup you need to do.
  • getGui() is not used. Angular will provide the Gui via the supplied template.

After that, all the other methods (onNewRowsLoaded(), getModel(), setModel() etc) behave the same so put them directly onto your Angular Component.

Accessing the Angular Component Instance

ag-Grid allows you to get a reference to the filter instances via the api.getFilterInstance(colKey) method. If your component is a Angular component, then this will give you a reference to the ag-Grid's Component which wraps your Angular Component. Just like Russian Dolls. To get to the wrapped Angular instance of your component, use the getFrameworkComponentInstance() method as follows: // lets assume a Angular component as follows @Component({ selector: 'filter-cell', template: ` Filter: <input style="height: 10px" #input (ngModelChange)="onChange($event)" [ngModel]="text"> ` }) class PartialMatchFilterComponent implements AgFilterComponent { ... // standard filter methods hidden // put a custom method on the filter myMethod() { // does something } } // then in your app, if you want to execute myMethod()... laterOnInYourApplicationSomewhere() { // get reference to the ag-Grid Filter component let agGridFilter = api.getFilterInstance('name'); // assume filter on name column // get Angular instance from the ag-Grid instance let ng2FilterInstance = agGridFilter.getFrameworkComponentInstance(); // now we're sucking diesel!!! ng2FilterInstance.myMethod(); }

Example: Filtering using Angular Components

Using Angular Components as a partial text Filter in the "Filter Component" column, illustrating filtering and lifecycle events.