YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: Menu Family: Adding A Context Menu To A Table

# Menu Family: Adding A Context Menu To A Table

This example creates context menu for an HTML table and illustrates how the content of a ContextMenu instance can be replaced on the fly based on the element that triggered its display.

Please Note: Opera users will need to do the following to use this example:

- Opera for Windows: Hold down the control key and click with the left mouse button.
- Opera for OS X: Hold down the command key (⌘) and click with the left mouse button.

View example in new window.

When adding context menus to large data structures like a `<table>` or large list (`<ol>` or `<ul>`), it is recommended to bind a single YAHOO.widget.ContextMenu instance to the structure's root element, than to each of its child nodes (`<tr>`s or `<li>`s). Doing so significantly improves the performance of a web page or application by reducing the number of "contextmenu" event handlers as well as the number of YAHOO.widget.ContextMenu instances in memory.

Begin by creating an `<table>` and giving `<tr>` elements that should have the same context menu a similar class name.

Next, use the `onContentReady` method of the Event utility to listen for when the `<table>` element is are ready to be scripted.

YAHOO.util.Event.onContentReady("dataset", function () { });

Inside the function passed to the Event Utility's `onContentReady` method, create a shortcut to the Dom Utility (since it will be used frequently) and an object literal that maps each class name to a set of MenuItem configuration properties.

YAHOO.util.Event.onContentReady("dataset", function () { var Dom = YAHOO.util.Dom; /* Map of CSS class names to arrays of MenuItem configuration properties. */ var oContextMenuItems = { "type1": [ "Context Menu 1, Item 1", { text: "Context Menu 1, Item 2", submenu: { id: "submenu1", lazyload: true, itemdata: [ "Context Menu 1 Submenu, Item 1", "Context Menu 1 Submenu, Item 2", "Context Menu 1 Submenu, Item 3", "Context Menu 1 Submenu, Item 4" ] } }, "Context Menu 1, Item 3", "Context Menu 1, Item 4" ], "type2": [ "Context Menu 2, Item 1", "Context Menu 2, Item 2", "Context Menu 2, Item 3", "Context Menu 2, Item 4", "Context Menu 2, Item 5", "Context Menu 2, Item 6", "Context Menu 2, Item 7", "Context Menu 2, Item 8", "Context Menu 2, Item 9", "Context Menu 2, Item 10" ], "type3": [ "Context Menu 3, Item 1", "Context Menu 3, Item 2", "Context Menu 3, Item 3", "Context Menu 3, Item 4" ], "type4": [ "Context Menu 4, Item 1", "Context Menu 4, Item 2" ], "type5": [ "Context Menu 5, Item 1", "Context Menu 5, Item 2", "Context Menu 5, Item 3", "Context Menu 5, Item 4", "Context Menu 5, Item 5", "Context Menu 5, Item 6" ] }; });

Lastly, add a "beforeShow" event handler to the ContextMenu instance. This event handler makes use of the "contextEventTarget" property to determine which `<tr>` element was the target of the "contextmenu" event. Once found, the `<tr>` element's class name is used to look up its corresponding menu items in the "oContextMenuItems" map, which are then added to the ContextMenu instance via the "addItems" method.

