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 
9    /*
10         Utility function used to return the parent <tr> element of
11         the actual node in the "dataset" table that fired the 
12         "contextmenu" event.
13    */ 
14 
15    function getTableRowFromEventTarget(p_oNode) { 
16 
17        if (p_oNode.tagName.toUpperCase() == "TR") { 
18         
19            return p_oNode; 
20 
21        } 
22        else { 
23 
24 
25            /*
26                 If the target of the event was a child of a TR, 
27                 get the parent TR element
28            */ 
29 
30            do { 
31 
32                if (p_oNode.tagName.toUpperCase() == "TR") { 
33 
34                    return p_oNode;                             
35 
36                    break
37                 
38                } 
39 
40            } 
41            while ((p_oNode = p_oNode.parentNode)); 
42         
43        } 
44     
45    } 
46 
47 
48    var oSelectedTR;    // The currently selected TR 
49 
50 
51    /*
52         "beforeshow" event handler for the ContextMenu instance - 
53         replaces the content of the ContextMenu instance based 
54         on the CSS class name of the <tr> element that triggered
55         its display.
56    */ 
57 
58    function onContextMenuBeforeShow(p_sType, p_aArgs) { 
59 
60        var aMenuItems, 
61            aClasses; 
62 
63        if (this.getRoot() == this) { 
64 
65            /*
66                 Get the <tr> that was the target of the 
67                 "contextmenu" event.
68            */ 
69 
70            oSelectedTR = getTableRowFromEventTarget(this.contextEventTarget); 
71 
72 
73            /*
74                Get the array of MenuItems for the CSS class name from 
75                the "oContextMenuItems" map.
76            */ 
77 
78            if (YAHOO.util.Dom.hasClass(oSelectedTR, "odd")) { 
79 
80                aClasses = oSelectedTR.className.split(" "); 
81 
82                aMenuItems = oContextMenuItems[aClasses[0]]; 
83             
84            } 
85            else { 
86                 
87                aMenuItems = oContextMenuItems[oSelectedTR.className.trim()]; 
88 
89            } 
90 
91 
92            // Remove the existing content from the ContentMenu instance 
93 
94            this.clearContent(); 
95             
96 
97            // Add the new set of items to the ContentMenu instance                     
98             
99            this.addItems(aMenuItems); 
100 
101 
102            // Render the ContextMenu instance with the new content 
103 
104            this.render(); 
105 
106 
107            /*
108                 Highlight the <tr> element in the table that was 
109                 the target of the "contextmenu" event.
110            */ 
111 
112            YAHOO.util.Dom.addClass(oSelectedTR, "selected"); 
113         
114        } 
115         
116    } 
117 
118 
119    /*
120         "hide" event handler for the ContextMenu - used to 
121         clear the selected <tr> element in the table.
122    */ 
123 
124    function onContextMenuHide(p_sType, p_aArgs) { 
125 
126        if (this.getRoot() == this && oSelectedTR) { 
127 
128            YAHOO.util.Dom.removeClass(oSelectedTR, "selected"); 
129         
130        } 
131     
132    } 
133 
134 
135    /*
136         Instantiate a ContextMenu:  The first argument passed to 
137         the constructor is the id of the element to be created; the 
138         second is an object literal of configuration properties.
139    */ 
140 
141    var oContextMenu = new YAHOO.widget.ContextMenu("contextmenu", {  
142                                                            trigger: "dataset",  
143                                                            lazyload: true  
144                                                            }); 
145 
146     
147    /*
148         Subscribe to the ContextMenu instance's "beforeshow" and 
149         "hide" events.
150    */ 
151 
152    oContextMenu.subscribe("beforeShow", onContextMenuBeforeShow); 
153    oContextMenu.subscribe("hide", onContextMenuHide);               
154 
155}); 
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