Expand All

  Getting Started

  Reference

  Features

  Row Models

  Themes

  Components

  Examples

Misc

Github stars make projects look great. Please help, donate a star, it's free.
Read about ag-Grid's Partnership with webpack.
Get informed on releases and other ag-Grid news only - never spam.
Follow on Twitter

Column Definitions

Each column in the grid is defined using a column definition. Columns are positioned in the grid according to the order the ColDef's 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.

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

Numeric Columns

The grid provides a handy shortcut for formatting numeric columns. Setting the column definition type to numericColumn aligns the column header and contents to the right, which makes the scanning of the data easier for the user.

var gridOptions = { columnDefs: [ { headerName: "Column A", field: "a" }, { headerName: "Column B", field: "b", type: "numericColumn" } ] }

Updating Column Definitions

After the grid has been initialised it may be necessary to update the column definition. It is important to understand that when a column is created it is assigned a copy of the column definition defined in the GridOptions. For this reason it is necessary to obtain the column definition directly from the column.

The following example shows how to update a column header name after the grid has been initialised. As we want to update the header name immediately we explicitly invoke refreshHeader() via the Grid API.

// get a reference to the column var col = gridOptions.columnApi.getColumn("colId"); // obtain the column definition from the column var colDef = col.getColDef(); // update the header name colDef.headerName = "New Header"; // the column is now updated. to reflect the header change, get the grid refresh the header gridOptions.api.refreshHeader();

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 colDef's 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. See column definitions for explanation of column ID
  • 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'

Column API Example

This section illustrates how to store and restore column state using the Column API.

  • hiding / showing columns as well as saving / restoring the entire state
  • registering for column events, the result of which are printed to the console.
This example also includes Column Groups which are covered in the next section, in order to demonstrate saving and restoring the expanded state.