Avg. Rating 4.0

Problem

In my current project, I use Context (right-click) Menus on several components. I was able to implement selecting the item under point in List-based components (DataGrid, Tree), but I wanted this functionality in AdvancedDataGrid as well.

Solution

I override the mouseOverHandler() method of the AdvancedDataGrid, and store the itemRenderer that the mouse is over in a private variable. When the user right clicks, I catch this with a ContextMenuEvent handler, using the itemRenderer under point to get that items index, and then setting the selectedIndex property to that value.

Detailed explanation

I override the mouseOverHandler() method of the AdvancedDataGrid, and store the itemRenderer that the mouse is over in a private variable. When the user right clicks, I catch this with a ContextMenuEvent handler, using the itemRenderer under point to get that items index, and then setting the selectedIndex property to that value:

<?xml version="1.0" encoding="utf-8"?>
<mx:AdvancedDataGrid xmlns:mx="http://www.adobe.com/2006/mxml"
    defaultLeafIcon="{null}" folderOpenIcon="{null}" folderClosedIcon="{null}" 
    allowMultipleSelection="true" initialize="initializeHandler();">
    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.controls.listClasses.IListItemRenderer;
            
            [Bindable]
            private var dataCollection:ArrayCollection;
            
            private var itemRendererUnderPoint:IListItemRenderer;
            
            override protected function mouseOverHandler(
                event:MouseEvent):void
            {
                itemRendererUnderPoint = super.mouseEventToItemRenderer(event);
                
                // Don't show rollOver effect for Header Items
                if (super.isHeaderItemRenderer(itemRendererUnderPoint))
                    return;
                
                super.mouseOverHandler(event);
            }
            
            private function initializeHandler():void
            {
                var dataArray:Array = 
                    [
                        {label: "C", displayCode: 1}, 
                        {label: "G", displayCode: 2}, 
                        {label: "E", displayCode: 1}, 
                        {label: "Q", displayCode: 2}, 
                        {label: "T", displayCode: 1}, 
                    ];
                
                dataCollection = new ArrayCollection(dataArray);
                
                createContextMenu();
            }
            
            private function createContextMenu():void
            {
                contextMenu = new ContextMenu();
                
                contextMenu.hideBuiltInItems();
                
                contextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, 
                                                                contextMenu_menuSelectHandler);
            }
            
            private function contextMenu_menuSelectHandler(
                event:ContextMenuEvent):void
            {
                if (itemRendererUnderPoint != null)
                {
                    var rightClickIndex:int = 
                        super.itemRendererToIndex(itemRendererUnderPoint);
                    
                    if (selectedIndex != rightClickIndex)
                        selectedIndex = rightClickIndex;
                }
            }
        ]]>
    </mx:Script>
    
    <mx:dataProvider>
        <mx:HierarchicalData source="{dataCollection}" />
    </mx:dataProvider>
</mx:AdvancedDataGrid>
Report abuse

Related recipes