The Column Menu is launched from a column header and provides actions such as sorting, pinning and sizing columns. Its Choose Columns item opens the separate Column Chooser, while enabled column filters can be opened from the menu or the header filter button.
AG Grid Community does not have a menu, but can launch Column Filters if enabled (see Launching Filters for configuration details).
The following example shows the column menu:
The Athlete column does not have filtering enabled, and only shows the main menu.
The Age column has filtering enabled, and shows an additional filter icon. Open and apply a filter to see the behaviour.
The Country column has filtering enabled with the floating filter. Open and apply a filter to see the behaviour.
Right-clicking on the column headers will also display the column menu.
Right-clicking in the empty space to the right of the column headers will display the column menu with options to choose/reset the columns.
The menu shows a default set of items. You can add your own items, or change which defaults are shown, via two independent properties - use whichever suits, or both:
colDef.columnMenuItems - set per column. Either a list of menu items, or a callback which is passed the list of default items.
getColumnMenuItems() - a grid option callback which is passed the list of default items, the column, and the source of the menu.
When both are set, colDef.columnMenuItems takes priority over getColumnMenuItems().
The source param is one of 'columnMenu', 'columnsToolPanel' or 'columnChooser', so a single callback can tailor the items for the column menu, the Columns Tool Panel context menu, and the Column Chooser.
Each item is either a string or a MenuItemDef. Use a string to pick a built-in item and a MenuItemDef for your own. All built-in tokens share one type, DefaultColumnMenuItem, and each is shown only where it applies to the column and grid state. The column menu's built-in items are listed below.
Customise the menu items shown for this column across the column menu, the Columns Tool Panel
right-click menu, and the Column Chooser. The source param indicates which surface the menu is for.
Takes precedence over mainMenuItems.
For customising the menu items shown for a column across the column menu, the Columns Tool Panel
right-click menu, and the Column Chooser. The source param indicates which surface the menu is for;
branch on it to target a single surface. Takes precedence over getMainMenuItems for the column menu.
colDef.mainMenuItems and the grid option getMainMenuItems() are the original way to customise the menu. They behave the same way, but apply to the column menu only - not the Columns Tool Panel or Column Chooser - and their callbacks do not receive a source. Prefer columnMenuItems / getColumnMenuItems(); these older properties remain supported and take effect when the newer ones are not set.
The full order of precedence is colDef.columnMenuItems, then getColumnMenuItems(), then colDef.mainMenuItems, then getMainMenuItems(). A grid-level getColumnMenuItems() therefore takes precedence over a per-column mainMenuItems.
The following is a list of all the default built-in menu items with the rules about when they are shown.
sortAscending: Sort the column in ascending order. Not included in the default items when columnMenu = 'legacy', or when the column is already sorted in ascending order.
sortDescending: Sort the column in descending order. Not included in the default items when columnMenu = 'legacy', or when the column is already sorted in descending order.
sortAbsoluteAscending: Sort the column in ascending order by magnitude, ignoring the sign - see Absolute Sorting. Not included in the default items when columnMenu = 'legacy', when the column does not allow absolute sorting, or when the column is already sorted in absolute ascending order.
sortAbsoluteDescending: Sort the column in descending order by magnitude, ignoring the sign - see Absolute Sorting. Not included in the default items when columnMenu = 'legacy', when the column does not allow absolute sorting, or when the column is already sorted in absolute descending order.
sortUnSort: Clear the sort on the column. Not included in the default items when columnMenu = 'legacy', or when the column is not sorted.
calculatedColumn: Show the Calculated Columns options. If the column selected is a Calculated Column, the menu will show options to edit and remove the column.
editColumnName: Rename the column header. Only shown when headerNameEditable is set on the column, and never on a calculated column, which is renamed via its Edit Calculated Column dialog instead.
columnFilter: Show the column filter. Not included in the default items when columnMenu = 'legacy', a filter is not enabled, or the header filter button or floating filter button are displayed.
columnChooser: Show the Column Chooser. Not included in the default items when columnMenu = 'legacy'.
pinSubMenu: Sub-menu for pinning. Always shown.
valueAggSubMenu: Sub-menu for value aggregation. Always shown.
autoSizeThis: Auto-size the current column. Always shown.
autoSizeAll: Auto-size all columns. Always shown.
rowGroup: Group by this column. Only shown if column is not grouped. Note this will appear once there is row grouping.
rowUnGroup: Un-group by this column. Only shown if column is grouped. Note this will appear once there is row grouping.
resetColumns: Reset column details. Always shown.
expandAll: Expand all groups. Only shown if grouping by at least one column.
contractAll: Collapse all groups. Only shown if grouping by at least one column.
The defaultItems list will change on different calls, depending on, for example, which columns are currently used for grouping.
If you do not override the list of menu items, then the items displayed will be based on the rules above.
The columnMenu = 'legacy' rules above apply to the default items only. A token supplied explicitly through columnMenuItems, getColumnMenuItems(), mainMenuItems or getMainMenuItems() is still rendered under the legacy menu.
Columns Tool Panel tokens such as value can also be returned here, and are shown where they apply to the column.
Menu items can be grouped together by adding separators between groups. Separators are defined by the string value 'separator'. For example, you could add menu item separators as follows:
The following example demonstrates the colDef.columnMenuItems property:
The Athlete column shows the list of built-in items.
The Age column includes the value token before its custom items. The grid is grouped by Sport, so Add Age to values aggregates Age in the group rows.
The Country column provides two custom items and one built-in item, Reset Columns (resetColumns). Clicking a custom item logs to the developer console.
The Year column keeps the default items but removes the separators, the pinning sub-menu, and the value aggregation sub-menu.
Selecting Choose Columns from the column menu opens the Column Chooser, which allows users to show, hide and reorder columns. See Column Chooser for configuration, custom labels, layouts and API usage.
Column filters are not considered part of the menu, so have their own API methods to show/hide.
However, when using the Legacy Tabbed Column Menu, the filter popup is part of the column menu, and can be opened/closed via the column menu API methods.
The column menu visibility has changed. Fires twice if switching between tabs - once with the old tab and once with the new tab.
The following example demonstrates the column menu API and events (by clicking the buttons outside the grid).
Note that the column menu and column filter popup close automatically when clicking outside the grid, so there are no buttons to close them in the example.
If not happy with the position of the popup, you can override its position using the postProcessPopup(params) callback. This gives you the popup HTML element so you can change its position should you wish to.
Under most scenarios, the menu will fit inside the grid. However if the grid is small and / or the menu is very large, then the menu will not fit inside the grid and it will be clipped. This will lead to a bad user experience.
To fix this, you should set the Popup Parent property.
The menu can also be displayed in the legacy tabbed format with three panels by setting the grid option columnMenu = 'legacy'. If you want to change the order in which panels are shown, or hide them, you can specify the property menuTabs in the colDef.
The property menuTabs is an array of strings. The valid values are: 'filterMenuTab', 'generalMenuTab' and 'columnsMenuTab'.
generalMenuTab: Include to show the main panel.
filterMenuTab: Include to show the filter panel.
columnsMenuTab: Include to show the column chooser panel.
The order of the menu tabs shown in the menu will match the order you specify in this array.
If you don't specify a menuTabs for a colDef the default is: ['generalMenuTab', 'filterMenuTab', 'columnsMenuTab']
The following example demonstrates the default tabbed menu:
The Athlete column shows the default tabs.
The Age column changes the order of the tabs to ['filterMenuTab', 'generalMenuTab', 'columnsMenuTab']
The Country column changes the order of the tabs to ['filterMenuTab', 'columnsMenuTab']. Note that the 'generalMenuTab' is suppressed.
The Year column changes the tabs to ['generalMenuTab']. Note that the 'filterMenuTab' and 'columnsMenuTab' are suppressed.
The Sport column hides the menu by suppressing all the menuTabs that can be shown: [].
import { useState, useEffect } from 'react';
/**
* Fetch example Json data
* Not recommended for production use!
*/
export const useFetchJson = <T,>(url:string, limit?: number) => {
const [data, setData] = useState<T[]>();
const [loading, setLoading] = useState(true);
useEffect(() => {
// StrictMode runs this effect twice: drop the superseded run's response rather than applying both.
let cancelled = false;
const fetchData = async () => {
setLoading(true);
// Note error handling is omitted here for brevity
const response = await fetch(url);
const json = await response.json();
const data = limit ? json.slice(0, limit) : json;
if (cancelled) {
return;
}
setData(data);
setLoading(false);
};
fetchData();
return () => {
cancelled = true;
};
}, [url, limit]);
return { data, loading };
};
With the legacy menu, the column menu button is hidden until moused over. This can be changed to always show the button using the grid option suppressMenuHide.
Only recommended for use if columnMenu = 'legacy'.
When true, the column menu button will always be shown.
When false, the column menu button will only show when the mouse is over the column header.
When using columnMenu = 'legacy', this will default to false instead of true.