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.

Pratik Bhat
  • 7,166
  • 2
  • 35
  • 57
Daniel C.
  • 569
  • 2
  • 7
  • 19

2 Answers2

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.

Alexander Zbinden
  • 2,315
  • 1
  • 17
  • 21
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.

un.spike
  • 4,857
  • 2
  • 18
  • 38
  • 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. – Daniel C. Dec 04 '18 at 11:49
  • 1
    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! – Daniel C. Dec 04 '18 at 14:05
  • 1
    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!? – Daniel C. Dec 04 '18 at 18:10
  • 1
    looking for more infos here: [setColumnDefs does not repaint the grid](https://github.com/ag-grid/ag-grid/issues/2771) – Daniel C. Dec 04 '18 at 18:40