Configuring Column Header

View as Markdown

Summary

  • AdapTable users are able to change the Header / Caption of a Column in AG Grid in 2 ways:
    • Changes configured in a particular Layout
    • Changes set through the columnHeader function in Column Options

By default, AdapTable will show as the header (or caption) of the Column (ie. the text that appears in the Header Bar) the default value provided in AG Grid's ColumnDefs.

However this can be changed in AdapTable in 2 ways:

When both are used, the function is always called. The Layout caption is passed into the function so you can honour a user override, keep the AG Grid header, or apply your own value.

Hint

  • This page is the source for the text that appears in the Grid header
  • To change how the Column is labelled in the AdapTable UI (Settings Panel, Expression Editor, and so on), use columnFriendlyName — see Adaptable Column

Per Layout

A given Layout can store a caption for a Column using the ColumnHeaders property.

Note

  • This enables the same Column to have different Headers / Captions in 2 Layouts
  • It also allows the Column's Header to be set at run-time via the AdapTable UI

A custom Column Header can be set in a Layout (using the ColumnHeaders property) in many ways:

  • In Layout Initial State (i.e. by developers at design-time)
  • Using the Change Caption menu option in each Column Menu
  • In the Columns section of the Layout Wizard (via the dropdown which opens an expanded section)
  • Programmatically using the setColumnCaption function in Layout API

Hint

  • AG Grid makes it easy to wrap Column Headers - useful when the header text is wider than the cell text
  • The wrapHeaderText & autoHeaderHeight props in Column Defs, when set to true, creates the necessary effect
Custom Column Headers Layout
Fork
  • This demo shows how to change the Column Header on a per-Layout basis
  • In the Initial State for the Standard Layout we change 'Name' to JavaScript Framework and 'Gihub Stars' to Fans
  • Note: we set wrapHeaderText and autoHeaderHeight to true in Column Def's to create a better visual effect
  • We provide a Dashboard button to allow the Header for "Updated" to be set via the Layout API
Try It Out
  • Switch between Layouts and note that the Grouped Layout contains the original Column Headers
  • Change a Column Header using the Change Header Menu Item in the Column Menu
  • Change another Column's Header by using the Layout Editor's Wizard
Loading demo…

Using a Function

The columnHeader function in Column Options provides a more configurable way to change Column Headers.

The function is always invoked (if provided)

Note

This includes when the current Layout already has a caption for that Column

That lets you choose, per column, between 3 values:

(1) colDefHeaderName: the original AG Grid Column Definition header (2) a value you compute in the function (3) layoutSpecificHeaderName: a caption the user (or Initial State) set on the Layout

The typical pattern is: use (2) unless the user has changed the header themselves, in which case use (3).

columnHeader: (context) => {
  // 3: the user overrode the header in the Layout — honour that
  if (context.layoutSpecificHeaderName) {
    return context.layoutSpecificHeaderName;
  }
  // 2: developer override for the Name column
  if (context.columnType === 'tableColumn' && context.columnId === 'name') {
    return 'JS Framework';
  }
  // 1: fall back to the AG Grid header
  return context.colDefHeaderName;
}

Hint

  • defaultHeaderName is the resolved value that would be displayed if no function were provided
  • This is the Layout caption if one exists, otherwise the ColDef header

The function's context supports many different categories of column, including dynamically created ones.

columnHeader

(context: ColumnHeaderContext) => string
Provide Custom Column Headers
Custom Column Headers Function
Fork
  • This demo shows how to change the Column Header programmatically. We change the Header for various types of columns (visible via different Layouts):
    • For tableColumn we change 'Name' to JS Framework (see Table Layout)
    • For autoGroupColumn we change the header to "My Row Group" (see Row Grouped Layout)
    • For tableColumnGroup we change the header to "My Column Group" (see Column Group Layout)
    • For pivotResultColumn we add prefix of "Agg" to header (see Pivot Layout)
    • For pivotColumnGroup we add prefix of "PC" to header (also see Pivot Layout)
Try It Out
  • Switch between Layouts and see the different Headers
  • Change the JavaScript Framework Column Header in the Layout Editor's Wizard — the function honours that user override via layoutSpecificHeaderName
Loading demo…

AG Grid Editable Header Names

AG Grid provides its own way to rename headers: setting headerNameEditable on a Column Definition (with ColumnHeaderEditModule) adds an Edit Column Name item to AG Grid's menus.

AdapTable does not integrate this feature, because AdapTable's own Column Header support already covers it and goes considerably further.

AdapTable Column Headers:

  • are set per Layout, so the same Column can have a different caption in each Layout
  • are saved in Adaptable State, together with the rest of the Layout
  • can be shared with other users, as part of the Layout, via Team Sharing
  • can be set at design-time in Initial State, at run-time in the Column Menu or the Layout Wizard, or programmatically via the Layout API
  • can be computed by the columnHeader function, which covers Table Columns and Column Groups as well as Row Group, Pivot and Total Columns

A name edited in AG Grid, by contrast:

  • is stored in AG Grid's Column State and Grid State rather than in the Layout, so it is not part of Adaptable State and cannot be shared via Team Sharing
  • applies to every Layout, rather than to the Layout where it was set
  • takes priority over the AdapTable header mechanism, hiding the Layout's ColumnHeaders caption and bypassing the columnHeader function for that Column

Enabling AG Grid's feature would therefore replace a more capable mechanism with a more limited one, so AdapTable renames Column Headers through the Layout only.

Note

  • AdapTable removes AG Grid's Edit Column Name item from the Column Menu, the Context Menu and the Columns Tool Panel menu
  • Developers can add it back via customColumnMenu / customContextMenu, but names edited that way bypass the Layout

Hint

To let users rename a Column at run-time, use the Change Caption Column Menu item, the Layout Wizard or the setColumnCaption function in Layout API