1

I have an ag-grid in angular 6 and have columns and data dynamically attached to it. Now i want to align header for (both parent and children in case of multi header) dynamically. Like first column header should be left aligned and others will be center aligned.

I tried to set cellStyle and some other options i found, but none of them worked

I tried to implement it like :

  let headerItem = {
    headerName: name,
    children: [
      {
        headerName: name,
        field: 'col' + i + '.value',
        hide: isHidden,
        pinned: pinned,
        cellStyle: function (params) {
          try {
            return {text-align : 'center'};
          } catch (e) {
            return null;
          }
        }
      }
    ],
    cellStyle: function (params) {
          try {
            return {text-align : 'center'};
          } catch (e) {
            return null;
          }
        }
  }

Please guide how can i get this working...

1
  • cellStyle is only applied to cells not to headers. What do you mean by "dynamically", do need to change it during runtime? Commented Apr 1, 2019 at 6:15

1 Answer 1

2

Assuming you are using the latest (if not one of the latest) versions of Ag-Grid, we have to create custom cell header components if we want to make any customisations to the header cells.

I have used on of the existing Ag-grid's sample, and created a demo for you. (refer to app/custom-header.component.ts for the custom header template)

What I have done is that I have created a separate component for it, and set the template for the header as the following to align it vertically and horizontally (I am using CSS flexbox properties for that)

<div style="display:flex;justify-content:center;align-items:center;">
  {{params.displayName}}
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

It worked like a charm .. thanks a lot. I have extended your solution to work for group header as well. Here is it : stackblitz.com/edit/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.