YAHOO.util.Event.onContentReady("dataset", function () { var Dom = YAHOO.util.Dom; /* Map of CSS class names to arrays of MenuItem configuration properties. */ var oContextMenuItems = { "type1": [ "Context Menu 1, Item 1", { text: "Context Menu 1, Item 2", submenu: { id: "submenu1", lazyload: true, itemdata: [ "Context Menu 1 Submenu, Item 1", "Context Menu 1 Submenu, Item 2", "Context Menu 1 Submenu, Item 3", "Context Menu 1 Submenu, Item 4" ] } }, "Context Menu 1, Item 3", "Context Menu 1, Item 4" ], "type2": [ "Context Menu 2, Item 1", "Context Menu 2, Item 2", "Context Menu 2, Item 3", "Context Menu 2, Item 4", "Context Menu 2, Item 5", "Context Menu 2, Item 6", "Context Menu 2, Item 7", "Context Menu 2, Item 8", "Context Menu 2, Item 9", "Context Menu 2, Item 10" ], "type3": [ "Context Menu 3, Item 1", "Context Menu 3, Item 2", "Context Menu 3, Item 3", "Context Menu 3, Item 4" ], "type4": [ "Context Menu 4, Item 1", "Context Menu 4, Item 2" ], "type5": [ "Context Menu 5, Item 1", "Context Menu 5, Item 2", "Context Menu 5, Item 3", "Context Menu 5, Item 4", "Context Menu 5, Item 5", "Context Menu 5, Item 6" ] }; var oSelectedTR; // The currently selected TR /* "beforeshow" event handler for the ContextMenu instance - replaces the content of the ContextMenu instance based on the CSS class name of the <tr> element that triggered its display. */ function onContextMenuBeforeShow(p_sType, p_aArgs) { var oTarget = this.contextEventTarget, aMenuItems, aClasses; if (this.getRoot() == this) { /* Get the <tr> that was the target of the "contextmenu" event. */ oSelectedTR = oTarget.nodeName.toUpperCase() == "TR" ? oTarget : Dom.getAncestorByTagName(oTarget, "TR"); /* Get the array of MenuItems for the CSS class name from the "oContextMenuItems" map. */ if (Dom.hasClass(oSelectedTR, "odd")) { aClasses = oSelectedTR.className.split(" "); aMenuItems = oContextMenuItems[aClasses[0]]; } else { aMenuItems = oContextMenuItems[YAHOO.lang.trim(oSelectedTR.className)]; } // Remove the existing content from the ContentMenu instance this.clearContent(); // Add the new set of items to the ContentMenu instance this.addItems(aMenuItems); // Render the ContextMenu instance with the new content this.render(); /* Highlight the <tr> element in the table that was the target of the "contextmenu" event. */ Dom.addClass(oSelectedTR, "selected"); } } /* "hide" event handler for the ContextMenu - used to clear the selected <tr> element in the table. */ function onContextMenuHide(p_sType, p_aArgs) { if (this.getRoot() == this && oSelectedTR) { Dom.removeClass(oSelectedTR, "selected"); } } /* Instantiate a ContextMenu: The first argument passed to the constructor is the id of the element to be created; the second is an object literal of configuration properties. */ var oContextMenu = new YAHOO.widget.ContextMenu("contextmenu", { trigger: "dataset", lazyload: true }); /* Subscribe to the ContextMenu instance's "beforeshow" and "hide" events. */ oContextMenu.subscribe("beforeShow", onContextMenuBeforeShow); oContextMenu.subscribe("hide", onContextMenuHide); });

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### Menu Family Examples:

- Basic Menu From Markup
- Basic Menu From JavaScript
- Grouped Menu Items Using Markup
- Grouped Menu Items Using JavaScript
- Grouped Menu Items With Titles From Markup
- Grouped Menu Items With Titles From JavaScript
- Multi-tiered Menu From Markup
- Multi-tiered Menu From JavaScript
- Handling Menu Click Events
- Listening For Menu Events
- MenuItem Configuration Properties
- Setting Menu Configuration Properties At Runtime
- Website Left Nav With Submenus Built From Markup
- Website Left Nav With Submenus From JavaScript
- Website Top Nav With Submenus Built From Markup
- Website Top Nav With Submenus From JavaScript
- Website Left Nav Using Animation With Submenus Built From Markup
- Website Left Nav Using Animation With Submenus From JavaScript
- Website Top Nav Using Animation With Submenus Built From Markup
- Website Top Nav Using Animation With Submenus From JavaScript
- Context Menu
- Adding A Context Menu To A Table
- Adding A Context Menu To A TreeView
- OS-Style Programs Menu
- Application Menubar
- Using the Menu ARIA Plugin
- Replacing the content of a Button's Menu (included with examples for Button Control)
- Using A Menu Button To Replace A <select> Element (included with examples for Button Control)
- Using the Button ARIA Plugin (included with examples for Button Control)
- Context Menu Integration (included with examples for DataTable Control)
- Menu Buttons (included with examples for Button Control)
- Split Buttons (included with examples for Button Control)
- Color Picker Button (included with examples for Button Control)
- Fixed Width Menu Button (included with examples for Button Control)
- Slider Button (included with examples for Button Control)
- Layout with Menu Controls (included with examples for Layout Manager)
- Using the ProfilerViewer API while Profiling the YUI Menu Control (included with examples for ProfilerViewer Control)

### More Menu Family Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2010 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings