Yahoo! Developer Network Home - Help

YUI Library Examples: Menu Family: Setting Menu Configuration Properties At Runtime

Menu Family: Setting Menu Configuration Properties At Runtime

This example demonstrates how to set MenuItem configuration properties at runtime and listen for the changes through the "configChanged" event.

Note: By default clicking outside of a menu will hide it. Additionally, menu items without a submenu or a URL to navigate to will hide their parent menu when clicked. Click the "Show Menu" button below to make the menu visible if it is hidden.

Setting MenuItem configuration properties at runtime

A MenuItem has a number of configuration properties that affect its behavior and visual rendering. Any of these configuration properties can be set at runtime via the setProperty method of the item's configuration object (accessible via its cfg property.

This example ulitizes the following configuration properties: text, selected, disabled, helptext, url, emphasis, strongemphasis, and checked.

1/*
2     "configChange" event handler for each MenuItem instance - used 
3     to log info about the configuration property that was changed.
4*/ 
5 
6function onMenuItemConfigChange(p_sType, p_aArgs) { 
7 
8    var sPropertyName = p_aArgs[0][0], 
9        sPropertyValue = p_aArgs[0][1]; 
10 
11    YAHOO.log(("Index: " + this.index + ", " + 
12               "Group Index: " + this.groupIndex + ", " + 
13               "Custom Event Type: " + p_sType + ", " +                   
14               "\"" + sPropertyName + "\" Property Set To: \""  
15               + sPropertyValue + "\""), "info", "example12"); 
16 
17
18 
19 
20/*
21     Instantiate a Menu:  The first argument passed to the 
22     constructor is the id of the element in the page 
23     representing the Menu; the second is an object literal 
24     of configuration properties.
25*/ 
26 
27var oMenu = new YAHOO.widget.Menu("basicmenu", { fixedcenter: true }); 
28 
29 
30/*
31     Subscribe to the Menu instance's "itemAdded" event in order to 
32     subscribe to the "configChange" event handler of each MenuItem 
33     instance's configuration object.
34*/ 
35 
36oMenu.subscribe("itemAdded"function (p_sType, p_aArgs) { 
37 
38    var oMenuItem = p_aArgs[0]; 
39    
40    /*
41         Subscribe to the "configChange" event handler of each MenuItem 
42         instance's configuration object.
43    */ 
44 
45    oMenuItem.cfg.subscribe("configChanged", onMenuItemConfigChange); 
46 
47}); 
48 
49 
50/*
51    Add items to the Menu instance by passing an array of strings 
52    (each of which represents the "text" configuration property of a 
53    YAHOO.widget.MenuItem instance) to the "addItems" method.
54*/ 
55 
56oMenu.addItems([ 
57     
58        "Selected MenuItem"
59        "Disabled MenuItem"
60        "MenuItem With A URL"
61        "Checked MenuItem" 
62 
63    ]); 
64 
65 
66/*
67    Since this Menu instance is built completely from script, call the 
68    "render" method passing in the DOM element that it should be 
69    appended to.
70*/ 
71 
72oMenu.render("rendertarget"); 
73 
74 
75/*
76     Set a configuration property of each MenuItem to trigger the firing
77     of its configuration object's "configChanged" event.
78*/ 
79 
80oMenu.getItem(0).cfg.setProperty("selected"true); 
81oMenu.getItem(1).cfg.setProperty("disabled"true); 
82oMenu.getItem(2).cfg.setProperty("url""http://www.yahoo.com"); 
83oMenu.getItem(3).cfg.setProperty("checked"true); 
view plain | print | ?

YUI Logger Output:

Logger Console

INFO0ms (+0) 2:02:05 AM:

global

Logger initialized

Menu Family Examples:

More Menu Family Resources:

Copyright © 2008 Yahoo! Inc. All rights reserved.

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