Reputation: 87
I'm using ag-grid community via plain js. I want to get the column header text for each column on load which i'm currently doing via the onFirstDataRendered event.
I then loop through all columns and get the value of headerName from the colDefs for each column. This works but only if I manually set the headerName in the colDefs when creating my grid. AG grid will auto create a header name based on the field name if headerName is not manually added. So my current solution isn't actually getting the current column headers but instead the headers that were manually set.
How can i get the actual column headers that ag grid uses?
Here is my current flawed solution.
onFirstDataRendered: (event) =>{
event.api.getAllGridColumns().forEach(column => {
console.log(column);
console.log(column.colDef.headerName)// only works if I manually set headername
});
}
Upvotes: 0
Views: 1377
Reputation: 5688
You can use the function getDisplayNameForColumn
to get the displayed header name. Change your code to the following:
onFirstDataRendered: (event) => {
event.api.getAllGridColumns().forEach((column) => {
//console.log(column);
console.log(event.api.getDisplayNameForColumn(column));
// console.log(column.colDef); // only works if I manually set headername
});
Demo.
Upvotes: 1