Core Features

Advanced Features

Vue Data GridColumn Sizing

Version 36.2.0

Columns can be resized by dragging the right edge of the column header or by using the keyboard.

Sizing Copy Link

Column resizing is enabled by default for all columns. To control resizing for individual columns, set the boolean resizable property in the column definitions.

The snippet below allows all columns except Address to be resized.

<ag-grid-vue
    :columnDefs="columnDefs"
    /* other grid options ... */>
</ag-grid-vue>

this.columnDefs = [
    { field: 'name' },
    { field: 'age' },
    { field: 'address', resizable: false },
];

The snippet below shows how to only allow the Address column to be resized by setting resizable=false on the default column definition and then resizable=true on the Address column.

<ag-grid-vue
    :defaultColDef="defaultColDef"
    :columnDefs="columnDefs"
    /* other grid options ... */>
</ag-grid-vue>

this.defaultColDef = {
    resizable: false,
};
this.columnDefs = [
    { field: 'name' },
    { field: 'age' },
    { field: 'address', resizable: true },
];

Column Flex Copy Link

It's often required that one or more columns fill the entire available space in the grid. For this scenario, it is possible to use the flex config. Some columns could be set with a regular width config, while other columns would have a flex config.

Flex sizing works by dividing the remaining space in the grid among all flex columns in proportion to their flex value. For example, suppose the grid has a total width of 450px and it has three columns: the first with width: 150; the second with flex: 1; and third with flex: 2. The first column will be 150px wide, leaving 300px remaining. The column with flex: 2 has twice the size with flex: 1. So final sizes will be: 150px, 100px, 200px.

If a column has no width or flex properties set, it will default to 200px.

The flex config does not work with a width config in the same column. If you need to provide a minimum width for a column, you should use flex and the minWidth config. Flex will also take maxWidth into account.

If you manually resize a column with flex either via the API or by dragging the resize handle, flex will automatically be disabled for that column.

The example below shows flex in action. Things to note are as follows:

  • Column A is fixed size. You can resize it with the drag handle and the other two columns will adjust to fill the available space
  • Column B has flex: 2, minWidth: 200 and maxWidth: 350, so it should be constrained to this max/min width.
  • Column C has flex: 1 so should be half the size of column B, unless column B is being constrained by its minWidth/maxWidth rules, in which case it should take up the remaining available space.

Auto-Sizing Columns Copy Link

Columns can be auto-sized in two main ways:

  1. Auto-size columns to fit grid - The columns will scale to fit the available grid width (or a provided width if desired).
  2. Auto-size columns to fit cell contents - The columns will resize to fit their visible cell contents.

Auto-Size Columns to Fit Grid Copy Link

Columns can be resized to fit the width of the grid. The columns will scale (growing or shrinking) to fit the available width unless they have suppressSizeToFit=true. In the example below, the Athlete column will not be affected when columns are sized to fit the grid because it's setting suppressSizeToFit=true.

Provide the grid option autoSizeStrategy to size the columns to fit when the grid is loaded. This can either be set to size to the actual grid width (type = 'fitGridWidth'), or to a fixed width that is provided (type = 'fitProvidedWidth').

<ag-grid-vue
    :autoSizeStrategy="autoSizeStrategy"
    /* other grid options ... */>
</ag-grid-vue>

this.autoSizeStrategy = {
    type: 'fitGridWidth',
    defaultMinWidth: 100,
    columnLimits: [
        {
            colId: 'country',
            minWidth: 900
        }
    ]
};
autoSizeStrategyCopy Link
AutoSizeStrategy
Auto-size the columns when the grid is loaded. Can size to fit the grid width, fit a provided width, or fit the cell contents.
animateColumnResizingCopy Link
boolean
default: false
Set to true to animate changes to column width when auto-sizing the columns.

The columns can also be sized on demand via api.sizeColumnsToFit(params).

