React Data Grid

Column Headers

react logo

Each Column has a Column Header providing a Header Name and typically functions such as Column Resize, Row Sorting and Row Filtering.

Header Name

When no header name is provided, the grid will derive the header name from the provided field. The grid expects the field value to use Camel Case and will insert spaces between word breaks to build a readable header name.

const [columnDefs, setColumnDefs] = useState([
    // header name will be 'Athlete'
    { field: 'athlete' },
    // header name will be 'First Name'
    { field: 'firstName' },
    // header name will be 'foo'
    { headerName: 'foo', field: 'bar' }
]);

<AgGridReact columnDefs={columnDefs} />

Header Height

These properties can be used to change the different heights used in the headers.

As you can see in the example below, if you change any of the header heights, this change will be reflected automatically. Note how if the value is cleared (set to undefined), it might reuse other values. To see all the interactions check the properties descriptions at the top of the page.

Auto Header Height

The column header row can have its height set automatically based on the content of the header cells. This is most useful when used together with Custom Header Components or when using the wrapHeaderText column property.

To enable this, set autoHeaderHeight=true on the column definition you want to adjust the header height for. If more than one column has this property enabled, then the header row will be sized to the maximum of these column's header cells so no content overflows.

The example below demonstrates using the autoHeaderHeight property in conjunction with the wrapHeaderText property, so that long column names are fully displayed.

  • Note that the long column header names wrap onto another line
  • Try making a column smaller by dragging the resize handle on the column header, observe that the header will expand so the full header content is still visible.

When autoHeaderHeight=true the Grid automatically disables Span Header Height, see: Suppress Span Header Height.

Selecting Components

By default the grid uses the provided Header Component. To use a Custom Header Component set headerComponent on the Column Definition.

// a list of column definitions
const colDefs = [{

   // no Header Comp specified, uses the Provided Header Comp
   {headerName: "Athlete", field: "athlete"},
   {headerName: "Sport", field: "sport"},

   // uses Custom Header Comp
   {headerName: "Age", field: "age", headerComponent: MyHeaderComponent}
}]

See Registering Components for an overview of registering componnets.

Provided Component

Most applications will use the Provided Header Component. It provides functionality such as Sorting, Filtering and Column Menu.

You can provide an HTML template to the Default Header Component for simple layout changes. This is the default template used in AG Grid:

<div class="ag-cell-label-container" role="presentation">
    <span ref="eMenu" class="ag-header-icon ag-header-cell-menu-button" aria-hidden="true"></span>
    <span ref="eFilterButton" class="ag-header-icon ag-header-cell-filter-button" aria-hidden="true"></span>
    <div ref="eLabel" class="ag-header-cell-label" role="presentation">
        <span ref="eText" class="ag-header-cell-text"></span>
        <span ref="eFilter" class="ag-header-icon ag-header-label-icon ag-filter-icon" aria-hidden="true"></span>
        <span ref="eSortOrder" class="ag-header-icon ag-header-label-icon ag-sort-order" aria-hidden="true"></span>
        <span ref="eSortAsc" class="ag-header-icon ag-header-label-icon ag-sort-ascending-icon" aria-hidden="true"></span>
        <span ref="eSortDesc" class="ag-header-icon ag-header-label-icon ag-sort-descending-icon" aria-hidden="true"></span>
        <span ref="eSortNone" class="ag-header-icon ag-header-label-icon ag-sort-none-icon" aria-hidden="true"></span>
    </div>
</div>

When you provide your own template, everything should work as expected as long as you re-use the same refs.

RefDescription
eMenuThe container where the column menu icon will appear to enable opening the column menu.
eFilterButtonThe container where the column filter icon will appear to enable opening the filter (when using columnMenu = 'new').
eLabelThe container where there is going to be an onClick mouse listener to trigger the sort.
eTextThe text displayed on the column.
eFilterThe container with the icon that will appear if the user filters this column.
eSortOrderIn case of sorting on multiple columns, this shows the index that represents the position of this column in the order.
eSortAscIn case of sorting ascending the data in the column, this shows the associated icon.
eSortDescIn case of sorting descending the data in the column, this shows the descending icon.
eSortNoneIn case of no sort being applied, this shows the associated icon. Note this icon by default is empty.

The ref parameters are used by the grid to identify elements to add functionality to. If you leave an element out of your template, the functionality will not be added. For example if you do not specify eLabel then the column will not react to click events for sorting.

Templates are not meant to let you configure icons. If you are looking to change the icons, check our icon docs.

Set the template using colDef.headerComponentParams. Set on the grid options defaultColDef to set for all Columns.

