Link to home
Start Free TrialLog in
Avatar of robbersrow
robbersrow

asked on

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)?

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

Avatar of maclema
maclema

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

Avatar of robbersrow

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of maclema
maclema

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This worked perfectly!  Thank you for your solution.
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.
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.