sizeColumnsToFitCopy Link
Function
Adjusts the size of columns to fit the available horizontal space. Note: it is not recommended to call this method rapidly e.g. in response to window resize events or as the container size is animated. This can cause the scrollbar to flicker. Use column flex for smoother results. If inferring cell data types with custom column types and row data is initially empty or yet to be set, the column sizing will happen asynchronously when row data is added. To always perform this synchronously, set cellDataType = false on the default column definition.

If you don't want a particular column to be included in the auto-resize, then set the column definition suppressSizeToFit=true. This is helpful if, for example, you want the first column to remain fixed width, but all other columns to fill the width of the table.

The grid calculates new column widths while maintaining the ratio of the column default widths. So for example if Column A has a default size twice as wide as Column B, then after the sizing is performed, Column A will still be twice the size of Column B, assuming no column min-width or max-width constraints are violated.

Column default widths, rather than current widths, are used while calculating the new widths. This ensures the result is deterministic and does not depend on any column resizing the user may have manually done.

A parameters object can be provided with minimum and maximum widths, either for all columns or for specific columns, to further restrain the column's resulting width from that function call. These widths will not exceed the column's defined minimum and maximum widths.

For example assuming a grid with three columns, the algorithm will be as follows:

scale = availableWidth / (w1 + w2 + w3)

w1 = round(w1 * scale)

w2 = round(w2 * scale)

w3 = totalGridWidth - (w1 + w2)

Assuming the grid is 1,200 pixels wide and the columns have default widths of 50, 120 and 300, then the calculation is as follows:

availableWidth = 1,198 (available width is typically smaller as the grid typically has left and right borders)

scale = 1198 / (50 + 120 + 300) = 2.54

col1 = round(50 * 2.54) = 127

col2 = round(120 * 2.54) = 306

