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 & Removing ColDefs

AdapTable's GridApi contains 3 functions to safely add and remove AG Grid Column Definitions at run-time:

  • addAgGridColumnDefinition - adds one ColumnDef
  • removeAgGridColumnDefinition - removes one ColumnDef
  • setAgGridColumnDefinitions - 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

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

The removeAgGridColumnDefinition function enables safe run-time deletion of existing AG Grid ColDefs.

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

Setting ColDefs

The setAgGridColumnDefinitions function enables setting multiple AG Grid Column Definitions at run-time in a single step.

setAgGridColumnDefinitions

columnDefinitions: (ColDef | ColGroupDef)[]
void
Set multiple AG Grid Columns
Setting Column Definitions at Runtime
Fork
  • In this example the grid starts with many columns visible
  • Clicking the button replaces all Column Definitions in a single step using the setAgGridColumnDefinitions function
  • The new set contains just 4 columns: Framework, Language, GitHub Stars and License
  • 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

Try It Out
  • Click the button to replace the Column Definitions with the compact set (and note how the Layout is updated)