简体   繁体   中英

Update columnDef's in agGrid without incrementing colIds

I have various requests where the columnDefs in my agGrid must be changed as there is no API method available for the respective property (egwhen changing the column filter and type to numeric).

In this case, I am currently building a new columnDef and setting it via...api.setColumnDefs();It seems that this is the proper way to update columnDefs. Directly accessing the current columnDefs property of the grid will not work (at least the Grid cannot be refreshed to accept the changes)

This is working pretty nicely except for the fact, that this will apparently re-create the columns from scratch, hence assign new (incremented) colId's to them. eg Column "first" had colId: 0 before the colDef update and colId: 1 after the update.

Now that I'm also facing requests to keep column states on changes, this behavior is giving me some challenges. As getColumnState() and setColumnState() will only work when the colIds are the same as before, I need to add own logic.

For this I am currently updating the colId in my saved states by matching the headerNames... Even though this is not so nice, I could live with it for now.

My current issue is, that I cannot preserve the column collapse/expand state this way, as this information is not stored in the colDef.

rememberGroupStateWhenNewData: true is also not working, due to the colIds being altered/incremented.

My question could probably be solved in two different ways

  1. The first and best way: A method to update the columnDefs without altering the colIds

  2. Otherwise: A method to preserve the groups collapse/expand state manually

Thank you all very much in advance

As @Pratik_Bhat mentioned, you can specify the colId in your colDef.

Also, I have found that when re-defining columns with colIds that are the same in the new colDefs as in the old colDefs, ag-grid will append an underscore and a number to the colId.

So, if you have a colDef with a colId of 'columnA', and you redefine the columns with another set of columns having a column with a colId of 'columnA', you will wind up with a 'columnA' and a 'columnA_1'.

You will see only the new column in the grid, but your new column will unexpectedly have a colId of something like 'columnA_1'. If you are depending on knowing the colId, this will cause your code to break.

Fortunately, there's an easy answer. Before assigning the new colDefs, assign an empty array. This appears to 'clear out' the old colDefs. Immediately after that, assign your real colDefs. You will then get the colIds that you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM