Set new column definition by setColumnDefs doesn't work anymore
Asked Answered
W

2

10

I'm trying to set new column definitions by calling setColumnDefs using the grid API. This doesn't work as expected. The names of the column headers will not be updated anymore!

See this Plunkr: Version 19.1.x

Version 19.0.0 is latest working version.

See this Plunkr: Version 19.0.0

For me it seems to be a bug!?

In my project I'm using Angular 5 and I notice the same behaviour.

Wiper answered 3/12, 2018 at 21:26 Comment(0)
V
18

I was able to reproduce your behaviour. The following (dirty) workaround works:

  gridOptions.api.setColumnDefs([]);
  gridOptions.api.setColumnDefs(newColDefs);

Setting the columnDefs to an empty array and then passing the newColDefs seems to achieve what you are looking for.

Voussoir answered 13/12, 2018 at 13:59 Comment(3)
Probably saved me a couple hours of debugging. Thanks!Pauper
This works, but for server-side row models it triggers a search request twice, which is less than ideal.Benavidez
This also clears the applied filter model, so will need to reapply the filter.Socha
C
0

I suppose it up to the new way of change-detection on the latest version.

If you will update your code like that:

function updateColDef()
{ 
    let data = [];
    columnDefs.forEach(function(colDef) { 
        colDef.headerName = colDef.headerName + ' X ';
        data.push(colDef);
  })

  data.push( {
      headerName: 'New Column',
  });

  gridOptions.api.setColumnDefs(data);
}

It will work as expected.

Update:

When new columns are set, the grid will compare with current columns and work out which columns are old (to be removed), new (new columns created) or kept (columns that remain will keep their state including position, filter and sort).

Comparison of column definitions is done on 1) object reference comparison and 2) column ID eg colDef.colId. If either the object reference matches, or the column ID matches, then the grid treats the columns as the same column.

In the first case, it was an object comparison, on the second sample (after update) its colId case.

changes came from 19.1 version release

AG-1591 Allow Delta Changes to Column Definitions.

Celebrate answered 4/12, 2018 at 10:46 Comment(4)
In my real scenario I don't want to change only the header names, i want to change the whole column definition. The only thing that can be the same is the ColId property.Wiper
Your answer is not an answer, its only a workaround for a possible bug/breaking change. If the new behavior is by design, then this is a breaking change in a minor version update. BTW: I updated my plunkr a little bit!Wiper
Many thanks for the update. Then for me the implementation of the feature request AG-1591 introduce a big breaking change to the public Grid API. And I think many customers will notice that. Why is this not included as a breaking change in the release notes!?Wiper
looking for more infos here: setColumnDefs does not repaint the gridWiper

© 2022 - 2024 — McMap. All rights reserved.