# Table Layouts - Column Order

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

- The Table Layout provides a list of Columns in the `TableColumns` property
- These Columns are displayed in the Grid in the order they are listed
- They can be either Columns provided in AG Grid ColDefs or AdapTable Special Columns

[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)

Every Table Layout must include a `TableColumns` property (a string array of Column Ids).

This lists every column to be displayed in AG Grid when the Layout loads - in the **order provided**.

- This is a **mandatory** property (though an empty array can be provided if you wish to display no Columns initially)
- The only other mandatory property is `Name` - how the Layout is referred to

### Defining Layout Columns

The `Name` is how the Layout is referred to in the AdapTable UI

The `TableColumns` property is an array of (string) Column Ids, displayed in the order provided here.

Any Columns not listed here, but provided in ColumnDefs, will still be available in the Layout Wizard (and also the Column ToolPanel).

Use `ColumnVisibility` property to define [which Columns are invisible](https://www.adaptabletools.com/docs/handbook-layouts-table-column-visibility/index.md) when Layout first runs (while maintaining their order if made visible)

Set one Layout as `Current Layout`

This Layout will be automatically applied at startup

```ts [[3, 6, "CurrentLayout"], [1, 9, "Name"], [2, 10, "TableColumns"]]
// Create a Layout named 'Basic Layout'
// Make that Layout the Current Layout
// Add 7 Columns
const initialState: InitialState = {
 Layout: {
    CurrentLayout: 'Basic Layout',
    Layouts: [
    {
      Name: 'Basic Layout',
      TableColumns: [
        'name',
        'github_stars',
        'github_watchers',
        'language',
        'open_pr_count',
        'closed_pr_count',
        'updated_at',
      ],
    }
  ]
 },
```

Any Columns which are not listed in the Layout will not be (initially) rendered in the Grid.

- Non-listed Columns **will** be included in the Columns Tool Panel in the position they appear in AG Grid ColDefs
- This means that when the column is checked in the ToolPanel, it will appear in the Grid in that position

**Example: Table Layouts: Column Order**

Creating Table Layouts with default Column Order

- This example lists many columns in `TableColumns` property of Layout but with 2 Columns missing: `Github Stars` and `License`
- These columns are therefore not visible in the Grid, and are placed in the Columns Tool Panel in the order they appear in ColDefs (positions 3 and 10 respectively)

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: 'Table Layouts Column Order',
  initialState: {
    Dashboard: {
      ModuleButtons: ['Layout'],
      Tabs: [
        {
          Name: 'Default',
          Toolbars: ['Layout', 'GridFilter'],
        },
      ],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},

    Layout: {
      CurrentLayout: 'Column Order',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            '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: 'Column Order',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

**`src/onAdaptableReady.ts`**

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

export const onAdaptableReady = (info: AdaptableReadyInfo) => {
  info.agGridApi.openToolPanel('columns');
};
```

## Special Columns

AdapTable provides 3 Special Columns which are not listed in ColDefs but which can appear in the Grid:

- [Calculated Columns](https://www.adaptabletools.com/docs/handbook-calculated-column/index.md)
- [FreeText Columns](https://www.adaptabletools.com/docs/handbook-freetext-column/index.md)
- [Action Columns](https://www.adaptabletools.com/docs/handbook-action-column/index.md)

Each of these Special Columns has a `ColumnId` which, if included in `TableColumns`, will display the Column.

### Adding Special Columns

Create the Special Columns in Initial AdapTable Stat.

These can be any of:

- [Calculated Columns](https://www.adaptabletools.com/docs/handbook-calculated-column/index.md)
- [FreeText Columns](https://www.adaptabletools.com/docs/handbook-freetext-column/index.md)
- [Action Columns](https://www.adaptabletools.com/docs/handbook-action-column/index.md)

Add the Ids of the Special Columns to the `TableColumns` property

```ts [[2, 12, "total_pr_count"], [2, 13, "comments"], [1, 18, "CalculatedColumn"], [1, 28, "FreeTextColumn"]]
// Create a Layout which includes a Calculated Column and a FreeText Column
const initialState: InitialState = {
  Layout: {
    CurrentLayout: 'Special Cols Layout',
    Layouts: [
      {
        Name: 'Special Cols Layout',
        TableColumns: [
          'name',
          'github_stars',
          'github_watchers',
          'total_pr_count',
          'comments',
        ],
      },
    ],
  },
  CalculatedColumn: {
    CalculatedColumns: [
      {
        FriendlyName: 'Total PRs',
        ColumnId: 'total_pr_count',
        Expression: '[open_pr_count] * 2',
        CalculatedColumnSettings: {DataType: 'number'},
      },
    ],
  },
  FreeTextColumn: {
    FreeTextColumns: [
      {
        ColumnId: 'comments',
        FriendlyName: 'Comments',
        FreeTextColumnSettings: {DataType: 'text'},
      },
    ],
  },
};
```

### FAQ

**I listed the Column in TableColumns but I dont see it when my Grid loads**
Check that the Column has not been hidden. This can happen in 2 ways:

- by being hidden (via the Layout's `ColumnVisibility` prop), in which case it can be made visible again
- by being set to be [always hidden](https://www.adaptabletools.com/docs/dev-guide-columns-hiding-columns/index.md) (in which case it can be never be made visible)
