Solved

In Flex/Actionscript, I am using an <mx:MenuBar> component.  How do I register/respond to a user clicking on a submenu item that itself has another submenu (ie. children)?

Posted on 2008-06-11
6
4,653 Views
Last Modified: 2011-10-03
In Flex/Actionscript, I am using an <mx:MenuBar> component which has 3 to 4 levels of submenus.  How do I register/respond to a user clicking on a submenu item that itself has another submenu (ie. children)?  The "itemClick" event seems to only respond to submenu items that have no children (ie. descendants).  But I need to be able to register a click/selection on submenu items at all levels, not just the end children.
<mx:MenuBar id="mainMenuBar" itemClick="handleSubMenuBarClickEvent(event);">
</mx:MenuBar>

Open in new window

0
Comment
Question by:robbersrow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 5

Expert Comment

by:maclema
ID: 21792446
Here is one way to accomplish this.

On roll over of each item, add a click listener to the item rolled over..
<mx:MenuBar x="10" y="10" dataProvider="{menudp.item}" labelField="@name" itemRollOver="handleItemRollOver(event)"></mx:MenuBar>
 
private function handleItemRollOver(e:MenuEvent):void {
	//add click listener's to the actual MenuBar items.
	if ( e.itemRenderer is MenuBarItem ) {
		var item:MenuBarItem = MenuBarItem(e.itemRenderer);
		if ( !item.hasEventListener(MouseEvent.CLICK) ) {
			item.addEventListener(MouseEvent.CLICK, handleItemClick);
		}
	}
	//add listeners to the menu items
	else {
		var renderer:MenuItemRenderer = MenuItemRenderer(e.itemRenderer);
		if ( !renderer.hasEventListener(MouseEvent.CLICK) ) {
			renderer.addEventListener(MouseEvent.CLICK, handleItemClick);
		}
	}
}
 
private function handleItemClick(e:MouseEvent):void {
	var item:* = e.currentTarget;
	trace(item.data);
}

Open in new window

0
 

Author Comment

by:robbersrow
ID: 21881634
Thank you for your response, and sorry for my delay in getting back to you.  I tested this, and I think this solution is almost complete, but still missing the final step.  I can get the MenuBar to register the MouseEvent.CLICK both on the main menu items and sub menu items. But then there is a problem at the next step, when inside the "handleItemClick" function.  The "item.data" statement does not get me the actual "data" property of the "menuitem" from the dataprovider.  I need to be able to access properties of the submenu "menuitem" that was clicked (ie. data, label, type, children).  An example of this is in the code below, and you can see how i need to access particularly the "data" property to figure out which value to pass to the next page.
<menuitem label="MenuItem 1" data="1">
    <menuitem label="MenuItem 1-A" data="100"/>
    <menuitem label="MenuItem 1-B" data="101"/>
    <menuitem label="MenuItem 1-C" data="102"/>
</menuitem>
<menuitem label="MenuItem 2" data="2">
    <menuitem label="MenuItem 2-A" data="200"/>
    <menuitem label="MenuItem 2-B" data="201"/>
    <menuitem label="MenuItem 2-C" data="202"/>
</menuitem>
 
//OR as I am implementing the dataProvider through an ArrayCollection
 
public var menuBarArrayCollection:ArrayCollection = new ArrayCollection([
{label:"Power", data:3, children:[
    {label:"Classic", data:212},
    {label:"Gripper", data:201, children:[
        {label:"Gripper 1", data:204}, 
        {label:"Gripper 2", data:205}, 
        {label:"Gripper 3", data:206}
    ]}
]);

Open in new window

0
 
LVL 5

Accepted Solution

by:
maclema earned 500 total points
ID: 21882564
Here is what I tried, and I can access all the properties.
<mx:Script>
	<![CDATA[
		import mx.controls.menuClasses.MenuItemRenderer;
		import mx.controls.menuClasses.MenuBarItem;
		import mx.events.MenuEvent;
		import mx.collections.ArrayCollection;
		
		public var menuBarArrayCollection:ArrayCollection = 
		new ArrayCollection([
			{label:"Power", data:3, 
				children:[
			    	{label:"Classic", data:212},
			    	{label:"Gripper", data:201, 
			    		children:[
			        		{label:"Gripper 1", data:204}, 
			        		{label:"Gripper 2", data:205}, 
			        		{label:"Gripper 3", data:206}
			    		]
			    	}
			    ]
			}
		]);
		
		private function handleItemRollOver(e:MenuEvent):void {
	        //add click listener's to the actual MenuBar items.
	        if ( e.itemRenderer is MenuBarItem ) {
	                var item:MenuBarItem = MenuBarItem(e.itemRenderer);
	                if ( !item.hasEventListener(MouseEvent.CLICK) ) {
	                        item.addEventListener(MouseEvent.CLICK, handleItemClick);
	                }
	                item.invalidateProperties();
	        }
	        //add listeners to the menu items
	        else {
	                var renderer:MenuItemRenderer = MenuItemRenderer(e.itemRenderer);
	                if ( !renderer.hasEventListener(MouseEvent.CLICK) ) {
	                        renderer.addEventListener(MouseEvent.CLICK, handleItemClick);
	                }
	                renderer.invalidateProperties();
	        }
	}
	 
	private function handleItemClick(e:MouseEvent):void {
	        var item:* = e.currentTarget;
	        trace(item.data.children);
	        trace(item.data.label);
	        trace(item.data.data);
	}
	]]>
</mx:Script>

Open in new window

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Closing Comment

by:robbersrow
ID: 31466069
This worked perfectly!  Thank you for your solution.
0
 

Author Comment

by:robbersrow
ID: 21886621
For some reason, now when I roll over the main menuBar items (not the submenu items), the background skin/color is not applied.  Do you have any idea why this might be the case?  Any suggestions would be greatly appreciated.  Thanks again for your help with this issue.
0
 

Author Comment

by:robbersrow
ID: 21886936
Please ignore my last post, I figured it out.  It was the "invalidateProperties()" method that was causing the issue.  I removed the method and it seems to work fine now.  Thanks.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Collecting Customer Data 5 38
PHP OAuth Uncaught exception 'OAuthException' 3 80
Website authentication - asp.net 3 98
Video and audio capture on Windows 7 3 127
      Install BugTracker on Windows 2008 server Step 1:  Install windows 2008 server 32 bit OS and configure IIS. Step 2:  Install SQL server ( SQL server 2005 or SQL server 2005 Express edition. The installer for 2008  version isn’t very f…
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question