# Switching and Synchronising Layouts

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

- It is possible to synchronise the (decreasing) common properties in Table and Pivot Layouts
- It is **not** advisable to create one Layout and switch between Pivot and Table at runtime

[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).
Table vs pivot definitions — [Table Layouts](https://www.adaptabletools.com/docs/handbook-layouts-table/index.md) and [Pivot Layouts](https://www.adaptabletools.com/docs/handbook-layouts-pivot/index.md).

## Switching Layout Types

It is common for an application to include both Table and Pivot Views.

The temptation for many developers is to create a single Layout and update it each time the user switches view.

That is possible (by using `updateCurrentLayout` in Layout API), but it is not recommended.

- When switching between Pivot and Table view, **try to avoid mutating / updating the same Layout**
- Instead try to have 2 Layouts of a similar name and synchronise the common properties in your code - see below

The Table Layout and Pivot Layout objects are very different, with fewer common properties.

## Synchronising Layouts

Another common use case is to synchronise common elements (e.g. Filters) between Pivot and Table Layouts.

There are a few ways to accomplish this, but perhaps the best is to leverage the [Layout Changed Event](https://www.adaptabletools.com/docs/handbook-layouts-monitoring/index.md) to keep properties in sync between the 2 Layouts.

**Example: Synchronising Layouts**

Using Layout Changed Event to synchronise between Layouts

- In this example we have 3 Layouts (1 Table, 1 Pivot and 1 Row Grouped) and we ensure that Column Filters and the Grid Filter is kept consistent between them

- In any Layout click the "Add Filters" button to create Grid and Columns Filters, and switch Layouts to see how the Filters in that Layout were updated simultaneously 

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 {
  AdaptableButton,
  AdaptableOptions,
  ColumnFilter,
  CustomToolbarButtonContext,
  FilterApi,
  GridFilter,
} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Layout Synchronisation',
  dashboardOptions: {
    customToolbars: [
      {
        name: 'ButtonToolbar',
        title: 'Buttons',
        toolbarButtons: [
          {
            label: 'Add Filters',
            onClick: (
              button: AdaptableButton<CustomToolbarButtonContext>,
              context: CustomToolbarButtonContext
            ) => {
              const filterApi: FilterApi = context.adaptableApi.filterApi;
              const colFilters: ColumnFilter[] = [
                {
                  ColumnId: 'github_stars',
                  Expression: '[github_stars] > 100000',
                },
                {
                  ColumnId: 'language',
                  Expression: '[language] = "JavaScript" OR [language] = "TypeScript"',
                },
              ];
              const expression: string =
                '[closed_pr_count] - [open_pr_count] > 1000';

              filterApi.columnFilterApi.clearAndSetColumnFilters(colFilters);
              filterApi.gridFilterApi.setGridFilterExpression(expression);
            },
            buttonStyle: {
              tone: 'info',
              variant: 'outlined',
            },
          },
        ],
      },
    ],
  },
  initialState: {
    Dashboard: {
      ModuleButtons: ['Layout'],
      PinnedToolbars: ['Layout', 'GridFilter', 'ColumnFilter', 'ButtonToolbar'],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Table Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'license',
            'language',
            'github_stars',
            'open_pr_count',
            'closed_pr_count',
            'created_at',
            'updated_at',
            'pushed_at',
            'created_at',
            'has_wiki',
            'github_watchers',
            'description',
            'open_issues_count',
            'closed_issues_count',
            'has_projects',
            'has_pages',
            'week_issue_change',
          ],
          Name: 'Table Layout',
          AutoSizeColumns: true,
        },
        {
          Name: 'Pivot Layout',
          PivotColumns: ['language'],
          PivotGroupedColumns: ['license'],
          SuppressAggFuncInHeader: true,
          PivotAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],
        },
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'open_pr_count',
            'closed_pr_count',
            'created_at',
            'updated_at',
            'pushed_at',
            'license',
            'has_wiki',
            'github_watchers',
            'description',
            'open_issues_count',
            'has_projects',
            'has_pages',
            'week_issue_change',
          ],
          RowGroupedColumns: ['language'],
          RowGroupValues: {
            RowGroupDefaultBehavior: 'always-expanded',
          },
          Name: 'Grouped Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```
**`src/onAdaptableReady.ts`**

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

export const onAdaptableReady = ({adaptableApi}: AdaptableReadyInfo) => {
  adaptableApi.eventApi.on('LayoutChanged', () => {
    const currentLayout = adaptableApi.layoutApi.getCurrentLayout();

    // update all Layouts except current one to have same Column Filters & Grid Filter
    adaptableApi.layoutApi.getLayouts().forEach(layout => {
      if (layout.Name === currentLayout.Name) {
        return;
      }

      adaptableApi.layoutApi.createOrUpdateLayout({
        ...layout,
        ColumnFilters: currentLayout.ColumnFilters,
        GridFilter: currentLayout.GridFilter,
      });
    });
  });
};
```
