Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap column header in ag-grid using angular

I have some columns which has four words i.e Pre Trading Follow Up, Post Trading Follow Up and some of them having three words. I tried the below css to wrap the text to multiple lines.

::ng-deep .ag-theme-material .ag-header-cell-label .ag-header-cell-text{
  white-space: normal;
  overflow-wrap: break-word;
}

HTML

<ag-grid-angular class="ag-theme-material" [rowData]="rowData" [columnDefs]="columnDefs" [overlayLoadingTemplate]="overlayLoadingTemplate" [domLayout]="domLayout" [enableSorting]="true" (gridReady)="onGridReady($event)" (gridOptions)="gridOptions" >
  </ag-grid-angular>

but the column header remains the same. I wanted to wrap the column header text to multiple lines. Is there anyway to do this?

Note: I can able to wrap the content using cellStyle: {'white-space': 'normal'}

{headerName: 'headername', field: 'headerfield', autoHeight:true, width: 100, cellStyle: {'white-space': 'normal'}},

But I wanted to wrap the header.

like image 668
UI_Dev Avatar asked Nov 26 '18 08:11

UI_Dev


2 Answers

Please review the following stackblitz example.

https://stackblitz.com/edit/angular-ag-grid-angular-xmbm3p?embed=1&file=styles.css

In the global style sheet I applied the following... you could use ::ng-deep in your component css, this is the first stackblitz I could find with ag-grid to fork and is not mine so there was no component css to use.

.ag-header-cell-label .ag-header-cell-text {
  white-space: normal !important;
}

The next piece is to use property headerHeight

this.gridOptions = <GridOptions>{
          headerHeight:75,

This part unfortunately is unavoidable... it also doesn't allow for you to make the header height dynamic based on the word wrap requirements.

  • The reason why is that the content area is defined with top style dynamically when the view is rendered; adjusting the header height via ::ng-deep will not dynamically shift the top of the content area down as it is calculated by the headerHeight property... if undefined the default is 25px so the top for content area is also 25px.
  • Not to mention that the z-index of the content area causes it to overlap the header when you change the height with ::ng-deep.. so you don't know if ::ng-deep truly worked... visually that is... as the header extends under the content area.

Sorry to say but this will be as close as you can get... adjusting all elements, shifting down the top etc based on a dynamic header height via DOM manipulation I fear will just get too ugly... and if you need the header height dynamic to the point this is a show stopper... it may be best to explore other options as a replacement to ag-grid.

https://www.ag-grid.com/javascript-grid-column-header/#headerHeight

like image 103
Marshal Avatar answered Nov 14 '22 10:11

Marshal


To achieve expected result , use below option of using line break tag - br in column definitions for that specific column headerName

{headerName: 'Pre<br>Trading<br> Follow<br> Up', field: 'headerfield', autoHeight:true, width: 100, cellStyle: {'white-space': 'normal'}}
like image 1
Naga Sai A Avatar answered Nov 14 '22 09:11

Naga Sai A