Yahoo! Developer Network Home - Help

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.

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.

1/*
2    Map of CSS class names to arrays of MenuItem 
3    configuration properties.
4*/ 
5 
6var oContextMenuItems = { 
7 
8    "type1": [ 
9                "Context Menu 1, Item 1",  
10                { 
11                    text: "Context Menu 1, Item 2",  
12                    submenu: {  
13                                id: "submenu1",  
14                                lazyload: true,  
15                                itemdata: [ 
16                                    "Context Menu 1 Submenu, Item 1",  
17                                    "Context Menu 1 Submenu, Item 2",  
18                                    "Context Menu 1 Submenu, Item 3",  
19                                    "Context Menu 1 Submenu, Item 4" 
20                                ]  
21                            }  
22                },  
23                "Context Menu 1, Item 3",  
24                "Context Menu 1, Item 4" 
25            ], 
26 
27    "type2": [ 
28                "Context Menu 2, Item 1",  
29                "Context Menu 2, Item 2",  
30                "Context Menu 2, Item 3",  
31                "Context Menu 2, Item 4",  
32                "Context Menu 2, Item 5",  
33                "Context Menu 2, Item 6",  
34                "Context Menu 2, Item 7",  
35                "Context Menu 2, Item 8",  
36                "Context Menu 2, Item 9",  
37                "Context Menu 2, Item 10" 
38            ], 
39 
40    "type3": [ 
41                "Context Menu 3, Item 1",  
42                "Context Menu 3, Item 2",  
43                "Context Menu 3, Item 3",  
44                "Context Menu 3, Item 4" 
45            ], 
46 
47    "type4": [ 
48                "Context Menu 4, Item 1",  
49                "Context Menu 4, Item 2" 
50            ], 
51 
52    "type5": [ 
53                "Context Menu 5, Item 1",  
54                "Context Menu 5, Item 2",  
55                "Context Menu 5, Item 3",  
56                "Context Menu 5, Item 4",  
57                "Context Menu 5, Item 5",  
58                "Context Menu 5, Item 6" 
59            ] 
60 
61}; 
view plain | print | ?

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.

1/*
2     Initialize the ContextMenu instance when the the elements 
3     that trigger their display are ready to be scripted.
4*/ 
5 
6YAHOO.util.Event.onContentReady("dataset"function () { 
7 
8    var Dom = YAHOO.util.Dom; 
9 
10    var oSelectedTR;    // The currently selected TR 
11 
12 
13    /*
14         "beforeshow" event handler for the ContextMenu instance - 
15         replaces the content of the ContextMenu instance based 
16         on the CSS class name of the <tr> element that triggered
17         its display.
18    */ 
19 
20    function onContextMenuBeforeShow(p_sType, p_aArgs) { 
21 
22        var aMenuItems, 
23            aClasses; 
24 
25        if (this.getRoot() == this) { 
26 
27            /*
28                 Get the <tr> that was the target of the 
29                 "contextmenu" event.
30            */ 
31 
32            oSelectedTR = oTarget.nodeName.toUpperCase() == "TR" ?  
33                oTarget : Dom.getAncestorByTagName(oTarget, "TR"); 
34 
35 
36            /*
37                Get the array of MenuItems for the CSS class name from 
38                the "oContextMenuItems" map.
39            */ 
40 
41            if (Dom.hasClass(oSelectedTR, "odd")) { 
42 
43                aClasses = oSelectedTR.className.split(" "); 
44 
45                aMenuItems = oContextMenuItems[aClasses[0]]; 
46             
47            } 
48            else { 
49                 
50                aMenuItems = oContextMenuItems[YAHOO.lang.trim(oSelectedTR.className)]; 
51 
52            } 
53 
54 
55            // Remove the existing content from the ContentMenu instance 
56 
57            this.clearContent(); 
58             
59 
60            // Add the new set of items to the ContentMenu instance                     
61             
62            this.addItems(aMenuItems); 
63 
64 
65            // Render the ContextMenu instance with the new content 
66 
67            this.render(); 
68 
69 
70            /*
71                 Highlight the <tr> element in the table that was 
72                 the target of the "contextmenu" event.
73            */ 
74 
75            Dom.addClass(oSelectedTR, "selected"); 
76         
77        } 
78         
79    } 
80 
81 
82    /*
83         "hide" event handler for the ContextMenu - used to 
84         clear the selected <tr> element in the table.
85    */ 
86 
87    function onContextMenuHide(p_sType, p_aArgs) { 
88 
89        if (this.getRoot() == this && oSelectedTR) { 
90 
91            Dom.removeClass(oSelectedTR, "selected"); 
92         
93        } 
94     
95    } 
96 
97 
98    /*
99         Instantiate a ContextMenu:  The first argument passed to 
100         the constructor is the id of the element to be created; the 
101         second is an object literal of configuration properties.
102    */ 
103 
104    var oContextMenu = new YAHOO.widget.ContextMenu("contextmenu", {  
105                                                            trigger: "dataset",  
106                                                            lazyload: true  
107                                                            }); 
108 
109     
110    /*
111         Subscribe to the ContextMenu instance's "beforeshow" and 
112         "hide" events.
113    */ 
114 
115    oContextMenu.subscribe("beforeShow", onContextMenuBeforeShow); 
116    oContextMenu.subscribe("hide", onContextMenuHide);               
117 
118}); 
view plain | print | ?

Menu Family Examples:

More Menu Family Resources:

Copyright © 2008 Yahoo! Inc. All rights reserved.

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