---
title: "Configuration"
framework: javascript
version: "36.1.0"
---

# Configuration

Columns are configured using Column Definitions, manipulated with Column State and referenced using IDs or the Column Object.

## Defining Columns

Each column in the grid is defined using a [Column Definition](https://www.ag-grid.com/javascript-data-grid/column-definitions/), which is a JavaScript key-value object consisting of [Column Options](https://www.ag-grid.com/javascript-data-grid/column-properties/). An array of these objects can be passed to the `columnDefs` [Grid Option](https://www.ag-grid.com/javascript-data-grid/grid-options/#reference-columns-columnDefs) and the grid will create matching columns.

```js
const gridOptions = {
    columnDefs: [
        { field: 'athlete' },
        { field: 'sport' },
        { field: 'age' }
    ],

    // other grid options ...
}
```

Columns can also be configured under [Column Groups](https://www.ag-grid.com/javascript-data-grid/column-groups/), which present the columns under shared headers. These can be configured by adding a level of nesting to the column definition.

```js
const gridOptions = {
    columnDefs: [
        { field: 'athlete' },
        {
            headerName: 'Stats',
            children: [
                { field: 'sport' },
                { field: 'age' }
            ]
        }
    ],

    // other grid options ...
}
```

## Referencing Columns

Columns can be updated via the `columnDefs` grid option when a sufficient ID has been provided, or manipulated via the API with a Column Object.

### Column IDs

Every column in the grid will be given a unique ID to identify it. The ID can be provided explicitly via the `colId`. If the `colId` is omitted, the grid will try to use the `field` property. If neither of these are provided, the grid will generate a numeric column ID.

It is recommended to provide an explicit `colId` for any column that will be referenced elsewhere in the application.

In the example below, the column IDs are logged to the dev console. Note the following:

- Col 1 uses the `field`.
- Col 2 and 3 use the `colId`.
- Col 4 and Col 5 have neither `colId` or `field` so the grid generates column IDs.

#### Column IDs

```ts
import {
  ClientSideRowModelModule,
  ColumnApiModule,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([ColumnApiModule, ClientSideRowModelModule]);

let gridApi: GridApi;

const gridOptions: GridOptions = {
  columnDefs: [
    // colId will be 'height',
    { headerName: "Col 1", field: "height" },

    // colId will be 'firstWidth',
    { headerName: "Col 2", colId: "firstWidth", field: "width" },
    // colId will be 'secondWidth'
    { headerName: "Col 3", colId: "secondWidth", field: "width" },

    // no colId, no field, so grid generated ID
    { headerName: "Col 4", valueGetter: "data.width" },
    { headerName: "Col 5", valueGetter: "data.width" },
  ],
  rowData: createRowData(),
  onGridReady: (params: GridReadyEvent) => {
    const cols = params.api.getColumns()!;
    cols.forEach((col) => {
      const colDef = col.getColDef();
      console.log(
        colDef.headerName + ", Column ID = " + col.getId(),
        JSON.stringify(colDef),
      );
    });
  },
};

function createRowData() {
  const data = [];
  for (let i = 0; i < 20; i++) {
    data.push({
      height: Math.floor(window.agRandom() * 100),
      width: Math.floor(window.agRandom() * 100),
      depth: Math.floor(window.agRandom() * 100),
    });
  }
  return data;
}

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
```

[Live example: Column IDs](https://www.ag-grid.com/examples/configuration/column-ids/typescript)

> **Warning**
>
> Column Ids should be unique across the grid. Where the provided `colId` or `field` are not unique, the grid will append `_n` where necessary (`n` being the first positive number that allows uniqueness). It is not recommended to rely on IDs generated with this behaviour.

### Column Objects

Every column displayed in the grid is represented by a [Column Object](https://www.ag-grid.com/javascript-data-grid/column-interface/#column) which has attributes, methods and events for interacting with the specific column e.g. `column.isVisible()`.

Columns can be accessed via Grid API methods, and provided as parameters from some [Grid Events](https://www.ag-grid.com/javascript-data-grid/grid-events/#reference-columns).

The [Column Reference](https://www.ag-grid.com/javascript-data-grid/column-object/) displays a list of functions available on the Column Object.

It is also possible to listen for [Column Events](https://www.ag-grid.com/javascript-data-grid/column-events/) by attaching an [Event Listener](https://www.ag-grid.com/javascript-data-grid/column-object/#reference-events-addEventListener).

Clicking on the `Log All Columns` and `Log All Column IDs` buttons will log the data to the developer console.

#### Column Object

```ts
import {
  ClientSideRowModelModule,
  ColDef,
  ColumnApiModule,
  GridApi,
  GridOptions,
  ModuleRegistry,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([ColumnApiModule, ClientSideRowModelModule]);

const columnDefs: ColDef[] = [
  { field: "make" },
  { field: "model" },
  { field: "price" },
];

// specify the data
const rowData = [
  { make: "Toyota", model: "Celica", price: 35000 },
  { make: "Ford", model: "Mondeo", price: 32000 },
  { make: "Porsche", model: "Boxster", price: 72000 },
  { make: "BMW", model: "M50", price: 60000 },
  { make: "Aston Martin", model: "DBX", price: 190000 },
];

function getAllColumns() {
  console.log(gridApi!.getColumns());
}

function getAllColumnIds() {
  const columns = gridApi!.getColumns();
  if (columns) {
    console.log(columns.map((col) => col.getColId()));
  }
}
let gridApi: GridApi;

// let the grid know which columns and what data to use
const gridOptions: GridOptions = {
  columnDefs: columnDefs,
  defaultColDef: {
    flex: 1,
  },
  rowData: rowData,
};

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);

if (typeof window !== "undefined") {
  // Attach external event handlers to window so they can be called from index.html
  (<any>window).getAllColumns = getAllColumns;
  (<any>window).getAllColumnIds = getAllColumnIds;
}
```

[Live example: Column Object](https://www.ag-grid.com/examples/configuration/column-object/typescript)

## Updating Columns

Columns can be controlled by updating the column state, or updating the column definition.

[Column State](https://www.ag-grid.com/javascript-data-grid/column-state/) should be used when restoring a users grid, for example saving and restoring column widths.

[Update Column Definitions](https://www.ag-grid.com/javascript-data-grid/column-updating-definitions/#changing-column-definition) to modify properties that the user cannot control, and as such are not supported by Column State. Whilst column definitions can be used to change stateful properties, this can cause additional side effects.
