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, which is a JavaScript key-value object consisting of Column Options. An array of these objects can be passed to the columnDefs
Grid Option and the grid will create matching columns.
const [columnDefs, setColumnDefs] = useState([
{ field: 'athlete' },
{ field: 'sport' },
{ field: 'age' }
]);
<AgGridReact columnDefs={columnDefs} />
Columns can be also be configured under Column Groups, which present the columns under shared headers. These can be configured by adding a level of nesting to the column definition.
const [columnDefs, setColumnDefs] = useState([
{ field: 'athlete' },
{
headerName: 'Stats',
children: [
{ field: 'sport' },
{ field: 'age' }
]
}
]);
<AgGridReact columnDefs={columnDefs} />
Referencing Columns
Columns can be updated via the colDefs
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
orfield
so the grid generates column IDs.
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 recommend to rely on IDs generated with this behaviour.
Column Objects
Every column displayed in the grid is represented by a Column Object 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.
The Column Reference displays a list of functions available on the Column Object.
It is also possible to listen for Column Events by attaching an Event Listener.
Updating Columns
Columns can be controlled by updating the column state, or updating the column definition.
Column State should be used when restoring a users grid, for example saving and restoring column widths.
Update Column Definitions 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.