Each column in the grid is defined using a Column Definition (ColDef
). Columns are positioned in the grid according to the order the Column Definitions are specified in the Grid Options.
<ag-grid-vue
:columnDefs="columnDefs"
/* other grid options ... */>
</ag-grid-vue>
this.columnDefs = [
{ field: 'athlete' },
{ field: 'sport' },
{ field: 'age' }
];
See Column Options for all available properties.
Column Defaults
Use defaultColDef
to set properties across ALL Columns.
<ag-grid-vue
:defaultColDef="defaultColDef"
/* other grid options ... */>
</ag-grid-vue>
this.defaultColDef = {
width: 150,
cellStyle: { fontWeight: 'bold' },
};
Column Types
Use columnTypes
to define a set of Column properties to be applied together. The properties in a column type are applied to a Column by setting its type
property.
<ag-grid-vue
:columnTypes="columnTypes"
:columnDefs="columnDefs"
/* other grid options ... */>
</ag-grid-vue>
// Define column types
this.columnTypes = {
currency: {
width: 150,
valueFormatter: CurrencyFormatter
},
shaded: {
cellClass: 'shaded-class'
}
};
this.columnDefs = [
{ field: 'productName'},
// uses properties from currency type
{ field: 'boughtPrice', type: 'currency'},
// uses properties from currency AND shaded types
{ field: 'soldPrice', type: ['currency', 'shaded'] },
];
Column Types work on Columns only and not Column Groups.
The below example shows Column Types.
Provided Column Types
The grid provides the Column Types rightAligned
and numericColumn
. Both of these types right align the header and cell contents by applying CSS classes ag-right-aligned-header
to Column Headers and ag-right-aligned-cell
to Cells.
<ag-grid-vue
:columnDefs="columnDefs"
/* other grid options ... */>
</ag-grid-vue>
this.columnDefs = [
{ headerName: 'Column A', field: 'a' },
{ headerName: 'Column B', field: 'b', type: 'rightAligned' },
{ headerName: 'Column C', field: 'c', type: 'numericColumn' },
];
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.
Column Definitions should be updated 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.
Using Column State
The Grid Api function applyColumnState
can be used to update Column State.
// Sort Athlete column ascending
this.gridApi.applyColumnState({
state: [
{
colId: 'athlete',
sort: 'asc'
}
]
});
In the example below, use the 'Sort Athlete' button to apply a column state.
Updating Column Definitions
To update an attribute by Updating Column Definitions, pass a new array of Column Definitions to the grid options.
// Define new column definitions
const updatedHeaderColumnDefs = [
{ field: 'athlete', headerName: 'C1' },
{ field: 'age', headerName: 'C2' },
{ field: 'country', headerName: 'C3' },
{ field: 'sport', headerName: 'C4' },
]
// Supply new column definitions to the grid
gridApi.setGridOption('columnDefs', updatedHeaderColumnDefs);
In the example below, use the 'Update Header Names' button to update the column definitions.