const defaultColDef = useMemo(() => { 
	return {
        width: 100,
        headerComponentParams: {
            template:
                `<div class="ag-cell-label-container" role="presentation">
                  <span ref="eMenu" class="ag-header-icon ag-header-cell-menu-button"></span>
                  <span ref="eFilterButton" class="ag-header-icon ag-header-cell-filter-button"></span>
                  <div ref="eLabel" class="ag-header-cell-label" role="presentation">
                    <span ref="eSortOrder" class="ag-header-icon ag-sort-order"></span>
                    <span ref="eSortAsc" class="ag-header-icon ag-sort-ascending-icon"></span>
                    <span ref="eSortDesc" class="ag-header-icon ag-sort-descending-icon"></span>
                    <span ref="eSortNone" class="ag-header-icon ag-sort-none-icon"></span>
                    ** <span ref="eText" class="ag-header-cell-text" role="columnheader"></span>
                    <span ref="eFilter" class="ag-header-icon ag-filter-icon"></span>
                  </div>
                </div>`
        }
    };
}, []);

<AgGridReact defaultColDef={defaultColDef} />

Note that specifying your own templates is compatible with other configurations:

  • suppressHeaderMenuButton is specified in: Athlete, Country, Date and Bronze columns
  • sortable=false is specified in: Age, Year, Sport, Silver and Total columns
  • Gold is the only column that doesn't have sortable=false or suppressHeaderMenuButton

Custom Component

Column Headers by default use the Provided Header Component. The default can be overridden with Custom Header Component. This example shows a Custom Header Component. Note the following:

  • Column moving and resizing works without custom logic.
  • Use of suppressHeaderMenuButton=true to suppress the menu.
  • Use of sortable=false to suppress sorting.
  • Configurable menu icon.

The following props are passed to the Custom Component (CustomHeaderProps interface).

Responsibilities

The grid provides the following features that should not be implemented by Custom Header Components:

  • Resizing: When enabled, the grid will put an invisible widget to be grabbed by the mouse for resizing.
  • Header Checkbox Selection: When enabled, the grid displays a checkbox for 'select all' in the header.
  • Column Moving The grid will react to Column Dragging to reorder columns.

The Custom Header Component is responsible for the following:

  • Sorting: You will need to process user interaction for sorting. The default grid component sorts when the user clicks the header with the mouse. You may also need to display icons as the sort state of the column changes.
  • Filtering: You do not filter via the column (you filter from inside the menu), however you may need to display icons as the filter state of the column changes.
  • Menu: If you want the user to be able to open the column menu, you will need to manage this user interaction. The default grid component provides a button for the user to click to show the menu.
  • Anything Else: Whatever you want, you are probably creating a custom header to add your own functionality in.

Sorting

How you interact with the user for sorting (eg do you listen for mouse clicks?) is up to you. The grid helps you by providing column state and events for getting and setting the sort.

After the user requests a sort, you should call ONE of the following:

  1. props.progressSort(multiSort): Call this method to progress the sort on the column to the next stage. This uses the grid logic to determine the next sort stage (eg 'descending' normally follows 'ascending').
  2. props.setSort(direction, multiSort): Use this to set to sort to a specific state. Use this if you don't want to use the grids logic for working out the next sort state.
// option 1) tell the grid when you want to progress the sorting
onSortClicked(event) {
     // in this example, we do multi sort if Shift key is pressed
    this.props.progressSort(event.shiftKey);
};

// or option 2) tell the grid when you want to set the sort explicitly
// button that always sorts ASCENDING
onSortAscClicked(event) {
    this.props.setSort('asc', event.shiftKey);
};

// button that always sorts DESCENDING
onSortDescClicked(event) {
    this.props.setSort('desc', event.shiftKey);
};

To know when a column's sort state has changed (eg when to update your icons), you should listen for sortChanged event on the column.

// listen to the column for sort events
column.addEventListener('sortChanged', function() {

   // get sort state from column
   var sort = column.getSort();
   console.log('sort state of column is ' + sort); // prints one of ['asc',desc',null]

   // then do what you need, eg set relevant icons visible
   var sortingAscending = sort==='asc';
   var sortingDescending = sort==='desc';
   var notSorting = !sortingAscending && !sortingDescending;
   // how you update your GUI accordingly is up to you
});

// don't forget to remove your listener in your destroy code

Filtering

The header doesn't normally initiate filtering. If it does, use the standard grid API to set the filter. The header will typically display icons when the filter is applied. To know when to show a filter icon, listen to the column for filterChanged events.

// listen to the column for filter events
column.addEventListener('filterChanged', function() {
   // when filter changes on the col, this will print one of [true,false]
   console.log('filter of column is ' + column.isFilterActive());
});

// don't forget to remove your listener in your destroy code

How you get the user to ask for the column menu is up to you. When you want to display the menu, call the params.showColumnMenu() callback. The callback takes the HTML element for the button so that it can place the menu over the button (so the menu appears to drop down from the button).

