# Managing the Grid Filter

Canonical page: https://www.adaptabletools.com/docs/handbook-grid-filter-managing

- Developers can define a Grid Filter in a Layout and also configure how Grid Filters are used
- Run-time users can create and manage the Grid Filter in multiple ways

## Defining

The Grid Filter in AdapTable is defined inside the Layout object

This means that the Grid Filter is per-Layout only

The Grid Filter can be defined for both Table and Pivot Layouts.

### Adding Grid Filter to Layout

Add the  `GridFilter` prop to the Layout in order to filter across the whole Grid.

Add it separately to each Layout where you want to apply it.

The Grid Filter can be defined for both Table and Pivot Layouts.

The Grid Filter contains an `Expression` property.

This is a simple [(Boolean) Expression](https://www.adaptabletools.com/docs/adaptable-ql-expression-standard/index.md) that is evaluated by [AdapTableQL](https://www.adaptabletools.com/docs/adaptable-ql-expression/index.md).

```js [[1, 8, "GridFilter"],[1, 17, "GridFilter"],[2, 9, "Expression"],[2, 18, "Expression"]]
const initialState: InitialState = {
  Layout: {
    CurrentLayout: 'Table Layout',
    Layouts: [
      {
        Name: 'Table Layout',
        TableColumns: ['currency', 'orderDate', 'github_watchers', 'price'],
        GridFilter: {
          Expression: '[currency]="EUR" OR [price] > 5000',
        },
      },
      {
        Name: 'Pivot Layout',
        PivotColumns: ['language'],
        PivotGroupedColumns: ['license'],
        PivotAggregationColumns: [{ ColumnId: 'price', AggFunc: 'sum' }],
        GridFilter: {
          Expression: '[currency]="EUR" OR [price] > 5000',
        },
      },
    ],
  },
};
```

Run-time users (if correctly [permissioned](https://www.adaptabletools.com/docs/handbook-permissioning/index.md)) can write, edit, delete, clear, save, suspend and reload Grid Filter Expressions.

## Creating

AdapTable provides two UI components to facilitate the creation of the Grid Filter Expression at run-time:
- a [Query Builder](https://www.adaptabletools.com/docs/ui-query-builder/index.md) for straightforward Expressions
- an [Expression Editor](https://www.adaptabletools.com/docs/ui-expression-editor/index.md) for more complex requirements

These editors open in the [Settings Panel](https://www.adaptabletools.com/docs/ui-settings-panel/index.md) when the user clicks **Expand** (or the expression summary) in the Grid Filter [Toolbar](https://www.adaptabletools.com/docs/ui-dashboard/index.md#tabs-and-toolbars) or [Tool Panel](https://www.adaptabletools.com/docs/ui-tool-panel/index.md).

Use **Apply Grid Filter** button in the Settings Panel to apply the draft Expression and close the panel.

Clear, Save, Suspend and Named Query actions remain on the Toolbar / Tool Panel.

- Once created, the Expression is shown as a **summary** in the Toolbar (it is not edited in place)
- The Grid Filter Toolbar works especially well when [Pinned](https://www.adaptabletools.com/docs/ui-dashboard-tabs-toolbars/index.md#pinned-toolbars) above the Grid

Users can create multiple Grid Filters and load them on demand, but **only one** Grid Filter can be run (and active) at any one time.

Editing a Named Query creates a <b>new</b> Grid Filter - which can then itself be saved as a new Named Query

## Validating

[AdapTableQL](https://www.adaptabletools.com/docs/adaptable-ql-expression/index.md) automatically validates the Grid Filter as it is being written.

AdapTable will only enable <b>Apply Grid Filter</b> if the Grid Filter's Expression is valid

If an applied Grid Filter becomes invalid, the Toolbar / Tool Panel shows an Invalid indicator next to the expression summary.

Set `performExpressionValidation` in [Expression Options](https://www.adaptabletools.com/docs/technical-reference-expression/index.md) to false to turn off automatic validation.

This is useful if you are [evaluating the Grid Filter on your server](https://www.adaptabletools.com/docs/adaptable-server-evaluation/index.md)

## Clearing

The Grid Filter can be cleared by clicking the *Clear Grid Filter* button in the Grid Filter Toolbar or Tool Panel. 

This will have the effect of showing all rows in the Grid (subject to any [Column Filters](https://www.adaptabletools.com/docs/handbook-column-filter/index.md) that have been applied).

- Cleared Queries <b>can</b> be subsequently [reloaded](#loading-grid-filters)
- This applies both to [Named Queries](https://www.adaptabletools.com/docs/handbook-named-queries/index.md) or unsaved Grid Filters (but only those created in the current session).

## Saving

A Grid Filter can be saved by clicking the *Save* button in the Grid Filter Toolbar or Tool Panel.

This will save the Grid Filter as a [Named Query](https://www.adaptabletools.com/docs/handbook-named-queries/index.md) for re-use as required.

Grid Filters which are not saved explicitly are still persisted temporarily and can be reloaded during the <b>current session</b>

## Loading

The Grid Filter [Toolbar](https://www.adaptabletools.com/docs/ui-dashboard/index.md#tabs-and-toolbars) and [Tool Panel](https://www.adaptabletools.com/docs/ui-tool-panel/index.md) both contain a control which allows previously run Expressions to be loaded and then run as a Grid Filter.

This includes all saved [Named Queries](https://www.adaptabletools.com/docs/handbook-named-queries/index.md), plus any unsaved Grid Filters created in the Current Session (listed as Recent Grid Filters).

Unsaved Grid Filters are named according to the time of creation (e.g. 'Grid Filter at 5:38:33 PM')

## Suspending

An active Grid Filter can be [suspended](https://www.adaptabletools.com/docs/dev-guide-adaptable-state-suspending/index.md) from the Toolbar or Tool Panel without clearing it.

While suspended:
- the Expression remains visible (muted) in the summary
- it is no longer applied to the Grid
- editing is blocked until the Filter is unsuspended

Use **Unsuspend** to re-apply the same Expression.

## Configuring

### Setting Editors

Grid Filter uses the same Expression editors as the rest of AdapTable — the [Query Builder](https://www.adaptabletools.com/docs/ui-query-builder/index.md) and [Expression Editor](https://www.adaptabletools.com/docs/ui-expression-editor/index.md). 

Which of those are offered, and which opens first, is configured once for all Boolean Expressions via 2 properties in `expressionOptions.editorOptions`:
- `availableEditors` - sets which of the 2 Editors are available for users
- `defaultEditor` - which Editor is displayed by default (can be configured on a per-Module basis)

See [Managing Expression Components](https://www.adaptabletools.com/docs/adaptable-ql-expression-ui/index.md#managing-expression-components) for full details and demos

### Clearing on Startup

AdapTable always saves the previously applied Grid Filter into Adaptable State (via the Current Layout).

It will then automatically re-apply this Grid Filter when the Application is reloaded.

If this behaviour is not desired, set the `clearFiltersOnStartUp` property in [Filter Options](https://www.adaptabletools.com/docs/technical-reference-filter/index.md) to *true*.

### `clearFiltersOnStartUp`

Clear the Grid Filter (and any Column Filters) applied in previous session when Application re-starts

AdapTable saves all [User State](https://www.adaptabletools.com/docs/dev-guide-adaptable-state/index.md) to either local or remote storage, including the Grid Filter.

It then re-applies the persisted Grid Filter when the application re-starts.

Setting this property to *true* will remove the Grid Filter (and any Column Filters) in Adaptable State from the previous session, and display a non-filtered AG Grid instance.

```ts {4}
// Clear any previously set Grid or Column Filters when AdapTable re-starts
const adaptableOptions: AdaptableOptions = {
    filterOptions = {
        clearFiltersOnStartUp: true
    }
};
``` 

The `clearQuickSearchOnStartUp` property in [Quick Search Options](https://www.adaptabletools.com/docs/technical-reference-quick-search/index.md) will clear a saved [Quick Search](https://www.adaptabletools.com/docs/handbook-quick-search/index.md)
