Apr 06

Here’s a simple example of how to add custom context menu items to your application.
It’s a little awkward on a List-based component to capture the list item where the context menu was brought up.
I’ve heard of people capturing and storing the rollOver events on the list, and you then know that the last item rolled over is the one where the context menu was brought up.
But, if your lists itemRenderer implements mx.controls.listClasses.IDropInListItemRenderer, you can get the selectedIndex from the listData without having to pay attention to rollOver events.
This example will trace the selected list items label and rowIndex, and the caption of the selected context menu item.
Because I was using an itemRenderer it got a little weird once I was trying to get the target list item. If you right-click on the actual label in the list, the events mouseTarget is UITextField, but if you right-click on the renderer itself then the events mouseTarget is the listRenderer that you want. So here’s how I check for that…

/* The user may click right on an object inside the renderer
* ... in our case it would be the label. If so, the target is the UITextField
*/
if( ! (target is listRenderer) )
{
selectedListRenderer	 	= target.parent.parent;
// otherwise they are clicking right on the renderer itself...
// ( outside of the label )
} else {
selectedListRenderer	 	= listRenderer(event.mouseTarget);
}

I haven’t tested this with any other objects in the itemRenderer. It sure ould be nice if this worked no matter what the contents of the itemRenderer are.
Click here to view the source

Post to Twitter

10 Responses to “Custom context menus on Lists: Flex 2 Beta 2”

  1. milan says:

    hi, thanks for this example, but I can’t see the source. Is this example builded with beta 2?

  2. Mac Martine says:

    Sorry about that, I just fixed it.

  3. Lucas says:

    Are you a member of the GT Crew on the midnight prowl? This is BBLDD.

  4. Mac Martine says:

    What? Luke-Dawg? Is this all of a sudden becoming a meet old friends site? Dang. Let’s take this off-line.

  5. James Ward says:

    Are you able to add more than 2 custom items to the context menu? I’m get 2 max. Strange. Thanks.
    -James

  6. Foobiahom says:

    Hello, excellent site, very rich in content
    and correctly carefully thought out,
    personally I found here much interesting
    and useful

  7. illuldese says:

    Hello! Excellent site! Searched a long ago

  8. eagepeduple says:

    Hello! Good site, simply super!

  9. Hello, excellent site, very rich in content and correctly carefully thought out

  10. Geafrorgo says:

    The good convenient site is made.

Leave a Reply

 
preload preload preload