# Custom Column Filters

Canonical page: https://www.adaptabletools.com/docs/handbook-column-filter-custom-filters

- Developers are able to provide their own bespoke Column Filter operators - boolean [Custom Expression Functions](https://www.adaptabletools.com/docs/adaptable-ql-expression-functions/index.md)
- These are defined via `customStandardFunctions` in [Expression Options](https://www.adaptabletools.com/docs/technical-reference-expression/index.md)
- Custom Expression functions will automatically appear in the operator dropdown for the relevant Columns

Developers can provide bespoke **custom** filter operators in addition to the AdapTable-provided [System Filters](https://www.adaptabletools.com/docs/handbook-column-filter-system-filters/index.md).

This is done by registering a custom boolean Expression Function - in the `customStandardFunctions` section of [Expression Options](https://www.adaptabletools.com/docs/technical-reference-expression/index.md) - and AdapTable will make it available.

Custom functions can be provided with `operands`, the number of which determines how the function appears in the Column Filter:

| `operands`             | Number | Appears as                                          |
| ---------------------- | :----: | --------------------------------------------------- |
| `[['number']]`         |   1    | An operator with **no input** (Column subject only) |
| `[['text', 'number']]` |   2    | An operator with **one input** (Column + value)     |

Optional `label` and `icon` appear in the Column Filter / Filter Bar operator picker — the same presentation fields used by System operators. An icon can be a named AdapTable icon (`{ name: 'star' }`), a short text glyph (`{ text: '>n' }`), or a custom image / element.

- Only boolean functions with **1 or 2** operands appear in the Column Filter (and Query Builder)
- Functions with 0 or 3+ operands are available in the Expression Editor only

- See [Custom Standard Functions](https://www.adaptabletools.com/docs/adaptable-ql-expression-functions-custom-standard/index.md) for the full anatomy of a custom Expression Function
- See [System Filters](https://www.adaptabletools.com/docs/handbook-column-filter-system-filters/index.md) to control (per Module or per Column) which operators are available

**Example: Custom Filters**

Adding custom filter operators using boolean Expression Functions

- This demo registers 2 custom boolean Expression Functions which become Column Filter operators:
  - `IS_POPULAR` (1 operand) - a **no-input** operator (labelled *Popular*, star icon) offered on numeric Columns; returns `true` when the value exceeds 10,000
  - `LONGER_THAN` (2 operands) - a **one-input** operator (labelled *Longer Than*, `>n` icon) offered on text Columns; returns `true` when the text is longer than the supplied number of characters
- Both are pre-applied as Column Filters: `IS_POPULAR([github_stars])` and `LONGER_THAN([name], 5)`

- Open the `Github Stars` filter dropdown and see the *Popular* operator (star icon, no input) alongside the System operators
- Open the `Name` filter dropdown, choose *Longer Than* and enter a number to filter by text length — the `>n` icon makes it easy to spot in the Filter Bar

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {
  AdaptableOptions,
  ExpressionContext,
} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Custom Filters',
  expressionOptions: {
    customStandardFunctions: {
      // A no-input (arity 1) boolean function: one `operands` overload with a
      // single column subject. It appears in the operator dropdown for numeric
      // Columns with no value input required.
      IS_POPULAR: {
        handler: (args: any[], _context: ExpressionContext) => args[0] > 10000,
        returnType: 'boolean',
        operands: [['number']],
        label: 'Popular',
        icon: { name: 'star' },
        description: 'Value is greater than 10,000',
        signatures: ['IS_POPULAR(value: number)'],
        examples: ['IS_POPULAR([github_stars])'],
      },
      // A with-input (arity 2) boolean function: the overload is `[subject,
      // value]`, so it appears in the operator dropdown for text Columns with a
      // number input the user supplies.
      LONGER_THAN: {
        handler: (args: any[], _context: ExpressionContext) =>
          String(args[0] ?? '').length > args[1],
        returnType: 'boolean',
        operands: [['text', 'number']],
        label: 'Longer Than',
        icon: { text: '>n' },
        description: 'Text is longer than the given number of characters',
        signatures: ['LONGER_THAN(value: text, length: number)'],
        examples: ['LONGER_THAN([name], 5)'],
      },
    },
  },
  initialState: {
    Dashboard: {
      ModuleButtons: ['ColumnFilter'],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout', 'ColumnFilter'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'github_watchers',
            'created_at',
            'topics',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
          ColumnFilters: [
            {
              ColumnId: 'github_stars',
              Expression: 'IS_POPULAR([github_stars])',
            },
            {
              ColumnId: 'name',
              Expression: 'LONGER_THAN([name], 5)',
            },
          ],
        },
      ],
    },
  },
};
```

Users can create as many custom (and System) Column Filters as required.

AdapTable will automatically update AG Grid so that only rows which match **all** Filters are displayed.

## Overriding a System Operator

You can also **replace** a System operator with your own implementation.

To do this, register a custom function using `extends` to inherit the System function's label, icon and operands while supplying your own `handler`.

See [Custom Standard Functions](https://www.adaptabletools.com/docs/adaptable-ql-expression-functions-custom-standard/index.md) for how to override a System Expression Function via `extends`