onMenuClicked() {
    this.props.showColumnMenu(refButton.current);
});

Custom Props

On top of the props provided by the grid, you can also provide your own parameters. This is useful if you want to allow configuring the header component. For example, you might have a header component for formatting currency which also requires the currency symbol to be provided.

colDef = {
   ...
   headerComponent: MyHeaderComponent;
   headerComponentParams : {
       currencySymbol: '£' // the pound symbol will be placed into params
   }
}

Keyboard Navigation

When using Custom Header Components, the Custom Header Component is responsible for implementing support for keyboard navigation among its focusable elements. This is why by default, focusing a grid header with a Custom Header Component will focus the entire cell instead of any of the elements inside.

Adding support for keyboard navigation and focus requires a custom suppressHeaderKeyboardEvent function in grid options. See Suppress Keyboard Events.

An example of this is shown below, enabling keyboard navigation through the custom header elements when pressing ⇥ Tab and ⇧ Shift+⇥ Tab:

  • Click on the top left Athlete header, press the ⇥ Tab key and notice that the button, textbox and link in the Country header can be tabbed into. At the end of the cell elements, the tab focus moves to the next Age header cell
  • Use ⇧ Shift+⇥ Tab to navigate in the reverse direction

The suppressHeaderKeyboardEvent callback is used to capture tab events and determine if the user is tabbing forward or backwards. It also suppresses the default behaviour of moving to the next cell if tabbing within the child elements.

If the focus is at the beginning or the end of the cell children and moving out of the cell, the keyboard event is not suppressed, so focus can move between the children elements. Also, when moving backwards, the focus needs to be manually set while preventing the default behaviour of the keyboard press event.

Dynamic Tooltips

When using Custom Header Components it might be necessary to have a better control of how Tooltips are added instead of simply using the headerTooltip config. For this purpose, we provide the setTooltip method.

Properties available on the CustomHeaderProps<TData = any, TContext = any> interface.

The example below demonstrates using the Dynamic Tooltips with a Custom Header Component.

  • Note that only Column Headers where the text is not fully displayed will show tooltips.

Tooltips

Tooltips can be added to the Column Header by using the headerTooltip property of the ColDef.

The example below demonstrates using the headerTooltip property in the grid columns.

Text Orientation

By default, the text label for the header is display horizontally, i.e. as normal readable text. To display the text in another orientation you have to provide your own CSS to change the orientation and also provide the adequate header heights using the appropriate grid property.

The following example shows how you can provide a unique look and feel to the headers. Note that:

  • The header heights have all been changed via grid options:
// Group columns
const groupHeaderHeight = 75;
// Label columns
const headerHeight = 150;
// Floating filter
const floatingFiltersHeight = 50;
// Pivoting, requires turning on pivot mode. Label columns
const pivotHeaderHeight = 100;
// Pivoting, requires turning on pivot mode. Group columns
const pivotGroupHeaderHeight = 50;

<AgGridReact
    groupHeaderHeight={groupHeaderHeight}
    headerHeight={headerHeight}
    floatingFiltersHeight={floatingFiltersHeight}
    pivotHeaderHeight={pivotHeaderHeight}
    pivotGroupHeaderHeight={pivotGroupHeaderHeight}
/>
  • The grouped column header Athlete Details has a specific style applied to it to make it bigger. Note that the style is slightly different depending if pivoting or not:
.ag-pivot-off .ag-header-group-cell {
    font-size: 50px;
    color: red;
}

.ag-pivot-on .ag-header-group-cell {
    font-size: 25px;
    color: green;
}
  • The column labels have CSS applied to them so they are displayed vertically.
.ag-header-cell-label {
    /* Necessary to allow for text to grow vertically */
    height: 100%;
    padding: 0 !important;
}

.ag-header-cell-label .ag-header-cell-text {
    /* Force the width corresponding to how much width
    we need once the text is laid out vertically */
    width: 30px;
    transform: rotate(90deg);
    margin-top: 50px;
    /* Since we are rotating a span */
    display: inline-block;
}
  • The floating filters are using a much bigger area and the font used is bigger and bolder.
.ag-floating-filter-body input {
    height: 49px;
}

.ag-floating-filter-button {
    margin-top: -49px;
}

.ag-floating-filter-button button {
    height: 49px
}

.ag-floating-filter-body input {
    font-size: 15px;
    font-weight: bold;
}
  • The styling of the column labels have also been tweaked depending if pivoting or not.
.ag-pivot-off .ag-header-cell-label {
    color: #8a6d3b;
}

.ag-pivot-on .ag-header-cell-label {
    color: #1b6d85;
    font-weight: bold;
}