The width of the columns can be set using a given value through columnDefs
or columnState
(
see Column Definitions, Updating Column Definitions
and Column State). It is also
possible to let Dash AG Grid calculate the width of the columns with the columnSize
property and allow the user to
resize the columns by dragging the top right portion of the column.
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.
columnDefs = [
{ "field": "name"},
{ "field": "age" },
{ "field": "address", "resizable": False},
]
The snippet below allows only the Address to be resized by setting resizable=False
on the default column
definition and then resizable=True
on the Address column.
defaultColDef = {
"resizable": False,
}
columnDefs = [
{ "field": "name" },
{ "field": "age" },
{ "field": "address", "resizable": True },
]
columnSize
propertyTo set the column size, use the columnSize
property, along with columnSizeOptions
to customize the columnSize
operation.
Note that
columnSize
andcolumnSizeOptions
are Dash properties only. You won’t find these properties defined the
same way in the AG Grid documentation.
columnSize
takes one of the following:
"autoSize"
: changes the column sizes to fit the column’s content,autoSizeColumns
or autoSizeAllColumns
."sizeToFit"
: changes the column sizes to fit the width of the grid,sizeColumnsToFit
."responsiveSizeToFit"
: changes the column sizes to fit the width of the grid and also resizing upon grid or columnsizeColumnsToFit
.None
: bypasses the altering of the column widths.The following example shows how to make columns adjust to fit either the screen or their contents. With each option, try
resizing the columns (by dragging the column header) and resize the browser width to see how the grid responds.
Setting columnSize="autoSize"
will auto-size all columns based on its contents. Note that, just like Excel, each
column can also be auto-resized by double-clicking the right side of the header rather than dragging it.
Notes about autosizing and Virtualisation:
python
dashGridOptions = {"suppressColumnVirtualisation": True}
By default, the grid will also resize the column to fit the header. If you do not want the headers to be included in
the autosize calculation, set the grid option:
dashGridOptions = {"skipHeaderOnAutoSize": True}
Using columnSize="autoSize"
, it is possible to apply the auto-size to only a subset of columns or to not take into
account the headers when calculating the column size, it is possible to use columnSizeOptions
with the following
parameters:
keys
(list of strings; optional): list of column IDs to auto-size. If omitted, all columns will be auto-sized.skipHeader
(boolean; optional): if skipHeader=True
, the header won’t be included when calculating the columnNote that Column Groups are never considered when calculating the column widths.
Setting columnSize="sizeToFit"
makes the columns fit the width of the grid. However, unlike "responsiveSizeToFit"
,
this is only done once when the grid is rendered. If the browser window changes, or the user changes the column width,
the columns will not be continually resized. But it is possible to set again "sizeToFit"
to recalculate the width of
the columns when needed, see
example Size to Fit Triggered by Callback Example
Setting columnSize="responsiveSizeToFit"
makes the currently visible columns fit the screen. The columns will scale (
growing or shrinking) to fit the available width.
Note that when
columnSize="responsiveSizeToFit"
, the 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.
For both columnSize
options, if you don’t want a particular column to be included in the 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 grid.
It is also possible to set minimum and maximum width when calculating the column sizes using columnSizeOptions
with
the following parameters:
columnLimits
(list of dicts; optional): per-column minimum and maximum width, in pixels. columnLimits
is a listkey
(string; optional): column ID to apply minWidth
and maxWidth
.maxWidth
(number; optional): minimum width for this column (does not override the column minimum width).minWidth
(number; optional): maximum width for this column (does not override the column maximum width).defaultMaxWidth
(number; optional): default maximum width, in pixels, if not overridden by columnLimits
.defaultMinWidth
(number; optional): default minimum width, in pixels, if not overridden by columnLimits
.This example shows how to set options with "sizeToFit"
, note the following:
suppressSizeToFit
and is not resized.maxWidth: 50
, which takes precedence over the functions defaultMinWidth: 100
maxWidth: 300
, which takes precedence over the functions minWidth: 900
defined for theThis example adds a button so the user can resize the grid. Try changing a column width or the browser window then
hit the “Resize” button.
If you hold the <kbd>Shift<kbd> 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 behavior for resizing. To have shift resizing as the default and normal resizing to
happen when the <kbd>Shift<kbd> key is pressed, set the grid option:
dashGridOptions = {"colResizeDefault": 'shift'}
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:
resizable=False
.resizable=False
.View the CSS classes used for this example
These CSS classes must be added to any *.css
file in the assets folder.
See Loading CSS files for more information.
span.legend-box {
display: inline-block;
border: 1px solid #aaa;
height: 15px;
width: 15px;
margin-left: 15px;
margin-right: 5px;
}
.resizable-header {
background-color: #66c2a5 !important;
}
.fixed-size-header {
background-color: #e78ac3 !important;
}
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
parameter. Some columns could be set with a regular width
parameter, while other columns
would have a flex
parameter.
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.
The
flex
parameter does not work with awidth
parameter in the same column. If you need to provide a
minimumwidth
for a column, you should useflex
and theminWidth
parameter.flex
will also takemaxWidth
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.
In the example below, note the following:
flex: 2
, minWidth: 200
and maxWidth: 350
, so it should be constrained to this max/min width.flex: 1
so should be half the size of column B, unless column B is being constrained byminWidth
/maxWidth
rules, in which case it should take up the remaining available space.This second example has flex in all columns. Try changing the browser window size and note how the proportions of the
columns
are maintained.