# Expression UI Components

Canonical page: https://www.adaptabletools.com/docs/adaptable-ql-expression-ui

- AdapTable provides 3 UI Components to help write and inspect Expressions:
  - Query Builder - UI-based editor (mainly dropdowns) for Boolean conditions
  - Expression Editor - full editor for hand-written and complex Expressions
  - Formula Editor - Formula Trace popup and Compact Editor for Calculated Columns
- All 3 validate as you work — an invalid Expression cannot be run or saved

AdapTable provides 3 User Interface components for working with [Expressions](https://www.adaptabletools.com/docs/adaptable-ql-expression/index.md).

- An AdapTableQL Expression is ultimately just a human-readable string and can, therefore, be written by hand
- Expressions provided in [Initial Adaptable State](https://www.adaptabletools.com/docs/dev-guide-adaptable-state-initial-state/index.md) might often be hand-produced

Each component is designed to deal with different levels of user proficiency and complexity of use case:

| Component                                                                             | Best For                                                 | Typical Use                                                                                                                                                                                                                                                                                                                |
| ------------------------------------------------------------------------------------- | -------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Query Builder](https://www.adaptabletools.com/docs/ui-query-builder/index.md)                          | Boolean conditions built from dropdowns | <ul><li>[Grid Filter](https://www.adaptabletools.com/docs/handbook-grid-filter/index.md)</li><li>[Alerts](https://www.adaptabletools.com/docs/handbook-alerting/index.md)</li><li>[Conditional Styles](https://www.adaptabletools.com/docs/handbook-column-formatting-conditions/index.md)</li><li>[Column Filters](https://www.adaptabletools.com/docs/handbook-column-filter/index.md)</li></ul> |
| [Expression Editor](https://www.adaptabletools.com/docs/ui-expression-editor/index.md) | Hand-written and complex Expressions                     | All Expression-based Modules                                                                                                                                                                                                                                                                                               |
| [Formula Editor](https://www.adaptabletools.com/docs/ui-formula-editor/index.md)                        | Inspecting and inline-editing a formula                  | [Calculated Columns](https://www.adaptabletools.com/docs/handbook-calculated-column/index.md)                                                                                                                                                                                                                                                |

New to AdapTableQL? Read the [AdapTableQL Getting Started Guide](https://www.adaptabletools.com/docs/adaptable-ql/index.md)

## Validation

All 3 Expression UI Components validate as you work. 

An invalid Expression cannot be run or saved.

- [Expression Editor](https://www.adaptabletools.com/docs/ui-expression-editor/index.md) — live underlines, error messages, and a Result preview
- [Query Builder](https://www.adaptabletools.com/docs/ui-query-builder/index.md) — flags issues on the Output Expression; will not open an Expression it cannot display
- [Formula Editor](https://www.adaptabletools.com/docs/ui-formula-editor/index.md) — live status in the Compact Editor; Grid cells show `#INVALID` until the formula is corrected

- Set `performExpressionValidation` to _false_ in [Expression Options](https://www.adaptabletools.com/docs/technical-reference-expression/index.md) to turn this off
- You will typically do this when [evaluating Expressions on your server](https://www.adaptabletools.com/docs/adaptable-server-evaluation/index.md)

## Managing Expression Components

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) can both be used wherever a **Boolean** Expression is edited.

By default both are offered, and the Query Builder opens first.

This is configured via 2 properties in `editorOptions` on [Expression Options](https://www.adaptabletools.com/docs/technical-reference-expression/index.md):

- `availableEditors` — which editors are shown (Query Builder, Expression Editor, or both)
- `defaultEditor` — which tab opens first when both are available

- These options do not apply to the [Formula Editor](https://www.adaptabletools.com/docs/ui-formula-editor/index.md), which is used only for Calculated Column formulae
- Nor do they apply to [Column Filters](https://www.adaptabletools.com/docs/handbook-column-filter/index.md) and Badge Style rules, which always use a Query Builder fixed to their Column

### `editorOptions.availableEditors`

Which Editors are available for Boolean Expressions

Set which of the UI Components end users can use when they create or edit Boolean Expressions.

If only one editor is listed, that is the only surface shown and `defaultEditor` is ignored.

```ts {4}
// Only make the Expression Editor available
expressionOptions: {
  editorOptions: {
    availableEditors: ['ExpressionEditor']
  }
},
```

**Example: Expression Editor only**

Boolean Expressions: Expression Editor only

- This demo sets `availableEditors` to `['ExpressionEditor']` so the Query Builder is never offered and never available (there is no Query Builder tab)

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
  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} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Expression Editor only',
  expressionOptions: {
    editorOptions: {
      availableEditors: ['ExpressionEditor'],
    },
  },
  initialState: {
    Dashboard: {
      PinnedToolbars: ['GridFilter'],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout', 'GridFilter'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'name',
            'github_stars',
            'language',
            'github_watchers',
            'updated_at',
            'description',
            'created_at',
            'has_wiki',
            'pushed_at',
            'open_issues_count',
          ],
          GridFilter: {
            Expression:
              '[github_stars] > [github_watchers] AND ([language] = "TypeScript" OR [language] = "JavaScript")',
          },
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

**`src/onAdaptableReady.ts`**

```ts
import {AdaptableReadyInfo} from '@adaptabletools/adaptable';

export const onAdaptableReady = ({adaptableApi}: AdaptableReadyInfo) => {
  setTimeout(() => {
    adaptableApi.filterApi.gridFilterApi.openUIEditorForGridFilter();
  }, 1000);
};
```

### `editorOptions.defaultEditor`

Which editor opens first when both are available

When both editors are listed, this chooses which tab opens first.

It can be a fixed value (`'QueryBuilder'` or `'ExpressionEditor'`), or a function so the choice can vary by Module.

The function receives a [`DefaultExpressionEditorContext`](https://www.adaptabletools.com/docs/reference/defaultexpressioneditorcontext.md):

| Property | Type | Description |
| --- | --- | --- |
| [adaptableModule](https://www.adaptabletools.com/docs/reference/defaultexpressioneditorcontext.md#adaptablemodule) | [`AdaptableModule`](https://www.adaptabletools.com/docs/reference/adaptablemodule.md) | Module that is hosting the Expression UI |
| [availableEditors](https://www.adaptabletools.com/docs/reference/defaultexpressioneditorcontext.md#availableeditors) | [`ExpressionEditors`](https://www.adaptabletools.com/docs/reference/expressioneditors.md) | Editors that are actually available for this surface (after `availableEditors` is applied) |
| [adaptableContext](https://www.adaptabletools.com/docs/reference/defaultexpressioneditorcontext.md#adaptablecontext) | `any` | Custom application Context provided in `AdaptableOptions.adaptableContext` |

```ts
// Open the Expression Editor first (both editors remain available)
expressionOptions: {
  editorOptions: {
    availableEditors: ['QueryBuilder', 'ExpressionEditor'],
    defaultEditor: 'ExpressionEditor',
  },
},
```

```ts
// Use the Expression Editor first for Alerts, Query Builder everywhere else
expressionOptions: {
  editorOptions: {
    defaultEditor: (context: DefaultExpressionEditorContext) =>
      context.adaptableModule === 'Alert' ? 'ExpressionEditor' : 'QueryBuilder',
  },
},
```

AdapTable overrides this to `'ExpressionEditor'` when the current Expression cannot be shown in the Query Builder (non-boolean results, advanced keywords such as `QUERY`, or an Expression the Builder cannot parse). Empty / new Expressions still honour `defaultEditor`.

**Example: Expression Editor opens first**

Boolean Expressions: Expression Editor as the default tab

- This demo keeps both editors available, but sets `defaultEditor` to `'ExpressionEditor'`
- The Grid Filter Settings Panel opens automatically on the Expression Editor tab

- Note the Expression Editor is the selected tab
- Switch to the Query Builder tab — both editors are still available

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
  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} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Expression Editor first',
  expressionOptions: {
    editorOptions: {
      availableEditors: ['QueryBuilder', 'ExpressionEditor'],
      defaultEditor: 'ExpressionEditor',
    },
  },
  initialState: {
    Dashboard: {
      PinnedToolbars: ['GridFilter'],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout', 'GridFilter'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'name',
            'github_stars',
            'language',
            'github_watchers',
            'updated_at',
            'description',
            'created_at',
            'has_wiki',
            'pushed_at',
            'open_issues_count',
          ],
          GridFilter: {
            Expression:
              '[github_stars] > [github_watchers] AND ([language] = "TypeScript" OR [language] = "JavaScript")',
          },
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

**`src/onAdaptableReady.ts`**

```ts
import {AdaptableReadyInfo} from '@adaptabletools/adaptable';

export const onAdaptableReady = ({adaptableApi}: AdaptableReadyInfo) => {
  setTimeout(() => {
    adaptableApi.filterApi.gridFilterApi.openUIEditorForGridFilter();
  }, 1000);
};
```
