Managing AG Grid Column Defs at RunTime
Summary
- AdapTable helps developers manage AG Grid Column Definitions easily and safely updated at runtime
- Two convenience functions reduce the update's complexity while ensuring AdapTable's Layouts work smoothly
- Other functions enable Column Definitions to be safely addded and removed at runtime
AG Grid's Column Definitions are immutable.
Nevertheless, AG Grid still makes it possible, at runtime, to add, update or delete ColDefs.
AdapTable supports this functionality via bespoke "wrapper" functions to ensure everything hangs together.
These functions ensure that AG Grid columns are added, updated or deleted correctly, while also ensuring AdapTable's Layouts and other features to run smoothly.
Caution
- We strongly recommend you use AdapTable's API functions (listed below) when managing AG Grid columns
- AdapTable will invoke the related AG Grid function, but in a way that ensures everything hangs together properly
Updating ColDefs
AG Grid distinguishes betweeen 2 types of Column properties - stateful and non-stateful.
It provides 2 complementary sets of GridAPI functions to update column props based on this distinction:
- For the reduced set of "stateful" Column properties the AG Grid Column State API is recommended
- For all other Column properties AG Grid's Grid API should be used
AdapTable fully supports AG Grid Column immutability, and also how columns can be updated at run-time.
AdapTable's Grid API contains 2 sets of complementary functions to mirror those provided by AG Grid.
Note
- AdapTable provides 2 different sets of methods for stateful and non-stateful Column properties
- However the shape of the object that is passsed into each function is identical
Stateful Properties
As noted above, AG Grid defines a subset of column properties as Column State.
These are properties which can be updated without needing to provide a full new set of Column definitions.
AdapTable provides the updateAgGridColumnState function in GridApi to update the Column State for a Column in a safe way.
Hint
A parallel updateAgGridColumnStates function can be used to update the Column State for multiple columns
updateAgGridColumnState
ColumnStatevoid- In this demo a Custom Dashboard Toolbar contains 3 buttons that each update stateful AG Grid Column props using the
updateAgGridColumnStatefunction:- The
Namecolumn is widened usingwidth - The
Langaguecolumn is hidden usinghide - The
Github Starscolumn is sorted and pinned usingsortandpin
- The
Expand to see how the Column Definitions are updated
- Click each button in turn and see how they update
Non-Stateful Properties
Adaptable also fully supports updating Column properties which are not stateful.
This is done using the updateAgGridColumnDefinition function in GridApi.
updateAgGridColumnDefinition
ColDefWithIdvoid- In this example the Custom Dashboard Toolbar again contains 3 buttons that each updates a Column Definition property
- But as these are non-stateful AG Grid Column props, the
updateAgGridColumnDefinitionfunction is used:- The
Namecolumn is given a newheaderNameof 'Framework' - The
Licensecolumn haseditableset to true - The
Issue Changecolumn is given a Column Type of 'Github' (because an existing Format Column has a Scope of that Column Type, theIssue ChangeColumn is automatically styled)
- The
Expand to see how the Column Definitions are updated
- Click each button in turn and see how they update
Adding ColDefs
AdapTable's GridApi contains an addAgGridColumnDefinition function to enable new AG Grid Column Definitions to be safely added at run-time.
Important
- Always use this function when adding new Columns at runtime
- AdapTable will make sure to add the column to ColumnDefs correctly
addAgGridColumnDefinition
ColDefWithIdvoid- In this example we provide a button that adds a
Forks Countcolumn using theaddAgGridColumnDefinitionfunction - We then use the
updateCurrentLayoutfunction in Layout API to update the Layout to include the newly created column (see Updating Layouts for more details)
Expand to see how the Column Definition is added
- Click the button to add a
Forks Countcolumn (and note how it is added to the Layout)
Removing ColDefs
AdapTable's GridApi additionally contains a removeAgGridColumnDefinition function to enable existing AG Grid Column Definitions to be safely deleted at run-time.
Important
- Always use this function when deleting existing Columns at runtime
- AdapTable will make sure to from the column from ColumnDefs correctly
removeAgGridColumnDefinition
columnId: stringvoid- In this example we provide a button that deletes the
Licensecolumn using theremoveAgGridColumnDefinitionfunction.
Expand to see how the Column Definition is deleted
- Click the button to remove the
Licensecolumn