Column Definitions

Each column in the grid is defined using a column definition. Columns are positioned in the grid according to the order the ColDefs are specified in the grid options.

The following example shows a simple grid with 3 columns defined:

var gridOptions = { // define 3 columns columnDefs: [ { headerName: 'Athlete', field: 'athlete' }, { headerName: 'Sport', field: 'sport' }, { headerName: 'Age', field: 'age' } ], // other grid options here... }

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:

var gridOptions = { columnDefs: [ // put the three columns into a group { headerName: 'Group A', children: [ { headerName: 'Athlete', field: 'athlete' }, { headerName: 'Sport', field: 'sport' }, { headerName: 'Age', field: 'age' } ] } ], // other grid options here... }

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.

Column Types are designed to work on Columns only, i.e. they won't be applied to Column Groups.

The following code snippet shows these three properties configures:

var gridOptions = { rowData: myRowData, // define columns columnDefs: [ // uses the default column properties { headerName: 'Col A', field: 'a'}, // overrides the default with a number filter { headerName: 'Col B', field: 'b', filter: 'agNumberColumnFilter' }, // overrides the default using a column type { headerName: 'Col C', field: 'c', type: 'nonEditableColumn' }, // overrides the default using a multiple column types { headerName: 'Col D', field: 'd', type: ['dateColumn', 'nonEditableColumn'] } ], // a default column definition with properties that get applied to every column defaultColDef: { // set every column width width: 100, // make every column editable editable: true, // make every column use 'text' filter by default filter: 'agTextColumnFilter' }, // if we had column groups, we could provide default group items here defaultColGroupDef: {} // define a column type (you can define as many as you like) columnTypes: { 'nonEditableColumn': { editable: false }, 'dateColumn': { filter: 'agDateColumnFilter', filterParams: { comparator: myDateComparator }, suppressMenu: true } } // other grid options here... }

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:

// Step 1: the grid starts with an empty merged definition {} // Step 2: default column properties are merged in { width: 100, editable: true, filter: 'agTextColumnFilter' } // Step 3: column type properties are merged in (using the 'type' property) { width: 100, editable: false, filter: 'agNumberColumnFilter' } // Step 4: finally column definition properties are merged in { headerName: 'Col C', field: 'c', width: 100, editable: false, filter: 'agNumberColumnFilter' }

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.

Because right alignment is used for numbers, we also provided an alias numericColumn that can be used to align the header and cell text to the right.
var gridOptions = { columnDefs: [ { headerName: 'Column A', field: 'a' }, { headerName: 'Column B', field: 'b', type: 'rightAligned' }, { headerName: 'Column C', field: 'c', type: 'numericColumn' } ] }

Column IDs

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 field is used. If both colId and field then colId gets preference. If neither colId or field then numeric is provided. Then finally the ID ensured to be unique by appending '_n' where 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 colId or field so 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:

[ { colId: 'athlete', aggFunc: 'sum', hide: false, rowGroupIndex: 0, width: 150, pinned: null }, { colId: 'age', aggFunc: null, hide: true, rowGroupIndex: null, width: 90, pinned: 'left' } ]

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 null.
  • hide: true if the column is hidden, otherwise false.
  • 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 'left' or 'right'
To suppress events raised when invoking columnApi.setColumnState(state), and also columnApi.resetColumnState(), use: gridOptions.suppressSetColumnStateEvents = true.

Column API Example

The example below shows using the grid's Column API.

This example also includes Column Groups which are covered in the next section, in order to demonstrate saving and restoring the expanded state.

Column Changes

It is possible to add and remove columns after the grid is created. This is done by either calling api.setColumnDefs() or setting the bound property columnDefs.

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.

If you are updating the columns (not replacing the entire set) then you must either provide column IDs or reuse the column definition object instances. Otherwise the grid will not know that the columns are in fact the same columns.

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.

Immutable Columns

By default when new columns are loaded into the grid, the following properties are not used:

  • Column Order
  • Aggregation Function (colDef.aggFunc)
  • Width (colDef.width)
  • Pivot (colDef.pivot or colDef.pivotIndex)
  • Row Group (colDef.rowGroup or colDef.rowGroupIndex)
  • Pinned (colDef.pinned)
This is done on purpose to avoid unexpected behaviour for the application user.

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 immutableColumns=true. 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:

  • Grid property immutableColumns is set to true.
  • 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 if immutableColumns was 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.