# Table Layouts - Column Sizing

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

- The Table Layout supports providing bespoke Column Widths
- There is also an option to autosize all Columns in the Layout

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

AdapTable allows developers to set (and run-time users to update) the size of AG Grid Columns.

There are 3 main ways that Columns can be sized in AdapTable:

- setting `width` and / or `flex` properties in the AdapTable Layout (using the [`ColumnSizing`](https://www.adaptabletools.com/docs/reference/columnsizingmap.md) object)
- setting the `AutoSizeColumns` property to *true* in the AdapTable Layout
- providing the column sizing in the AG Grid ColumnDef (or DefaultColumnDef)

## Column Sizing Precedence

AdapTable provides a set of precedence rules for evaluating **each Column's** size as follows:

1. ColumnSizing in Layout
  
  If `ColumnSizing` has been provided for a Column in the Layout, then that is used.  Any sizing set in AG Grid `ColumnDefs` for that Column is ignored, as is the Layout's `AutoSizeColumns` property

This rule applies to all 4 properties in the Layout's `ColumnSizing` object: `Width`, `Flex` `MinWidth` and `MaxWidth`

1. AutoSizing in Layout

  If no `ColumnSizing` has been provided for any column, but `AutoSizeColumns` is set to *true*, then all Columns are auto-sized, and any sizing set in any `ColumnDefs` is ignored

3. AG Grid ColumnDefs

  If no `ColumnSizing` has  been provided for a Column in the Layout (and `AutoSizeColumns` is not *true*), AdapTable will size the Column using any sizing (width or flex) set in `ColumnDefs` (either in the Column directly or in `defaultColumnDef`)

## Column Sizing in Layout

Layout Column Sizing is configured using the `ColumnSizing` property.

### Understanding the Column Sizing Objects

The `ColumnSizing` property is of type [`ColumnSizingMap`](https://www.adaptabletools.com/docs/reference/columnsizingmap.md), which is a map of 2 properties:

- `ColumnId` - a string (the id of the Column)
- `ColumnSizingDefinition` - sets the sizing for the Column

The [`ColumnSizingDefinition`](https://www.adaptabletools.com/docs/reference/columnsizingdefinition.md) contains 4 (self explanatory ) properties:

- `Width` (number in pixels)
- `Flex`
- `MinWidth`
- `MaxWidth`

This allows you set the width (or flex) for each Column in the Layout.

Additionally you can set the Minumum Width, Maximum Width and Resizability of each Column.

- One of `Width` or `Flex` must be set (but not both); all other properties are optional
- Column Sizing defined in a Layout **take precedence** over any sizing explicitly set in `columnDefs` in GridOptions

Once the Layout is rendered, AdapTable automatically updates widths in the `ColumnSizing` property, when run time users resize columns manually in the Grid.

### Defining a Layout with Column Sizing

Layouts can include details of **initial Column Widths**.

These will be overridden by changes made in AG Grid UI.

This is provided in the form of map with 2 properties:
- `ColumnId`
- `ColumnSizingDefinition` containing a `Width` property which provides the width of the column in **pixels**

The `ColumnSizingDefinition` can also be used to configure `MinWidth` and `MaxWidth` if required.

As an alternative to setting Widths `ColumnSizingDefinition` can also be used to set the Flex for a Column.

One of Width or Flex **must be set** for each column listed.

All columns are, by default, resizable.

But this can be set to false using the `Resizable` property.

```ts [[1, 14, "Width"],[1, 18, "Width"],[2, 19, "MinWidth"],[2, 20, "MaxWidth"],[3, 23, "Flex"],[3, 26, "Flex"],[4, 15, "Resizability"],[4, 27, "Resizability"]]
// Set sizing for 4 Columns in Layout: 
// Use Width for 2 Columns (name, license)
// Use Flex for other 2 Columns (updated_at, language)
// Turn off Resizability for Name and Language columns
const initialState: InitialState = {
 Layout: {
    CurrentLayout: 'Table Layout',
    Layouts: [
    {
      Name: 'Table Layout',
      TableColumns: ['name', 'license', 'updated_at', 'language', 'open_pr_count'],
      ColumnSizing: {
        name: { 
          Width: 150,
          Resizability: false,
        }, 
        license: { 
          Width: 200, 
          MinWidth: 100, 
          MaxWidth: 300 
        }, 
        updated_at: { 
          Flex: 5,
        }, 
        language: { 
          Flex: 10, 
          Resizability: false
        }, 
    },
    }]
 },
};
```

- Run time users can update this property by using 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 in order to set the width for that Column

**Example: Table Layouts: Column Sizing**

Creating Table Layouts with custom Column Widths & Flex

- This example contains 3 Layouts that each provide different custom column sizing:
  - `Column Widths Layout` - the *Name*, *Language* and *Topics* columns have all been given bespoke widths
  - `Column Flex Layout` - the *Name*, *Language* and *Topics* columns have all been given bespoke flex values
  - `Column AutoSize Layout` - all (visible) columns are auto-sized

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 Sizing',
  initialState: {
    Dashboard: {
      Tabs: [
        {
          Name: 'Default',
          Toolbars: ['Layout'],
        },
      ],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Column Widths Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'topics',
            'github_watchers',
            'open_pr_count',
            'closed_pr_count',
          ],
          Name: 'Column Widths Layout',
          ColumnSizing: {
            name: {Width: 200},
            language: {Width: 118},
            topics: {Width: 700},
          },
        },
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'topics',
            'github_watchers',
            'open_pr_count',
            'closed_pr_count',
          ],
          Name: 'Column Flex Layout',
          ColumnSizing: {
            name: {Flex: 30},
            language: {Flex: 20},
            topics: {Flex: 40},
          },
        },
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'topics',
            'github_watchers',
            'open_pr_count',
            'closed_pr_count',
          ],
          Name: 'Column AutoSize Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

## Auto Sizing in Layout

As an alternative to defining specific Columns Widths, a Layout can have all columns set to be auto sized.

The `AutoSizeColumns` property in the Layout object (available for both Pivot and Table Layouts) auto sizes the Layout the **first time** that it loads.

- AutoSizeColumns works by leveraging AG Grid's own autosizing functionality
- This means that if `suppressColumnVirtualisation` is set to *true* in GridOptions, only **visible** columns are auto sized

It sets each Column's width based on its contents, and saves it to the `ColumnSizing` property (see above).

- `ColumnSizing` has precedence - if that is set for any Column, then `AutoSizeColumns` is ignored completely
- AutoSizing requires the `ColumnAutoSizeModule` [AG Grid Module](https://www.adaptabletools.com/docs/getting-started-aggrid-modules/index.md) is present

### Defining a Layout with Autosized Columns

AdapTable provides an option to **autosize** all Columns in the Layout.

This leverages the AG Grid autosizing functionality.

```ts [[1, 9, "AutoSizeColumns"]]
// Create a Layout with AutoSized Columns
const initialState: InitialState = {
 Layout: {
    CurrentLayout: 'AutoSized Layout',
    Layouts: [
    {
      Name: 'AutoSized Layout',
      TableColumns: ['name', 'license', 'github_stars', 'language', 'open_pr_count'],
      AutoSizeColumns: true,
    }
  ]
 },
};
```

## AG Grid Column Defs

Each Column in AG Grid Column Defs has `width`, `flex`, `minWidth`, `maxWidth` and `initialWidth` properties.

This properties will be used by AdapTable to set Column Sizing, but only if the property is not overrien in Layout `ColumnSizing` (and if Layout `AutoSizeColumns` is not true).

- Usually [ColDef properties which set values](https://www.adaptabletools.com/docs/dev-guide-columns-configuring-coldefs/index.md) are **ignored** in favour of AdapTable Layout equivalents (even if not set)
- However this is an **exception** to that rule, and the properties are applied if not explicitly overridden in a Layout

**Example: Table Layouts: ColDef Widths**

Creating Table Layouts with custom ColDef Widths

- This example contains a Layout where no custom sizing has been set (i.e. neither `ColumnSizing` nor `AutoSizeColumns` are provided)
- However the *Name* and *Language* columns display the bespoke widths they were given in AG Grid ColumnDefs

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 Widths',
  initialState: {
    Dashboard: {
      Tabs: [
        {
          Name: 'Default',
          Toolbars: ['Layout'],
        },
      ],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Column Def Widths',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'topics',
            'open_pr_count',
            'closed_pr_count',
          ],
          Name: 'Column Def Widths',
        },
      ],
    },
  },
};
```

**`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,
    width: 500,
  },
  {
    field: 'language',
    cellDataType: 'text',
    enablePivot: true,
    enableRowGroup: true,
    width: 500,
  },
  {
    field: 'github_stars',
    headerName: 'GitHub Stars',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    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 custom sizing of Columns automatically created by AG Grid.

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

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