Yahoo! Developer Network Home - Help

YDN Site Search

# 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 a set 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, create an object literal that maps each class name to a set of MenuItem configuration properties.

/* 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" ] };

Use the `onContentReady` method of the Event utility to instantiate the ContextMenu as soon as the elements whose "contextmenu" event trigger its display are ready to be scripted.

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.

/* Initialize the ContextMenu instance when the the elements that trigger their display are ready to be scripted. */ YAHOO.util.Event.onContentReady("dataset", function () { /* Utility function used to return the parent <tr> element of the actual node in the "dataset" table that fired the "contextmenu" event. */ function getTableRowFromEventTarget(p_oNode) { if (p_oNode.tagName.toUpperCase() == "TR") { return p_oNode; } else { /* If the target of the event was a child of a TR, get the parent TR element */ do { if (p_oNode.tagName.toUpperCase() == "TR") { return p_oNode; break; } } while ((p_oNode = p_oNode.parentNode)); } } 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 aMenuItems, aClasses; if (this.getRoot() == this) { /* Get the <tr> that was the target of the "contextmenu" event. */ oSelectedTR = getTableRowFromEventTarget(this.contextEventTarget); /* Get the array of MenuItems for the CSS class name from the "oContextMenuItems" map. */ if (YAHOO.util.Dom.hasClass(oSelectedTR, "odd")) { aClasses = oSelectedTR.className.split(" "); aMenuItems = oContextMenuItems[aClasses[0]]; } else { aMenuItems = oContextMenuItems[oSelectedTR.className.trim()]; } // 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. */ YAHOO.util.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) { YAHOO.util.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); });

### 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
- Implementing ARIA Roles and States with Menu
- Context Menu Integration (included with examples for DataTable Control (beta))
- 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)
- Using the ProfilerViewer API while Profiling the YUI Menu Control (included with examples for ProfilerViewer Control (beta))

### More Menu Family Resources:

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

Copyright © 2008 Yahoo! Inc. All rights reserved.

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