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:

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

ColumnState
void
Updates the Column State for a given Column
Updating Stateful Column Props at Runtime
Fork
  • In this demo a Custom Dashboard Toolbar contains 3 buttons that each update stateful AG Grid Column props using the updateAgGridColumnState function:
    • The Name column is widened using width
    • The Langague column is hidden using hide
    • The Github Stars column is sorted and pinned using sort and pin

Expand to see how the Column Definitions are updated

Try It Out
  • 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

ColDefWithId
void
Updates non-stateful properties in an AG Grid Column
Updating Other Column Props at Runtime
Fork
  • 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 updateAgGridColumnDefinition function is used:
    • The Name column is given a new headerName of 'Framework'
    • The License column has editable set to true
    • The Issue Change column is given a Column Type of 'Github' (because an existing Format Column has a Scope of that Column Type, the Issue Change Column is automatically styled)

Expand to see how the Column Definitions are updated

Try It Out
  • 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

ColDefWithId
void
Adds new AG Grid Columns
Adding Column Definitions at Runtime
Fork
  • In this example we provide a button that adds a Forks Count column using the addAgGridColumnDefinition function
  • We then use the updateCurrentLayout function 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

Try It Out
  • Click the button to add a Forks Count column (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: string
void
Removes existing AG Grid Columns
Deleting Column Definitions at Runtime
Fork
  • In this example we provide a button that deletes the License column using the removeAgGridColumnDefinition function.

Expand to see how the Column Definition is deleted

Try It Out
  • Click the button to remove the License column