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 & Removing ColDefs
AdapTable's GridApi contains 3 functions to safely add and remove AG Grid Column Definitions at run-time:
addAgGridColumnDefinition- adds one ColumnDefremoveAgGridColumnDefinition- removes one ColumnDefsetAgGridColumnDefinitions- adds a group of ColumnDefs
Important
- Always use these function when adding new Columns, or removing existing Columns, at runtime
- AdapTable will make sure to add or remove column in ColumnDefs correctly and ensure everything works
Adding ColDefs
The addAgGridColumnDefinition function enables a new AG Grid Column Definition to be added at run-time.
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
The removeAgGridColumnDefinition function enables safe run-time deletion of existing AG Grid ColDefs.
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
Setting ColDefs
The setAgGridColumnDefinitions function enables setting multiple AG Grid Column Definitions at run-time in a single step.
setAgGridColumnDefinitions
columnDefinitions: (ColDef | ColGroupDef)[]void- In this example the grid starts with many columns visible
- Clicking the button replaces all Column Definitions in a single step using the
setAgGridColumnDefinitionsfunction - The new set contains just 4 columns:
Framework,Language,GitHub StarsandLicense - The current Layout is also updated so it stays in sync with the new columns (see Updating Layouts for more details)
Expand to see how the Column Definitions are set
- Click the button to replace the Column Definitions with the compact set (and note how the Layout is updated)