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:
@Component({
selector: 'app-grid',
standalone: true,
imports: [AgGridModule],
template: `
<ag-grid-angular
class="ag-theme-material"
[columnDefs]="columnDefs"
[rowData]="rowData"
[defaultColDef]="defaultColDef"
></ag-grid-angular>
`
})
export class GridComponent {
// code ommitted for brevity
defaultColDef = {
sortable: true
} as ColDef;
}
Let's quickly review:
- First, we define a new
defaultColDefproperty that is aColDefobject. - Next, we bind to the
defaultColDefinput binding on the<ag-grid-angular>component.
Exercise
- Open the exercise on Stackblitz.
- Define a new
defaultColDefproperty in the component class. ThedefaultColDefwill be an object of typeColDef. - Set the
sortableproperty to true. - Bind the
defaultColDefproperty to thedefaultColDefinput on the<ag-grid-angular>component. - Override the
sortableproperty to false for one of the columns.
Solution
@Component({
standalone: true,
imports: [AgGridModule, CommonModule],
template: `
<ag-grid-angular
class="ag-theme-material"
[columnDefs]="columnDefs"
[rowData]="rowData"
[defaultColDef]="defaultColDef"
></ag-grid-angular>
`
})
export class GridComponent {
columnDefs = [
{ headerName: 'Name', field: 'name' },
{ headerName: 'Color', field: 'color', sortable: false },
] as ColDef<RowData>[];
defaultColDef = {
sortable: true
} as ColDef;
rowData: RowData[] = products;
}