Context Menu - Custom Items
Summary
- Bespoke Menu Items can be added to the Context Menu by developers
Developers can provide bespoke Menu Items to add to the Context Menu.
Like all Context Menu manipulation, it is done using the customContextMenu property in Context Menu Options.
A number of properties can be provided for the bespoke Menu Item, including:
label- the text that appears in the Menu Itemicon- an Icon to displaydisabled- whether Menu Item is disabledhidden- whether Menu Item is visibleonClick- what happens when the Menu Item is clickedsubMenuItems- lower levels of Menu Items (enables the Menu Item to be as deeply nested as required)
- This example showws how to add a Custom Context Menu Item (to change the Theme)
- We place the item at the top of the Context Menu - and leave the default AG Grid and AdapTable Menu Items in place
- The custom Context Menu Item is hidden when opening the Column Menu in a Row-Grouped Column and disabled for
DateColumns
Expand to see the Menu Options configuration
Deep Dive
Anatomy of a Custom Context Menu Item
Developer Guide
Providing a Custom Context Menu Item
A few steps are required to define a Context Menu item:
// Provide Menu Type of 'User'
// Set the Label to Expand or Collapse based on current state
menuType: 'User',
label: (menuContext: ContextMenuContext) => {
return menuContext.adaptableApi.dashboardApi.isDashboardCollapsed()
? 'Expand Dashboard'
: 'Collapse Dashboard';
}The menuType is always 'User'.
The label property provides the Custom Menu Item's text.
It returns either a string directly or a function which receives ContextMenuContext and returns a string
// Expand or Collapse the Dashboard as required
onClick: (menuContext: ContextMenuContext) => {
menuContext.adaptableApi.dashboardApi.isDashboardCollapsed()
? menuContext.adaptableApi.dashboardApi.expandDashboard()
: menuContext.adaptableApi.dashboardApi.collapseDashboard();
},The onClick function is invoked by AdapTable when the menu item is selected by the user.
It receives a ContextMenuContext object and returns void.
// Make the Menu item visible but disabled
hidden: false,
disabled: trueUse hidden and disabled properties to set visibility and if enabled
Note
- In earlier versions of AdapTable, these 2 properties were functions that could be set dynamically each time the menu opened
- However this same logic can now be more flexibly set when configuring the Context Menu
// Provide an Icon
icon: {
src: 'https://img.icons8.com/flat-round/344/traffic-light--v1.png',
},
The Icon can be an object, URL or HTML Element
Find Out More
See Guide to Adaptable Icons for full details on the different types of Icons that can be used
// Provide 2 sub menu items to hide and float the Dashboard
subMenuItems: [
{
label: 'Hide Dashboard',
onClick: (menuContext: ContextMenuContext) => {
menuContext.adaptableApi.dashboardApi.hideDashboard();
},
},
{
label: 'Float Dashboard',
onClick: (menuContext: ContextMenuContext) => {
menuContext.adaptableApi.dashboardApi.floatDashboard();
},
},
],Each Menu Item can have Sub Menu Items allowing you to provide nested Menus as deep as required.
Each Sub Menu Item is simply another Menu Item object.
// Add custom menu Item before AG Grid and AdapTable menu items
menuOptions: {
customContextMenu: (context: CustomContextMenuContext) => {
return [
customDashboardMenuItem,
'-',
...context.defaultAgGridMenuStructure,
'-',
...context.defaultAdaptableMenuStructure,
];
},
},The customContextMenu property receives all AG Grid and AdapTable menu items as context, both a full list and the default structure.
Place the custom menu item in the desired location in the Context Menu.