Default Column Definition
Default Column Definition
To avoid unnecessary repetition, we can set a default column definition.
- Every column inherits the default column definition
- Each column definition can override default values
Let's look at an example:
export default function Grid() {
const defaultColDef: ColDef<RowData> = {
sortable: true,
};
return (
<div className="ag-theme-quartz-auto-dark">
<AgGridReact
columnDefs={columnDefs}
rowData={rowData}
defaultColDef={defaultColDef}
></AgGridReact>
</div>
);
}
Let's quickly review:
- First, we define a new
defaultColDef
const that is aColDef
object. - Next, we bind to the
defaultColDef
prop on the<AgGridReact>
component.
Exercise
- Open the exercise on Stackblitz.
- Define a new
defaultColDef
const in the component function. ThedefaultColDef
will be an object of typeColDef<RowData>
. - Set the
sortable
property to true. - Bind the
defaultColDef
const to thedefaultColDef
prop on the<AgGridReact>
component. - Override the
sortable
property to false for one of the columns.
Solution
export default function Grid() {
const [columnDefs] = useState<ColDef<RowData>[]>([
{ headerName: 'Name', field: 'name' },
{ headerName: 'Color', field: 'color', sortable: false },
]);
const [rowData] = useState<RowData[]>(data.products);
const defaultColDef: ColDef<RowData> = {
sortable: true,
};
return (
<div className="ag-theme-quartz-auto-dark">
<AgGridReact
columnDefs={columnDefs}
rowData={rowData}
defaultColDef={defaultColDef}
></AgGridReact>
</div>
);
}