# Format Columns

Canonical page: https://www.adaptabletools.com/docs/handbook-column-formatting

- The **Format Column** module controls how column values read and how cells look in AG Grid
- Each definition can include a [Display Format](https://www.adaptabletools.com/docs/handbook-column-formatting-display-format/index.md), an [Style](https://www.adaptabletools.com/docs/handbook-column-formatting-adaptable-style/index.md), or both
- Definitions are scoped to columns, data types, or whole rows; an optional [Condition](https://www.adaptabletools.com/docs/handbook-column-formatting-conditions/index.md) limits when a rule applies
- Multiple Format Columns can apply to the same column; AdapTable **composes** matching rules and uses **precedence** when properties clash

The Format Column module lets users control how values read and how cells look in AG Grid.

This Module is used by AdapTable for 2 main types of formatting:

- **Display Formats** - formats the display value of a Cell
- **Cell Styling** - colours, fonts, alignment etc.

- There are a few other styling-related functionalities in AdapTable including:
  - Six [Styled Columns](https://www.adaptabletools.com/docs/handbook-styled-column-overview/index.md) (dedicated renderers for charts, gradients, percent bars, badges etc.)
  - [Flashing Cells](https://www.adaptabletools.com/docs/handbook-flashing-cell/index.md) (short-lived highlights).
  - [Quick Search](https://www.adaptabletools.com/docs/handbook-quick-search/index.md) highlights matching cells in a search
  - A comprehensive [Theming Module](https://www.adaptabletools.com/docs/handbook-theming/index.md) to style the AdapTable UI (i.e. all wizards and popups)

The style specified in Column Formatting has the **lowest** level of precedence when AdapTable applies styling.

### Style Precedence in AdapTable

So with all these Modules in AdapTable that can apply cell styling - which takes precedence?

The answer is that the shorter the display time, and the less frequently the style is applied, the higher the precedence.

Which means that the order in which AdapTable will apply a Style to a cell is:

1.  Any [Flashing Cells](https://www.adaptabletools.com/docs/handbook-flashing-cell/index.md)
2.  A [Quick Search](https://www.adaptabletools.com/docs/handbook-quick-search/index.md)
3.  Format Column

- [Styled Columns](https://www.adaptabletools.com/docs/handbook-styled-column-overview/index.md) are a separate renderer
- Where they expose Font or Cell slices, those override matching Format Column properties on same column
- Anything left unset still inherits from Format Column

Each Format Column definition shares the same building blocks:

- **Scope** — which columns, data types, or whole rows are in scope (see [Scope](#scope) below)
- **Target** — cell or column header
- **Display Format** and/or **Style** — how values read and how cells look (not mutually exclusive)
- **Condition** — optional rule; most often used with styles (see [Conditional Styling](https://www.adaptabletools.com/docs/handbook-column-formatting-conditions/index.md))
- **Row scope** — data rows, group rows, summaries, and related settings

- Format Columns apply to **all column types** including [Calculated](https://www.adaptabletools.com/docs/handbook-calculated-column/index.md) and [Free Text](https://www.adaptabletools.com/docs/handbook-freetext-column/index.md) columns
- Formatted columns remain editable and filterable when those features are enabled on the column
- A Format Column can be limited to specific Layouts via [Layout Extensions](https://www.adaptabletools.com/docs/handbook-layouts-extending/index.md)

## Topics in this Section

Format Column behaviour is split across several handbook pages. **Display format types** (numeric, string, date, template, custom) are listed on [Applying Display Formats](https://www.adaptabletools.com/docs/handbook-column-formatting-display-format/index.md).

| Topic                                                                                                | Summary                                                                                                                          |
| ---------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| [Display Formats](https://www.adaptabletools.com/docs/handbook-column-formatting-display-format/index.md)              | How cell **text** is shown — presets, formatters, **templates**, and **custom display formatters**                               |
| [Custom Display Format](https://www.adaptabletools.com/docs/handbook-column-formatting-display-format-custom/index.md) | Register `customDisplayFormatters` in Adaptable Options; reference by id from state or the wizard                                |
| [Template Formats](https://www.adaptabletools.com/docs/handbook-column-formatting-display-format-template/index.md)    | `[value]`, `[column]`, `[rowData.x]` template literals in display format options                                                 |
| [Styles](https://www.adaptabletools.com/docs/handbook-column-formatting-adaptable-style/index.md)                      | Colours, fonts, alignment, borders, optional CSS classes                                                                         |
| [Conditional Styling](https://www.adaptabletools.com/docs/handbook-column-formatting-conditions/index.md)              | Boolean rules; includes [$SCOPE](https://www.adaptabletools.com/docs/adaptable-ql-expression-advanced-scope-function/index.md) for type-wide rules |
| [Observable Conditions](https://www.adaptabletools.com/docs/handbook-column-formatting-conditions-observable/index.md) | Rules driven by change history / time                                                                                            |
| [Configuring Column Formatting](https://www.adaptabletools.com/docs/handbook-column-formatting-configuring/index.md)   | Composition, **precedence**, row and column-group scope                                                                          |
| [Column Headers](https://www.adaptabletools.com/docs/handbook-column-formatting-headers/index.md)                      | Format column **header** text and style (no conditions on headers)                                                               |
| [Technical Reference](https://www.adaptabletools.com/docs/technical-reference-styling-formatting/index.md)             | State, `formatColumnOptions`, Format Column API                                                                                  |

**Also in Cell Rendering**

| Topic                                                                        | Summary                                                             |
| ---------------------------------------------------------------------------- | ------------------------------------------------------------------- |
| [Styled Columns](https://www.adaptabletools.com/docs/handbook-styled-column-overview/index.md) | Gradient, percent bar, badge, sparkline, and other renderer columns |
| [Flashing Cells](https://www.adaptabletools.com/docs/handbook-flashing-cell/index.md)          | Temporary highlight when data or observables change                 |

**Example: Column Formatting**

How Column Formatting Works

- This example contains a mixture of different Column Formats and Styles:
  - `Name` has an Adaptable Style of Bold
  - `Github Stars` has a Display Format (showing a Prefix) and Adaptable Style for both fore and back colours
  - All `Date` columns have a Display Format of 'MMM do yyyy' applied
  - A Blue Style has been set for the Whole Row, using a Condition: `[language] = "TypeScript" AND [license] = "MIT License"`

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

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Formatting Columns',
  initialState: {
    Dashboard: {
      ModuleButtons: ['FormatColumn'],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'FormatColumn',
      Layouts: [
        {
          Name: 'FormatColumn',
          TableColumns: [
            'name',
            'github_stars',
            'has_projects',
            'language',
            'closed_issues_count',
            'created_at',
            'license',
            'open_pr_count',
            'updated_at',
            'pushed_at',
            'has_wiki',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
    FormatColumn: {
      FormatColumns: [
        {
          Name: 'formatColumn-name',
          Scope: {
            ColumnIds: ['name'],
          },
          Style: {
            FontWeight: 'Bold',
          },
        },
        {
          Name: 'formatColumn-github_stars',
          Scope: {
            ColumnIds: ['github_stars'],
          },
          Style: {
            ForeColor: '#c2bb00',
            BackColor: '#f5f5f5',
          },
          DisplayFormat: {
            Formatter: 'NumberFormatter',
            Options: {
              Prefix: 'Stars: ',
            },
          },
        },
        {
          Name: 'formatColumn-date',
          Scope: {
            DataTypes: ['date'],
          },
          DisplayFormat: {
            Formatter: 'DateFormatter',
            Options: {
              Pattern: 'MMM do yyyy',
            },
          },
          Style: {
            Alignment: 'Center',
          },
        },
        {
          Name: 'style-all',
          Style: {
            BackColor: 'var(--ab-color-palette-26)',
            ForeColor: 'var(--ab-color-palette-13)',
          },
          Scope: {
            All: true,
          },
          Rule: '[language] = "TypeScript" AND [license] = "MIT License"  ',
        },
      ],
    },
  },
};
```

## Scope

Scope defines **where** the Format Column is applied.

Scope is provided by the commonly-used [Column Scope Object](https://www.adaptabletools.com/docs/dev-guide-columns-scope/index.md) which provides 4 sets of options:

- **All Columns** - The entire Row can be rendered with a Format Column (by providing a Scope of 'All')

- **Columns** - consists of a list of Column Names which will display the Style

- **Column Types** - applies style to all Columns that share a supplied [Column Type](https://www.adaptabletools.com/docs/dev-guide-columns-column-types/index.md)

- **Data Types** - applies style to all Columns which share supplied [Cell Data Type](https://www.adaptabletools.com/docs/dev-guide-aggrid-cell-data-types/index.md) (e.g. `text`, `number` or `date`)

Use this to give all dates a distinctive format, or apply a condition to all numbers, e.g. Green Positive / Red Negative

**Example: Format Column - Scope**

Column Scope in Format Columns

- This demo showcases how to use Scope in Format Columns
  - The `Issue Change` **Column** has 2 Format Conditions applied (Green for Positive and Red for Negative)
  - All Columns of **DataType** `Date` have a Display Format (of 'MMM do yyyy')
  - Columns with a **ColumnType** of 'Github' (i.e. `Github Watchers` & `Github Stars`) are italicised and centre-aligned
  - **Whole Rows** are styled to match an Expression ('[language] = "TypeScript" AND [license] = "MIT License" ')

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

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts
  gridOptions.ts
  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} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Format Column Scope',
  columnOptions: {
    columnTypes: ['github', 'issue-pr'],
  },
  initialState: {
    Dashboard: {ModuleButtons: ['FormatColumn']},
    Theme: {CurrentTheme: 'dark'},
    FormatColumn: {
      FormatColumns: [
        // Column Scope
        {
          Name: 'formatColumn-week_issue_change',
          Style: {
            ForeColor: 'var(--ab-color-palette-19)',
          },
          Scope: {
            ColumnIds: ['week_issue_change'],
          },
          Rule: '[week_issue_change] > 0',
        },
        {
          Name: 'formatColumn-week_issue_change',
          Style: {
            ForeColor: 'var(--ab-color-palette-30)',
          },
          Scope: {
            ColumnIds: ['week_issue_change'],
          },
          Rule: '[week_issue_change] < 0',
        },

        // DataType Scope
        {
          Name: 'formatColumn-date',
          Scope: {
            DataTypes: ['date'],
          },
          DisplayFormat: {
            Formatter: 'DateFormatter',
            Options: {
              Pattern: 'MMM do yyyy',
            },
          },
        },

        // ColumnType Scope
        {
          Name: 'formatColumn-github',
          Scope: {
            ColumnTypes: ['github'],
          },
          Style: {
            FontStyle: 'Italic',
            Alignment: 'Center',
          },
        },

        // Row Scope
        {
          Name: 'style-all',
          Style: {
            BackColor: '#8fd3fe',
            ForeColor: 'var(--ab-color-palette-13)',
          },
          Scope: {
            All: true,
          },
          Rule: '[language] = "TypeScript" AND [license] = "MIT License"  ',
        },
      ],
    },
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'name',
            'license',
            'language',
            'week_issue_change',
            'github_watchers',
            'github_stars',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'open_issues_count',
            'open_pr_count',
            'closed_pr_count',
            'has_projects',
            'has_pages',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```
**`src/columnDefs.ts`**

```ts
import {ColDef} from 'ag-grid-enterprise';
import {WebFramework} from './rowData';

export const columnDefs: ColDef<WebFramework>[] = [
  {
    field: 'name',
    cellDataType: 'text',
    sortable: true,
  },
  {
    field: 'language',
    cellDataType: 'text',
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    field: 'github_stars',
    headerName: 'GitHub Stars',
    cellDataType: 'number',
    type: 'github',
    enableValue: true,
  },
  {
    field: 'github_watchers',
    headerName: 'GitHub Watchers',
    cellDataType: 'number',
    type: 'github',
    enableValue: true,
  },
  {
    field: 'license',
    cellDataType: 'text',
    editable: false,
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    field: 'week_issue_change',
    headerName: 'Issue Change',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'rating',
    headerName: 'Rating',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'created_at',
    headerName: 'Created',
    cellDataType: 'date',
  },
  {
    field: 'has_wiki',
    headerName: 'Has Wiki',
    cellDataType: 'boolean',
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    field: 'updated_at',
    headerName: 'Updated',
    cellDataType: 'date',
  },
  {
    field: 'pushed_at',
    headerName: 'Pushed',
    cellDataType: 'date',
  },
  {
    field: 'description',
    cellDataType: 'text',
    sortable: false,
  },
  {
    field: 'open_issues_count',
    headerName: 'Open Issues',
    cellDataType: 'number',
    type: 'issue-pr',
    enableValue: true,
  },
  {
    field: 'closed_issues_count',
    headerName: 'Closed Issues',
    cellDataType: 'number',
    type: 'issue-pr',
    enableValue: true,
  },
  {
    field: 'open_pr_count',
    headerName: 'Open PRs',
    cellDataType: 'number',
    type: 'issue-pr',
    enableValue: true,
  },
  {
    field: 'closed_pr_count',
    headerName: 'Closed PRs',
    cellDataType: 'number',
    type: 'issue-pr',
    enableValue: true,
  },
  {
    field: 'has_projects',
    headerName: 'Has Projects',
    cellDataType: 'boolean',
  },
  {
    field: 'has_pages',
    headerName: 'Has Pages',
    cellDataType: 'boolean',
  },
  {
    field: 'topics',
    cellDataType: 'textArray',
    editable: false,
    sortable: false,
  },
  {
    field: 'id',
    cellDataType: 'number',
    hide: true,
    editable: false,
  },
];
```
**`src/gridOptions.ts`**

```ts
import {GridOptions, themeBalham} from 'ag-grid-enterprise';

import {rowData, WebFramework} from './rowData';
import {columnDefs} from './columnDefs';

export const gridOptions: GridOptions<WebFramework> = {
  theme: themeBalham,
  defaultColDef: {
    resizable: true,
    sortable: true,
    editable: true,
    filter: true,
    floatingFilter: true,
  },
  columnDefs: columnDefs,
  rowData: rowData,
  sideBar: true,
  suppressMenuHide: true,
  columnMenu: 'new',
  cellSelection: true,
  statusBar: {
    statusPanels: [
      {statusPanel: 'agTotalRowCountComponent', align: 'left'},
      {statusPanel: 'agFilteredRowCountComponent'},
      {
        key: 'Center Panel',
        statusPanel: 'AdaptableStatusPanel',
        align: 'center',
      },
    ],
  },
  columnTypes: {
    github: {},
    'issue-pr': {},
  },
};
```

### Composition

Each Column will render **every Format Column** for which it is in Scope.

This means that a Column can successfully display 2 (or more) Format Columns.

If a Column is in scope for 2 clashing Format Columns, AdapTable renders the Format Column with **higher precedence**

See [Configuring Formatting](https://www.adaptabletools.com/docs/handbook-column-formatting-configuring/index.md) for further details on composition and also order of precedence betweeen Formats

## Condition

By default, a Format Column applies to every cell in scope.

An optional **Rule** limits application to matching rows — commonly for conditional highlighting (e.g. negative P&amp;L, limit breaches).

The same Rule mechanism can also be used for Display Formats (though that is less common).

See [Conditional Styling](https://www.adaptabletools.com/docs/handbook-column-formatting-conditions/index.md) for Expressions, and demos

---

## Using Column Formats

Run-time users are able to add, edit, share, suspend and delete Format Columns using the relevant section in the [Settings Panel](https://www.adaptabletools.com/docs/ui-settings-panel/index.md).

There are no [Toolbars](https://www.adaptabletools.com/docs/ui-dashboard-tabs-toolbars/index.md) or [Tool Panels](https://www.adaptabletools.com/docs/ui-tool-panel/index.md) for Format Columns

A Menu Item in the [Column Menu](https://www.adaptabletools.com/docs/ui-column-menu/index.md) opens the relevant Wizard.

- When <b>Creating</b> Format Columns the `Create Format Column` menu item appears in every [Column Menu](https://www.adaptabletools.com/docs/ui-column-menu/index.md)
- When <b>Editing</b> each existing Format Column has an `Edit Format Column` Column Menu Item

The Format Column Wizard enables creating and editing Format Columns.

### Using the Format Column Wizard

There are many stages when creating a Column Style or Display Format in the AdapTable Wizard:

Choose a name for the Format Column being created.

Specify which rows should be included in the Column Format. Options are:

- Data Rows
- Group Rows
- Row Summaries

This defines where the Format Column style is applied. Options are:

- one (or more) **Columns**
- one (or more) **DataTypes** (e.g. String, Number, Date etc)
- an entire **Row** (Scope of 'All')

See [Guide to Column Scope](https://www.adaptabletools.com/docs/dev-guide-columns-scope/index.md) for more information on this commonly used object

This sets which elements of the Format Column are styled. Options are:

- **Cell** (default)
- **Column Header**

Column Headers cannot use Conditions. They can use a Style and an optional **string** Display Format.

Provide a Condition which states whether or not the Format will be applied.

There are 2 possibilities:

- No Condition - the Format will always be applied
- An Expression - Format is applied based on an [Expression](https://www.adaptabletools.com/docs/adaptable-ql-expression/index.md) which is evaluated by [AdapTableQL](https://www.adaptabletools.com/docs/adaptable-ql-expression/index.md)

Read more about [Expressions](https://www.adaptabletools.com/docs/adaptable-ql-expression/index.md) in the AdapTableQL Guide

Use the Style Component to create an Adaptable Style for the column.

You can specify:

- Fore, Back and Border Colours
- Font Properties: Bold, Italic, Size etc
- Cell Alignment for the Column - Left, Right or Centre

Use this Step if you wish to set a Display Format for the Column.

The available properties differ based on the Column's DataType:

- String - Select from the String properties and / or add a Prefix or Suffix
- Number - Create a Display Format by filling in the form, or select one of the Numeric presets

- Date - Provide a 'Date' pattern (using a [symbol](https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table)) or apply one of the Date presets

If you register [Custom Display Formatters](https://www.adaptabletools.com/docs/handbook-column-formatting-display-format-custom/index.md), their ids appear in the wizard when scope and data type match. [Template](https://www.adaptabletools.com/docs/handbook-column-formatting-display-format-template/index.md) literals are available on string and number formatters via the `Content` option.

Display Formatting is only available if all the Columns in the Scope are all Numeric, all String or all Date

## AG Grid Rendering

AG Grid provides very powerful, custom column display functionality.

These include [Value Getters](https://www.ag-grid.com/javascript-data-grid/value-getters/), [Value Formatters](https://www.ag-grid.com/javascript-data-grid/value-formatters/) and [Cell Components](https://www.ag-grid.com/javascript-data-grid/cell-rendering/) (previously called Cell Renderers)

All of these will work in AdapTable and nothing will be lost.

However despite the richness of the AG Grid rendering capabilities we recommend **not using AG Grid's Value Formatters or Cell Components**, if possible.

Instead use AdapTable's Styling and Formatting features as they integrate more tightly into the tool.

See the [Tutorial on AG Grid Cell Rendering](https://www.adaptabletools.com/docs/dev-guide-aggrid-cell-rendering/index.md) for more information and examples

## UI Entitlements

The UI Entitlements behaviour for Format Columns is as expected for `Full` and `Hidden` [`Access Levels`](https://www.adaptabletools.com/docs/reference/accesslevel.md).

The `ReadOnly` Entitlement behaviour is that the Columns will still be rendered but Users are not permitted to manage or suspend the Format Column definitions.

### FAQ

**Can I apply more than one Format to a column?**
Yes — a column can match several Format Column definitions at once.
**Styles** (and general settings such as alignment) are **merged**; clashing properties follow **precedence** order (see [Configuring Column Formatting](https://www.adaptabletools.com/docs/handbook-column-formatting-configuring/index.md#format-column-precedence)).
**Display formats** are not merged: the definition with **higher precedence** wins when more than one applies.

**Can I use Format Column to display a different caption for the Column?**
Yes, you can format a Column Header including providing a custom Caption.
But it is preferable to do this by using the `ColumnHeaders` property in a [Layout](https://www.adaptabletools.com/docs/handbook-layouts/index.md)

**Why is the Style or Format not being applied in Aggregated values?**
Check that you have not set `ExcludeGroupRows` to _true_ in the `RowScope` property in the Format Column Definition
