apache-flexdrag-and-drophierarchical-dataadvanceddatagrid

Flex Advanced Data Grid w/ hierarchical data: How to access currentTarget fields on dragdrop event?


So this is driving me crazy. I've got an advanced data grid with a dataprovider that's an array collection w/ hierarchical data. Each object (including the children) has an id field. I'm trying to drag and drop data from within the ADG. When this happens, I need to grab the id off the drop target and change the dragged object's parentid field. Here's what I've got:

public function topAccountsGrid_dragDropHandler(event:DragEvent):void{  
    //In this function, you need to make the move, update the field in salesforce, and refresh the salesforce data...
    if(checkActivateAccountManageMode.selected == true) {
        var dragObj:Array = event.dragSource.dataForFormat("treeDataGridItems") as Array;
        var newParentId:String = event.currentTarget['Id'];
        dragObj[0].ParentId = newParentId;
    } else {
        return;
    }
    app.wrapper.save(dragObj[0], 
        new mx.rpc.Responder(
            function():void {
            refreshData();
        },
        function():void{_status = "apex error!";}
        )
    );
}

I can access the data I'm draggin (hence changing parentId) but not the currentTarget. I think the hierarchical data is part of the problem, but I can't find much in the documentation? Any thoughts?


Solution

  • event.currentTarget is not a node, it's the ADG itself. However, it's quite easy to get the information you want, since the ADG stores that data internally (as mx_internal).

    I'm using the following code snippets (tested with Flex SDK 4.1) in a dragOver handler, but I guess it will work in a dragDrop handler too.

    protected function myGrid_dragDropHandler(event:DragEvent):void
    {
        // Get the dragged items. This could either be an Array, a Vector or NULL.
        var draggedItems:Object = getDraggedItems(event.dragSource);
    
        if (!draggedItems)
            return;
    
        // That's our ADG where the event handler is registered.
        var dropTarget:AdvancedDataGrid = AdvancedDataGrid(event.currentTarget);
    
        // Get the internal information about the dropTarget from the ADG.
        var dropData:Object = mx_internal::dropTarget._dropData;
    
        // In case the dataProvider is hierarchical, get the internal hierarchicalData aka rootModel.
        var hierarchicalData:IHierarchicalData = dropTarget.mx_internal::_rootModel;
        var targetParent:Object = null;
    
        // If it's a hierarchical data structure and the dropData could be retrieved
        // then get the parent node to which the draggedItems are going to be added.
        if (hierarchicalData && dropData)
            targetParent = dropData.parent;
    
        for each (var draggedItem:Object in draggedItems)
        {
            // do something with the draggedItem
        }
    }
    
    protected function getDraggedItems(dragSource:DragSource):Object
    {
        if (dragSource.hasFormat("treeDataGridItems"))
            return dragSource.dataForFormat("treeDataGridItems") as Array;
    
        if (dragSource.hasFormat("items"))
            return dragSource.dataForFormat("items") as Array;
    
        if (dragSource.hasFormat("itemsByIndex"))
            return dragSource.dataForFormat("itemsByIndex") as Vector.<Object>;
    
        return null;
    }