0
votes

In ag-grid I have a table with a structure like this:

            |   Temperature      |  ....
 -----------|------|------|------|---------
 Date       | min  | avg  | max  |  ....
 -----------|------|------|------|---------
 2017-03-01 | 19.5 | 20.2 | 22.0 |  ....
 2017-03-02 | 18.8 | 20.4 | 21.6 |  ....

I want to be able to hide the entire Temperature column group and I do it like this:

  • get column group by it's name with columnApi.getColumnGroup(groupId)
  • get column children with getChildren()
  • loop through all elements and hide/show depending on Column visibility state

The hiding part works ok, but when I want to show the columns again, the getColumnGroup method returns a null object, and I cannot set the columns to be visible again. Any ideas?

The entire code (part of an Angular2 component) looks like this:

toggleColumn(groupId: string) {
    let groupColumn = this.dataGridOptions.columnApi.getColumnGroup(groupId);
    let children = groupColumn.getChildren();

    for (let idx = 0; idx < children.length; idx++) {
        let colId: string = children[idx].getUniqueId();
        let colState = this.dataGridOptions.columnApi.getColumn(colId);
        let colVisibility = colState.isVisible();

        this.dataGridOptions.columnApi.setColumnVisible(colId, !colVisibility);
    }
}
1

1 Answers

1
votes

you can't directly (explanation below). what you can do is loop over ALL columns and get the parent and check that it matches the groupId.

take a look at following link : https://github.com/ag-grid/ag-grid/issues/696

the columns always exist, exactly one column for every column def. the column then has a 'visible' attribute.

the groups are transient and only exists if they are needed (there is also the concept of 'OriginalColumnGroup' to keep track of what columns are in each group, but you don't have access to that, it's internal)

[...]

https://www.ag-grid.com/angular-grid-master-slave/index.php

so, in summary, column groups only exists if the group is showing, and there can be multiple groups for the same group if the columns are split. so, that's why the column groups don't return if they are not visible!