# Monitoring Layout Changes

Canonical page: https://www.adaptabletools.com/docs/handbook-layouts-monitoring

- The Layout Changed Event fires whenever Layout State changes
- `actionName` says what triggered the change
- `changedAreas` says which parts of the current Layout changed

[Layouts](https://www.adaptabletools.com/docs/handbook-layouts/index.md) · [Topics in this Section](https://www.adaptabletools.com/docs/handbook-layouts/index.md#topics-in-this-section).

AdapTable fires a **Layout Changed Event** whenever [Layout State](https://www.adaptabletools.com/docs/technical-reference-layouts/index.md) changes.

`LayoutChangedInfo`, the action names and the changed areas are also listed in the [Layout Technical Reference](https://www.adaptabletools.com/docs/technical-reference-layouts/index.md#layout-changed-event)

The Event Info is a single [`LayoutChangedInfo`](https://www.adaptabletools.com/docs/reference/layoutchangedinfo.md) object.

It includes the previous and current Layout State, plus:

- `actionName` — **why** the Event fired (a Layout was selected, saved, filtered, and so on)
- `changedAreas` — **what** on the current Layout actually changed (when the Layout is edited in place)

## Listening to the Event

Subscribe to the Event the same way as with any other [Adaptable Event](https://www.adaptabletools.com/docs/technical-reference-adaptable-events/index.md).

A common way to use the Event is to check the `actionName`, and then make any changes using Layout API

```ts {4}
// This example ensures every Layout has a Grand Total Row at the top.
// `LAYOUT_SELECT` fires when the user switches Layout;
// `LAYOUT_READY` fires once after AdapTable starts, for the Layout that is already current:
api.eventApi.on('LayoutChanged', (eventInfo: LayoutChangedInfo) => {
  if (
    eventInfo.actionName !== 'LAYOUT_SELECT' &&
    eventInfo.actionName !== 'LAYOUT_READY'
  ) {
    return;
  }

  const layout = api.layoutApi.getCurrentLayout();
  if (layout.GrandTotalRow === 'top') {
    return;
  }

  api.layoutApi.updateCurrentLayout(current => ({
    ...current,
    GrandTotalRow: 'top',
  }));
});
```

## Layout Changed Info

The [`LayoutChangedInfo`](https://www.adaptabletools.com/docs/reference/layoutchangedinfo.md) object which is provided by the Layout is defined as follows:

| Property | Type | Description |
| --- | --- | --- |
| [actionName](https://www.adaptabletools.com/docs/reference/layoutchangedinfo.md#actionname) | [`LayoutChangedAction`](https://www.adaptabletools.com/docs/reference/layoutchangedaction.md) | Which action caused this change. See . |
| [changedAreas](https://www.adaptabletools.com/docs/reference/layoutchangedinfo.md#changedareas) | [`LayoutChangedArea`](https://www.adaptabletools.com/docs/reference/layoutchangedarea.md)`[]` | Which parts of the **current** Layout changed.<br/><br/>Use this when you care *what* changed, rather than which `actionName` fired:<br/><br/>- `LAYOUT_SAVE`: diffs non-filter facets (e.g. `ColumnSorting`, `RowGrouping`)<br/>- Column Filter actions: always `['ColumnFilter']`<br/>- Grid Filter actions: always `['GridFilter']`<br/><br/>Omitted for layout identity changes (`LAYOUT_SELECT`, `LAYOUT_ADD`, `LAYOUT_DELETE`, `LAYOUT_READY`). |
| [newLayoutState](https://www.adaptabletools.com/docs/reference/layoutchangedinfo.md#newlayoutstate) | [`LayoutState`](https://www.adaptabletools.com/docs/reference/layoutstate.md) | Layout State after this change. |
| [oldLayoutState](https://www.adaptabletools.com/docs/reference/layoutchangedinfo.md#oldlayoutstate) | [`LayoutState`](https://www.adaptabletools.com/docs/reference/layoutstate.md)` \| undefined` | Layout State before this change.<br/><br/>Absent for `LAYOUT_READY`, which fires once after startup and has no previous state. |
| [adaptableContext](https://www.adaptabletools.com/docs/reference/layoutchangedinfo.md#adaptablecontext) | `any` | Custom application Context provided in `AdaptableOptions.adaptableContext` |

## Layout Actions

Every Layout Changed Event includes an Action, which describes what caused the Layout to change.

This is provided using the `actionName` property which is of type [`LayoutChangedAction`](https://www.adaptabletools.com/docs/reference/layoutchangedaction.md).

These are the Actions which can cause the Layout Changed Event to trigger:

| Action                                                | Description                                                                                        |
| ----------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| `LAYOUT_READY`                       | Fired once after AdapTable starts, for the Layout that is already current                          |
| `LAYOUT_ADD`                         | A Layout has been added                                                                            |
| `LAYOUT_EDIT`                        | A Layout has been edited                                                                           |
| `LAYOUT_SAVE`                        | A Layout has been saved, including auto-save after grid edits (sort, grouping, columns, and so on) |
| `LAYOUT_DELETE`                      | A Layout has been deleted                                                                          |
| `LAYOUT_SELECT`                      | A different Layout has been selected                                                               |
| `LAYOUT_COLUMN_FILTER_ADD`           | A Column Filter has been added                                                                     |
| `LAYOUT_COLUMN_FILTER_EDIT`          | A Column Filter has been edited                                                                    |
| `LAYOUT_COLUMN_FILTER_SET`           | Column Filters have been set                                                                       |
| `LAYOUT_COLUMN_FILTER_CLEAR`         | A Column Filter has been cleared                                                                   |
| `LAYOUT_COLUMN_FILTER_CLEAR_ALL`     | All Column Filters have been cleared                                                               |
| `LAYOUT_COLUMN_FILTER_SUSPEND`       | A Column Filter has been suspended                                                                 |
| `LAYOUT_COLUMN_FILTER_SUSPEND_ALL`   | All Column Filters have been suspended                                                             |
| `LAYOUT_COLUMN_FILTER_UNSUSPEND`     | A Column Filter has been unsuspended                                                               |
| `LAYOUT_COLUMN_FILTER_UNSUSPEND_ALL` | All Column Filters have been unsuspended                                                           |
| `LAYOUT_GRID_FILTER_SET`             | The Grid Filter has been set                                                                       |
| `LAYOUT_GRID_FILTER_CLEAR`           | The Grid Filter has been cleared                                                                   |
| `LAYOUT_GRID_FILTER_SUSPEND`         | The Grid Filter has been suspended                                                                 |
| `LAYOUT_GRID_FILTER_UNSUSPEND`       | The Grid Filter has been unsuspended                                                               |

## Layout Changed Areas

`changedAreas` is for when you care **which part** of the current Layout changed, without enumerating every filter `actionName` or deep-diffing Layout State yourself.

It is only set when the **current** Layout is edited in place:

- `LAYOUT_SAVE` — AdapTable diffs the previous and next Layout and lists the facets that actually changed (sort, grouping, columns, and so on)
- any Column Filter action — always `['ColumnFilter']`
- any Grid Filter action — always `['GridFilter']`

- It is **omitted** for Layout identity changes: `LAYOUT_SELECT`, `LAYOUT_ADD`, `LAYOUT_DELETE` and `LAYOUT_READY`
- Those change _which_ Layout is current (or that a Layout exists), not a facet of the current one

```ts
api.eventApi.on('LayoutChanged', (eventInfo: LayoutChangedInfo) => {
  if (eventInfo.changedAreas?.includes('ColumnSorting')) {
    // sort changed on the current Layout (via LAYOUT_SAVE)
  }
  if (eventInfo.changedAreas?.includes('ColumnFilter')) {
    // any Column Filter action (set, clear, suspend, and so on)
  }
});
```

Column sizing is not included because AG Grid often reports incidental width or flex changes on unrelated edits

The possible values are of type [`LayoutChangedArea`](https://www.adaptabletools.com/docs/reference/layoutchangedarea.md):

| Area            | Description                                              |
| --------------- | -------------------------------------------------------- |
| `Columns`       | Visible columns or column order changed                  |
| `ColumnPinning` | Column pinning changed                                   |
| `ColumnHeaders` | Column captions (or suppress agg func in header) changed |
| `ColumnSorting` | Column sorts changed                                     |
| `RowGrouping`   | Row grouping (table or pivot) changed                    |
| `Aggregations`  | Table or pivot aggregations changed                      |
| `RowSummaries`  | Row summaries changed                                    |
| `RowSelection`  | Layout row-selection settings changed                    |
| `ColumnFilter`  | A Column Filter action fired                             |
| `GridFilter`    | A Grid Filter action fired                               |
| `Pivoting`      | Pivot columns, expand level or pivot totals changed      |

**Example: Layout Changed**

Event triggered when the Layout is changed

- This demo listens to the Layout Changed Event and outputs a [System Status Message](https://www.adaptabletools.com/docs/handbook-system-status-message/index.md) with the `actionName` and any `changedAreas`

- Switch Layout in the Layout toolbar and note `LAYOUT_SELECT`
- Sort or filter a column and note `LAYOUT_SAVE` / Column Filter actions, together with the matching `changedAreas`

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: 'Layout Changed Event',
  initialState: {
    Dashboard: {
      Tabs: [
        {
          Name: 'Demo',
          Toolbars: ['Layout', 'SystemStatus'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'action',
            'name',
            'language',
            'github_stars',
            'license',
            'created_at',
            'has_wiki',
            'updated_at',
            'topics',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
        {
          TableColumns: ['name', 'language', 'license', 'github_stars'],
          Name: 'Languages Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

**`src/onAdaptableReady.ts`**

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

export const onAdaptableReady = ({adaptableApi}: AdaptableReadyInfo) => {
  adaptableApi.toolPanelApi.openAdapTableToolPanel();

  adaptableApi.eventApi.on('LayoutChanged', (info: LayoutChangedInfo) => {
    const areas = info.changedAreas?.length
      ? ` (${info.changedAreas.join(', ')})`
      : '';
    adaptableApi.systemStatusApi.setInfoSystemStatus(
      `Layout Change: ${info.actionName}${areas}`
    );
  });
};
```
