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
defaultColDef
property that is aColDef
object. - Next, we bind to the
defaultColDef
input binding on the<ag-grid-angular>
component.
Exercise
- Open the exercise on Stackblitz.
- Define a new
defaultColDef
property in the component class. ThedefaultColDef
will be an object of typeColDef
. - Set the
sortable
property to true. - Bind the
defaultColDef
property to thedefaultColDef
input on the<ag-grid-angular>
component. - Override the
sortable
property 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;
}