# Table Layouts - Column Pinning

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

- The Table Layout fully supports AG Grid Pinning

[Layouts](https://www.adaptabletools.com/docs/handbook-layouts/index.md) · [Table Layouts](https://www.adaptabletools.com/docs/handbook-layouts-table/index.md) · [All Table Layout Topics](https://www.adaptabletools.com/docs/handbook-layouts-table/index.md#table-layout-topics)

Table (and Pivot) Layouts can also include **Pinning** information.

You can pin to the left or right and include multiple columns if required.

Column Pinning in AdapTable can be set in 3 ways:
- In the Layout definition (in Layout Initial State) using the `ColumnPinning` property (see below)

Column Pinning defined in a Layout **take precedence** over pinning set in `columnDefs` in GridOptions 

- In the [Table Layout UI Wizard](https://www.adaptabletools.com/docs/handbook-layouts-wizard-table/index.md)
  

Click the expander next to any Column in the `Columns` step, to access the Pinning dropdown for that Column

- Via the `Pin Column` menu item in the AG Grid Column Menu 

There is no fourth option to set pinning directly in ColDefs - any pinning configured there is ignored by AdapTable

### Defining a Layout with Pinning

A Layout definition also allows for Column pinning information to be provided.

The `ColumnPinning` property (of type [`ColumnDirectionMap`](https://www.adaptabletools.com/docs/reference/columndirectionmap.md)) is an array containing 2 properties:

- `ColumnId` - a string (the id of the Column)
- the pinning location - can be either `left` or `right`

```ts [[1, 9, "ColumnPinning"]]
// Pins the name column to the left and updated_at to the right
const initialState: InitialState = {
 Layout: {
    CurrentLayout: 'Pinning Layout',
    Layouts: [
    {
      Name: 'Pinning Layout',
      TableColumns: ['name', 'license', 'github_stars', 'language', 'open_pr_count'],
      ColumnPinning: {
        name: 'left',
        updated_at: 'right',
      },
    }
  ]
 },
};
```

**Example: Table Layouts: Column Pinning**

Creating Table Layouts with Column Pinning

- This example contains a `Pinned Columns` Layout which has pinned the *Name* and *Language* columns to the *left* and `Github Watchers` column to the *right*

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: 'Table Layouts Column Pinning',
  initialState: {
    Dashboard: {
      ModuleButtons: ['Layout'],
      Tabs: [
        {
          Name: 'Default',
          Toolbars: ['Layout', 'GridFilter'],
        },
      ],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    CustomSort: {
      CustomSorts: [
        {
          Name: 'customSort-license',
          ColumnId: 'license',
          SortedValues: ['Other'],
        },
      ],
    },
    Layout: {
      CurrentLayout: 'Pinned Columns',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'license',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'github_watchers',
            'description',
            'open_issues_count',
            'closed_issues_count',
            'open_pr_count',
            'closed_pr_count',
            'has_projects',
            'has_pages',
            'week_issue_change',
          ],
          Name: 'Pinned Columns',
          ColumnPinning: {
            name: 'left',
            language: 'left',
            github_watchers: 'right',
          },
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

## AG Grid Column Defs

Each Column in AG Grid Column Defs has a `pinned` property which can be set to 'left' or 'right'.

This property is ignored by AdapTable, however, when displaying columns.

Only pinning set in the `ColumnPinning` property in the Layout is respected.

Do not set pinning in AG Grid ColDefs as this will be ignored by AdapTable

**Example: Table Layouts: ColDef Pinning**

Creating Table Layouts with custom ColDef Pinning

- This example contains a Layout where no pinning has been configured, but the ColDefs has pinning on 3 Columns
- The ColDefs pinning is ignored by AdapTable and the columns are not pinned

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

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts
  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: 'Table Layouts ColDef Pinning',
  initialState: {
    Dashboard: {
      Tabs: [
        {
          Name: 'Default',
          Toolbars: ['Layout'],
        },
      ],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Column Def Pinning',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'topics',
            'open_pr_count',
            'closed_pr_count',
          ],
          Name: 'Column Def Pinning',
        },
      ],
    },
  },
};
```

**`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,
    resizable: false,
    pinned: 'left',
  },
  {
    field: 'language',
    cellDataType: 'text',
    enablePivot: true,
    enableRowGroup: true,
    pinned: 'left',
  },
  {
    field: 'github_stars',
    headerName: 'GitHub Stars',
    cellDataType: 'number',
    pinned: 'right',
  },
  {
    field: 'github_watchers',
    headerName: 'GitHub Watchers',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'license',
    cellDataType: 'text',
    editable: false,
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    field: 'week_issue_change',
    headerName: 'Issue Change',
    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',
    enableValue: true,
  },
  {
    field: 'closed_issues_count',
    headerName: 'Closed Issues',
    cellDataType: 'number',
    enableValue: true,
  },

  {
    field: 'open_pr_count',
    headerName: 'Open PRs',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'closed_pr_count',
    headerName: 'Closed PRs',
    cellDataType: 'number',
    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,
  },
];
```

## Autogenerated Columns

AdapTable supports pinning of Columns automatically created by AG Grid.

This is typically done by adding the autogenerated ColumnId to the `ColumnPinning` property in the Layout.

- See [Pinning Row Groups](https://www.adaptabletools.com/docs/handbook-grouping-rows/index.md) for instructions on pinning Row Grouped Columns
- See [Pinning Tree Data Grid](https://www.adaptabletools.com/docs/handbook-tree-data-grid/index.md#pinning) for instructions on pinning Tree Data key columns
