views:

20

answers:

2

I have a DataGrid with a custom itemRenderer(Canvas) which has a context menu on its right click. I am trying to get the data of the itemRenderer.

I tried to find something in event & variables. I also tried with FlexNativeMenu on RIGHT_MOUSE_CLICK. But I didn't find any way out.

Please help me in getting the data of the itemrenderer or its instance.

+1  A: 

contextMenuOwner property of the ContextMenuEvent dispatched by the ContextMenu would point to the itemRenderer of interest.

var renderer:Canvas = Canvas(event.contextMenuOwner);
trace(renderer.data);
trace(renderer.data.something);
Amarghosh
+1  A: 

Use ContextMenuEvent.mouseTarget:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" initialize="init();">

    <fx:Script>
    <![CDATA[
        import mx.core.IDataRenderer;

        private function init():void
        {
            var dataGridContextMenu:ContextMenu = new ContextMenu();
            dataGridContextMenu.hideBuiltInItems();
            dataGridContextMenu.addEventListener(ContextMenuEvent.MENU_SELECT,
                menuSelectHandler);
            dataGrid.contextMenu = dataGridContextMenu;
        }

        private function menuSelectHandler(event:ContextMenuEvent):void
        {
            var displayObject:DisplayObject = event.mouseTarget as DisplayObject;
            while (!(displayObject is IDataRenderer) && !(displayObject == dataGrid))
            {
                displayObject = displayObject.parent;
            }
            var data:Object;
            if (displayObject is IDataRenderer)
                data = IDataRenderer(displayObject).data;

            var customItems:Array = [];
            if (data)
            {
                var contextMenuItem:ContextMenuItem = new ContextMenuItem(data.name, false, false);
                customItems.push(contextMenuItem);
            }

            var menu:ContextMenu = ContextMenu(event.target);
            menu.customItems = customItems;
        }

    ]]>
    </fx:Script>

    <mx:DataGrid id="dataGrid">
        <mx:dataProvider>
            <s:ArrayCollection>
                <fx:Object name="Mike" age="21"/>
                <fx:Object name="Juss" age="19"/>
            </s:ArrayCollection>
        </mx:dataProvider>
    </mx:DataGrid>

</s:Application>
Maxim Kachurovskiy