---
product: "AG Grid"
title: "External Filter"
description: "External filtering allows custom filtering logic to be mixed with the grid's inbuilt filtering."
framework: javascript
version: "36.2.0"
related:
    - title: "Overview"
      url: "https://www.ag-grid.com/archive/36.2.0/javascript-data-grid/filtering-overview/"
    - title: "Column Filters"
      url: "https://www.ag-grid.com/archive/36.2.0/javascript-data-grid/filtering/"
    - title: "Custom Column Filters"
      url: "https://www.ag-grid.com/archive/36.2.0/javascript-data-grid/component-filter/"
    - title: "Floating Filters"
      url: "https://www.ag-grid.com/archive/36.2.0/javascript-data-grid/floating-filters/"
    - title: "Custom Floating Filters"
      url: "https://www.ag-grid.com/archive/36.2.0/javascript-data-grid/component-floating-filter/"
    - title: "Advanced Filter"
      url: "https://www.ag-grid.com/archive/36.2.0/javascript-data-grid/filter-advanced/"
    - title: "Quick Filter"
      url: "https://www.ag-grid.com/archive/36.2.0/javascript-data-grid/filter-quick/"
llms: "https://www.ag-grid.com/archive/36.2.0/llms.txt"
---

# External Filter

External filtering allows custom filtering logic to be mixed with the grid's inbuilt filtering.

> **Warning**
>
> This form of filtering is only compatible with the Client-Side Row Model, see [Row Models](https://www.ag-grid.com/archive/36.2.0/javascript-data-grid/row-models/) for more details.

#### External Filter

```ts
import {
  ClientSideRowModelModule,
  ColDef,
  DateFilterModule,
  ExternalFilterModule,
  GridApi,
  GridOptions,
  IDateFilterParams,
  IRowNode,
  ModuleRegistry,
  NumberFilterModule,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";
import {
  ColumnMenuModule,
  ColumnsToolPanelModule,
  ContextMenuModule,
  SetFilterModule,
} from "ag-grid-enterprise";
import { IOlympicData } from "./interfaces";

if (process.env.NODE_ENV !== "production") {
  // Enable extended validations only for development
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ExternalFilterModule,
  ClientSideRowModelModule,
  ColumnsToolPanelModule,
  ColumnMenuModule,
  ContextMenuModule,
  SetFilterModule,
  NumberFilterModule,
  DateFilterModule,
]);

const dateFilterParams: IDateFilterParams = {
  comparator: (filterLocalDateAtMidnight: Date, cellValue: string) => {
    const cellDate = asDate(cellValue);

    if (filterLocalDateAtMidnight.getTime() === cellDate.getTime()) {
      return 0;
    }

    if (cellDate < filterLocalDateAtMidnight) {
      return -1;
    }

    if (cellDate > filterLocalDateAtMidnight) {
      return 1;
    }
    return 0;
  },
};

const columnDefs: ColDef[] = [
  { field: "athlete", minWidth: 180 },
  { field: "age", filter: "agNumberColumnFilter" },
  { field: "country" },
  { field: "year" },
  {
    field: "date",
    filter: "agDateColumnFilter",
    filterParams: dateFilterParams,
  },
  { field: "total", filter: "agNumberColumnFilter" },
];

let gridApi: GridApi<IOlympicData>;

const gridOptions: GridOptions<IOlympicData> = {
  columnDefs: columnDefs,
  defaultColDef: {
    flex: 1,
    minWidth: 120,
    filter: true,
  },
  isExternalFilterPresent: isExternalFilterPresent,
  doesExternalFilterPass: doesExternalFilterPass,
};

let ageType = "everyone";

function isExternalFilterPresent(): boolean {
  // if ageType is not everyone, then we are filtering
  return ageType !== "everyone";
}

function doesExternalFilterPass(node: IRowNode<IOlympicData>): boolean {
  if (node.data) {
    switch (ageType) {
      case "below25":
        return node.data.age < 25;
      case "between25and50":
        return node.data.age >= 25 && node.data.age <= 50;
      case "above50":
        return node.data.age > 50;
      case "dateAfter2008":
        return asDate(node.data.date) > new Date(2008, 0, 1);
      default:
        return true;
    }
  }
  return true;
}

function asDate(dateAsString: string) {
  const splitFields = dateAsString.split("/");
  return new Date(
    Number.parseInt(splitFields[2]),
    Number.parseInt(splitFields[1]) - 1,
    Number.parseInt(splitFields[0]),
  );
}

function externalFilterChanged(newValue: string) {
  ageType = newValue;
  gridApi!.onFilterChanged();
}

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);

fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
  .then((response) => response.json())
  .then(function (data) {
    (document.querySelector("#everyone") as HTMLInputElement).checked = true;
    gridApi!.setGridOption("rowData", data);
  });

if (typeof window !== "undefined") {
  // Attach external event handlers to window so they can be called from index.html
  (<any>window).externalFilterChanged = externalFilterChanged;
}
```

[Live example: External Filter](https://www.ag-grid.com/archive/36.2.0/examples/filter-external/external-filter/typescript/)

## Implementing External Filtering

The example above shows external filters in action. Two methods on `gridOptions` are required to be implemented: `isExternalFilterPresent` and `doesExternalFilterPass`.

| Property | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `isExternalFilterPresent` | `IsExternalFilterPresent` |  |  |  |
| `doesExternalFilterPass` | `DoesExternalFilterPass` |  |  |  |

## Re-running the External Filter

The filter state is held outside the grid, so the grid has to be told when that state has changed. Pick one of the following approaches:

- [Calling onFilterChanged](https://www.ag-grid.com/archive/36.2.0/javascript-data-grid/filter-external/#calling-onfilterchanged) - the callback references are kept stable and the filter is re-run only when the API is called.
- [Supplying New Callbacks](https://www.ag-grid.com/archive/36.2.0/javascript-data-grid/filter-external/#supplying-new-callbacks) - a new callback reference is handed to the grid and the filter is re-run automatically.

### Calling onFilterChanged

After the filter state has changed call `api.onFilterChanged()` to ask the grid to run filtering again.

| Property | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `onFilterChanged` | `FilterChangedEventSourceType` |  |  |  |

```js
// Filter state updated now re-run filtering
api.onFilterChanged();
```

Ensure the callbacks have stable references to avoid triggering filtering excessively.

### Supplying New Callbacks

`isExternalFilterPresent` and `doesExternalFilterPass` are reactive grid properties, so replacing either one with a new function re-runs filtering automatically.

```js
api.setGridOption('doesExternalFilterPass', (node) => node.data.age > 50);
```

The example on this page takes the first path: the callbacks are stable, and each change to the filter state calls `api.onFilterChanged()`.
