Each column in the grid is defined using a column definition. Columns are positioned in the grid according to the order
ColDefs are specified in the grid options.
The following example shows a simple grid with 3 columns defined:
See Column Properties for a list of all properties that can be applied to a column.
If you want the columns to be grouped, then you include them as groups like the following:
Groups are explained in more detail in the section Column Groups.
Custom Column Types
In addition to the above, the grid provides additional ways to help simplify and avoid duplication of column definitions. This is done through the following:
- defaultColDef: contains column properties all columns will inherit.
- defaultColGroupDef: contains column group properties all column groups will inherit.
- columnTypes: specific column types containing properties that column definitions can inherit.
Default columns and column types can specify any of the column properties available on a column.
The following code snippet shows these three properties configures:
When the grid creates a column it starts with the default column, then adds in anything from the column type, then finally adds in items from the column definition.
For example, the following is an outline of the steps used when creating 'Col C' shown above:
The following examples demonstrates this configuration.
Provided Column Types
Right Aligned and Numeric Columns
The grid provides a handy shortcut for aligning columns to the right.
Setting the column definition type to
rightAligned aligns the column header and contents to the right,
which makes the scanning of the data easier for the user.
numericColumnthat can be used to align the header and cell text to the right.
Each column generated by the grid is given a unique ID. Parts of the Grid API use Column IDs (column identifiers).
If you are using the API and the columns IDs are a little complex (e.g. if two columns have the same
field, or if you are using
valueGetter instead of
field) then it is useful to
understand how columns IDs are decided.
If the user provides
colId in the column definition, then this is used, otherwise the
is used. If both
colId gets preference. If neither
field then numeric is provided. Then finally the ID ensured to be unique by
n is the first positive number that allows uniqueness.
In the example below, columns are set up to demonstrate the different ways IDs are generated. Open the example in a new tab and observe the output in the dev console. Note the following:
Col 1 and Col 2 both use
colId. The grid appends
'_1'to Col 2 to make the ID unique.
Col 3 and Col 4 both use
field. The grid appends
'_1'to Col 4 to make the ID unique.
Col 5 and Col 6 have neither
fieldso the grid generates column IDs.
Saving and Restoring Column State
It is possible to save and subsequently restore the column state via the Column API. Examples of state include column visibility, width, row groups and values.
This is primarily achieved using the following methods:
columnApi.getColumnState(): Returns the state of a particular column.
columnApi.setColumnState(state): To set the state of a particular column.
The column state used by the above methods is an array of objects that mimic the
colDefs which can be
converted to and from JSON. An example is shown below:
The values have the following meaning:
colId: The ID of the column.
aggFunc: If this column is a value column, this field specifies the aggregation function. If the column is not a value column, this field is
trueif the column is hidden, otherwise
rowGroupIndex: The index of the row group. If the column is not grouped, this field is
null. If multiple columns are used to group, this index provides the order of the grouping.
width: The width of the column. If the column was resized, this reflects the new value.
pinned: The pinned state of the column. Can be either
columnApi.setColumnState(state), and also
gridOptions.suppressSetColumnStateEvents = true.
Column API Example
The example below shows using the grid's Column API.
It is possible to add and remove columns after the grid is created. This is done by
api.setColumnDefs() or setting the bound property
When new columns are set, the grid will compare with current columns and work out which columns are old (to be removed), new (new columns created) or kept (columns that remain will keep their state including position, filter and sort).
Comparison of column definitions is done on 1) object reference comparison and 2)
column ID eg
colDef.colId. If either the object reference matches, or
the column ID matches, then the grid treats the columns as the same column. For example
if the grid has a column with ID
'country' and the user sets new columns, one of which
also has ID of
'country', then the old country column is kept in place of the new one
keeping its internal state such as width, position, sort and filter.
The example below demonstrates changing columns. Select the checkboxes for the columns to display and hit Apply. Note the following:
- Column Width: If you change the width of a column (e.g. Year) and then add or remove other columns (e.g. remove Age) then the width of Year remains unchanged.
- Column Sort: If you sort the data by a column (e.g. Year) and then add or remove other columns (e.g. remove Age) then the sort remains unchanged. Conversely if you remove a column with a sort (e.g. remove Year while also sorting by Year) then the sort order is removed.
- Column Filter: If you filter the data by a column (e.g. Year) and then add or remove other columns (e.g. remove Age) then the filter (on Year) remains unchanged. Conversely if you remove a column with a filter (e.g. remove Year while also filtering on Year) then the filter is removed.
- Row Group & Pivot: If you row group or pivot the data by a column (e.g. Year) and then add or remove other columns (e.g. remove Age) then the row group or pivot remains unchanged. Conversely if you remove a column with a row group or pivot (e.g. remove Year while also row grouping or pivoting on Year) then the row group or pivot is removed.
- The Columns Tool Panel and Filters Tool Panel updates with the new columns. The order of columns in both tool panels will always match the order of the columns supplied in the column definitions. To observe this, hit the Reverse button which does same as Apply but reverses the order of the columns first. This will result in the columns appearing in the tool panels in reverse order.
By default when new columns are loaded into the grid, the following properties are not used:
- Column Order
- Aggregation Function (
- Width (
- Pivot (
- Row Group (
- Pinned (
For example, suppose the application user rearranges the order of the columns. If the application then sets new column definitions for the purposes of adding one extra column into the grid, it would be a bad user experience to reset the order of all the columns.
Likewise if the user changes an aggregation function, or the width of a column, or whether a column was pinned, all of these changes the user does should not be undone because the application decided to update the column definitions.
To change this behaviour and have column attributes above (order, width, row group etc) take effect
each time the application updates the grid columns, set the grid property
The responsibility is then on your application to make sure the provided column definitions are in sync
with what is in the grid if you don't want undesired visible changes - e.g. if the user changes the width
of a column, the application should listen to the grid event
columnWidthChanged and update
the application's column definition with the new width - otherwise the width will reset back to the default
after the application updates the column definitions into the grid.
The example below demonstrates Immutable Column mode. Note the following:
immutableColumnsis set to
- Each button sets a different list of columns into the grid. Because each column definition provides an ID, the grid knows the instance of the column is to be kept. This means any active sorting or filtering associated with the column will be kept between column changes.
Each button changes the column definitions in a way that would be otherwise ignored
immutableColumnswas not set. The changes are as follows:
- Normal: Columns are set to the starting normal state. Use this to reset the example while observing what the other buttons do.
- Reverse Order: Columns are provided in reverse order.
- Widths: Columns are provided with different widths.
Visibility: Columns are provided with
colDef.hidden=true. The columns will still be in the grid and listed in the tool panel, however they will not be visible.
- Grouping: Rows will be grouped by Sport.
- No Resize or Sort: Clicking the columns will not sort and it will not be possible to resize the column via dragging its edge.
- Pinned: Columns will be pinned to the left and right.