ag-grid modify Row Group's Header name
Asked Answered
S

4

6

I'm using ag-grid to display a table which has a Row Grouping. The column that I group by is invisible since its value holds no meaning.

So for example, I will have the following (fully collapsed) table:

---------------------------------
|   H1       H2        H3        |
----------------------------------
| > groupId1 (1)                 |
----------------------------------
| > groupId2 (5)                 |
----------------------------------

As you see the grouping is done by user-unfriendly ID which is not reflected at all in the Column Definitions. I would like to change groupId1 / groupId2 to a user-friendly text that is assigned dynamically according to the content of the group rows.

I am using React.

I am using ag-Grids examples as a starting point, the following example embodies the problem I'm facing: (eg: https://plnkr.co/edit/VM59gScPD55PhX4y4JBj?p=preview)

  1. It has row grouping
  2. The grouping is done by a column that is invisible (country)
  3. I would like to change the country name dynamically into a different value that's derived from the values of the inner row.

Thanks for your time. :)

Strangeness answered 3/2, 2019 at 14:36 Comment(0)
S
2

A colleague helped me at the end, his solution was:

valueFormatter: ({ value, data }) => 'something ' + value

inside the colDef of my group, and the value to be displayed as the header is sent inside 'data'.

He said that cellRenderer is an overkill for a simple task like this.

Strangeness answered 4/2, 2019 at 12:59 Comment(0)
M
7

You can also rename the rowGroup header of ag-grid by using this simple prop autoGroupColumnDef

<AgGridReact 
  columnDefs={columnDefs} 
  rowData={rowData}
  autoGroupColumnDef={{
    headerName: 'Name'
  }}
/>
Muenster answered 19/11, 2019 at 6:55 Comment(0)
S
2

A colleague helped me at the end, his solution was:

valueFormatter: ({ value, data }) => 'something ' + value

inside the colDef of my group, and the value to be displayed as the header is sent inside 'data'.

He said that cellRenderer is an overkill for a simple task like this.

Strangeness answered 4/2, 2019 at 12:59 Comment(0)
C
1

You can pass additional params about group cell renderer and customize it like this -

colDef = {
    // set the cell renderer to 'group'
    cellRenderer:'agGroupCellRenderer',
    // provide extra params to the cellRenderer
    cellRendererParams: {
        innerRenderer: myInnerRenderer, // provide an inner renderer
    }
    ...
};

You should be then able to display any text based on custom logic using the myInnerRenderer implementation.

Here is an example from ag-grid docs. Check the Group Renderer C implementation

Caesarean answered 3/2, 2019 at 23:6 Comment(0)
H
0
cellStyle: (params) => {
        if (params.data?.footer) {
          return { textAlign: 'left', fontWeight: 'bold' }; // Bold for footer row
        }
        return null;
      },
Heliostat answered 22/10 at 2:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.