col3 = 1198 - (127 + 306) = 765 (the last column gets any space that's left, which ensures all space is used, so no rounding issues)

Auto-Size Columns to Fit Cell Contents Copy Link

Columns can be resized to fit the contents of the cells. By default the grid will resize the column to fit the header. If you do not want the headers to be included in the auto-size calculation, set the grid property skipHeaderOnAutoSize = true, or pass skipHeader = true to the autoSizeStrategy params or the API method. If you don't want a particular column to be included in the auto-resize, then set the column definition suppressAutoSize = true. The grid also provides the scaleUpToFitGridWidth option which proportionally scales up columns to fill any empty space in the grid after autosizing them.

The example below demonstrates the use of autoSizeStrategy to size the columns by default. The example button can reapply this sizing via the API at any time. There are also controls provided to toggle on the skipHeader and scaleUpToFitGridWidth parameters. The "Athlete" column has suppressAutoSize = true.

Provide the grid option autoSizeStrategy with type = 'fitCellContents' to size the columns to fit their content when the first data is rendered in the grid.

<ag-grid-vue
    :autoSizeStrategy="autoSizeStrategy"
    /* other grid options ... */>
</ag-grid-vue>

this.autoSizeStrategy = {
    type: 'fitCellContents',
};
autoSizeStrategyCopy Link
AutoSizeStrategy
Auto-size the columns when the grid is loaded. Can size to fit the grid width, fit a provided width, or fit the cell contents.
animateColumnResizingCopy Link
boolean
default: false
Set to true to animate changes to column width when auto-sizing the columns.

By default the Autosize This Column and Autosize All Columns actions in the Column Menu size to cell contents without the strategy's options. Set applyToUiActions to have them reuse the strategy instead, including scaleUpToFitGridWidth, skipHeader and any column limits.

<ag-grid-vue
    :autoSizeStrategy="autoSizeStrategy"
    /* other grid options ... */>
</ag-grid-vue>

this.autoSizeStrategy = {
    type: 'fitCellContents',
    scaleUpToFitGridWidth: true,
    applyToUiActions: true
};

In the example below, narrow the columns and then run Autosize All Columns from the column menu. The columns scale back up to fill the grid, as the strategy specifies.

This can also be performed on demand via the following API methods:

autoSizeColumnsCopy Link
Function
Auto-sizes columns based on their contents. If inferring cell data types with custom column types and row data is initially empty or yet to be set, the column sizing will happen asynchronously when row data is added. To always perform this synchronously, set cellDataType = false on the default column definition.
autoSizeAllColumnsCopy Link
Function
Auto-sizes columns based on their contents. If inferring cell data types with custom column types and row data is initially empty or yet to be set, the column sizing will happen asynchronously when row data is added. To always perform this synchronously, set cellDataType = false on the default column definition.

Column Groups are never considered when calculating the column widths.

Just like Excel, each column can also be auto-resized by double clicking the right side of the header rather than dragging it. When you do this, the grid will work out the best width to fit the contents of the cells in the column.

  • The grid works out the best width by considering the virtually rendered rows only. For example, if your grid has 10,000 rows, but only 50 rendered due to virtualisation of rows, then only these 50 will be considered for working out the width to display. The rendered rows are all the rows you can see on the screen through the vertical scroll plus a small buffer (default buffer size is 20). With Continuous Auto-Sizing opted into scroll-driven re-sizes, the width is worked out again from the rows rendered at each new scroll position.
  • The same applies across columns. Column Virtualisation renders only the columns the horizontal scroll position makes visible, so a grid of 1,000 columns may have only 10 rendered, and the grid can measure only what it renders. A one-off auto-size therefore leaves off-screen columns untouched. To size them, either set suppressColumnVirtualisation=true so every column is rendered and measured in one go, or keep virtualisation and use Continuous Auto-Sizing with scroll-driven re-sizes, which fits each column as the scroll brings it into view.
  • Note that Pinned Columns, the Selection Column and the Row Numbers column will not be scaled up to fill any empty space when using scaleUpToFitGridWidth.

Continuous Auto-Sizing Copy Link

By default autoSizeStrategy is applied once, when the grid first renders. Set continuous to re-apply it whenever the grid changes in a way that affects column widths.

<ag-grid-vue
    :autoSizeStrategy="autoSizeStrategy"
    /* other grid options ... */>
</ag-grid-vue>

this.autoSizeStrategy = {
    type: 'fitCellContents',
    continuous: true,
};
  • fitCellContents re-sizes when the row data or the displayed columns change.
  • fitGridWidth and fitProvidedWidth re-size when the available grid width or the displayed columns change, including changes caused by pagination or scrollbars.
  • Re-sizes are debounced while the grid is being resized.

The example below spreads the grid width across five columns with fitGridWidth:

  • Add or remove a column and the rest give up or take back space.
  • Add rows until a vertical scrollbar appears and the columns re-fit to the width it leaves behind.
  • Narrow the grid itself and the columns follow, still filling it exactly.

The example sets animateColumnResizing so each re-size transitions instead of jumping to the new widths.

Controlling Each Re-Size Copy Link

shouldAutoSizeColumns is called before each re-size, with the eligible columns and a reason of 'dataChanged', 'columnsChanged', 'viewportChanged' or 'gridSizeChanged'. Return false to skip that re-size. It has no effect unless continuous is true.

<ag-grid-vue
    :autoSizeStrategy="autoSizeStrategy"
    /* other grid options ... */>
</ag-grid-vue>

this.autoSizeStrategy = {
    type: 'fitCellContents',
    continuous: true,
    // re-size on new data only, so the widths hold still as the grid is resized
    shouldAutoSizeColumns: ({ reason }) => reason === 'dataChanged',
};

The callback is passed the following params:

reasonCopy Link
'dataChanged' | 'columnsChanged' | 'viewportChanged' | 'gridSizeChanged'
The grid change that triggered this re-size.
All eligible columns — those the user has not resized, and not otherwise excluded. For fitCellContents, the grid may only be able to measure the subset that is currently rendered.
The grid api.
Application context as set on gridOptions.context.

Scrolling Copy Link

Scrolling never re-sizes columns by default, in either direction. fitCellContents can opt in: vertical scrolling brings new rows into view and horizontal scrolling brings new columns into view, and both change what there is to measure. Provide shouldAutoSizeColumns and allow the 'viewportChanged' reason.

<ag-grid-vue
    :autoSizeStrategy="autoSizeStrategy"
    /* other grid options ... */>
</ag-grid-vue>

this.autoSizeStrategy = {
    type: 'fitCellContents',
    continuous: true,
    // re-size as rows and columns scroll into view, as well as on data and column changes
    shouldAutoSizeColumns: () => true,
};

Scroll-driven re-sizes are debounced, so the columns settle once the gesture stops. fitGridWidth and fitProvidedWidth are not affected by scrolling.

The example below sizes a grouped, paginated grid of 34 columns and over 8,000 rows with fitCellContents. Scrolling right fits the remaining columns as they arrive. The medal columns at the far end are narrow, and stay narrow. The text columns hold sentences of differing lengths, so moving between pages or changing the page size visibly re-fits them to the rows now on screen.

Column Width Ownership Copy Link

Continuous sizing never changes the width of a column the user has sized themselves. A column becomes theirs through a header drag resize, a keyboard resize or a double-click auto-size, and through applyColumnState with an explicit width. Their width is then treated as fixed, so the width-distribution strategies hold the column out of the distribution and share the remaining space between the rest.

width and initialWidth in a column definition are both only starting widths: the grid may re-size either of them. To hold a column at a fixed width, set suppressAutoSize for fitCellContents or suppressSizeToFit for the width-distribution strategies.

Also excluded are flex columns and the special Selection and Row Numbers columns.

When scaleUpToFitGridWidth is also set, only the eligible columns are scaled, and the grid shows a horizontal scrollbar when the fixed widths alone cannot fit.

Ownership is released by api.resetColumnState(), which returns every column to its column definition and so makes the user-sized ones eligible again. Note that it also resets sort, order, visibility and pivot state.

In the example below

  • "Athlete" column sets suppressAutoSize, so it does not re-size
  • Press "Next Values" to load values of a different length.
  • Manually re-size the "Sport" column and note how it keeps the width you gave it while changing the values.
  • Click "Reset Column State" to have the "Sport" column auto size again.

Two limits apply to continuous fitCellContents:

  • It is subject to the same virtualisation limits as a one-off auto-size: only the rendered cells can be measured, so columns are sized progressively as scrolling renders more content.
  • It is not supported by the Viewport Row Model, where the grid logs a warning and ignores the option.

Resize via Keyboard Copy Link

Column headers can be resized using the keyboard. When a column header is focused, press ⌥ Alt + / to resize the column in that direction.

See Column Header Navigation for a full list of header keyboard interactions.

Shift Resizing Copy Link

If you hold the ⇧ Shift key while dragging the resize handle, the column will take space away from the column adjacent to it. This means the total width for all columns will be constant.

You can also change the default behaviour for resizing. Set the grid property colResizeDefault='shift' to have shift resizing as the default and normal resizing to happen when the ⇧ Shift key is pressed.

In the example below, note the following:

  • Grid property colResizeDefault='shift' so default column resizing will behave as if ⇧ Shift key is pressed.
  • Holding down ⇧ Shift will then resize the normal default way.

Resizing Groups Copy Link

When you resize a group, it will distribute the extra room to all columns in the group equally. In the example below the groups can be resized as follows:

  • The group 'Everything Resizes' will resize all columns.
  • The group 'Only Year Resizes' will resize only year, because the other columns have resizable=false.
  • The group 'Nothing Resizes' cannot be resized at all because all the columns in the groups have resizable=false.