6 Replies Latest reply on Feb 16, 2011 6:11 PM by oldMster

    Reading a datagrid after a drag'n drop

    d3mac123

      I have a datagrid (from an Array();) that allows users to drag and drop elements inside (users can move elements from 2 row to 1, etc). After the repositioning of elements into the datagrid, I need to re-read it in order to save the new element positioning. I tried to read it thru the data provider and (obviously) I keep receiving the original position of each element in the array, not in the grid.

       

      Any ideas on how to solve it?

      thanks!

      Alex

        • 1. Re: Reading a datagrid after a drag'n drop
          oldMster Level 3

          Data binding for datagrids is one way - updates to the dataprovider will update the datagrid, but updates to the datagrid will not automatically flow back to the dataprovider.  Your drag/drop event handler will need to update the dataprovider.

          • 2. Re: Reading a datagrid after a drag'n drop
            miguel8312 Level 3
            IViewCursor
            
            
            <mx:Script>
                <![CDATA[
                    import mx.events.ListEvent;
                    private function itemClickEvent(event:ListEvent):void {
                        clickColumn.text=String(event.columnIndex);
                        clickRow.text=String(event.rowIndex);
                        eventType.text=event.type;
                    }
                ]]>
                </mx:Script>
            
            
            
            
             <mx:DataGrid id="myGrid" width="350" height="150"
                        itemClick="itemClickEvent(event);">
            

            Umm im not sure if this would work but you might try doing something like this.

            you would problably look into using IViewCursor and also kickin off a list function  on the end of drag to add the item to the dataprovider. the example above is taken from the docs obviously you would want to change your code to add to the provider.

            • 3. Re: Reading a datagrid after a drag'n drop
              d3mac123 Level 1

              oldMaster,

               

              thanks for your return. I know my handler needs to solve this. my challenge

              is to discover how to read each row from the grid, in order to update the

              array with the new info. do you know how?

              • 4. Re: Reading a datagrid after a drag'n drop
                oldMster Level 3

                asouza,

                Actually it's oldMumpster, but that's a whole different discussion.

                 

                I have reviewed some code I have that this is working in, and in all cases I used an Array Collection rather than an Array as the dataprovider.  To my surprise, if I drag and drop an item within the datagrid to change the order, it just seems to work, despite the one way binding.  My dragDrop event handler doesn't do anything in this case except turn 'On' the save button.

                 

                The code for my dragDrop handler is below.  In this example, which is a menu maintenance function, I have 3 datagrids, the drag target is a menu segment (dgTarget), the drag sources can be the menu segment  (id = dgTarget) itself.  If this is the case, the user is changing the order of the items in the menu segment.  Antoher datagrid is a list of other menu segments (so the menu segment can call another menu segment) with id = dgSourceMenu, and the third is a datagrid containing items (menu action items) with id =  dgSourceAction

                 

                public function itemDragDrop(event:DragEvent):void {

                 

                var itemsArray:Array = event.dragSource.dataForFormat('items') as Array;

                if (itemsArray[0].Privilege=="NONE"){

                Alert.show("You not authorized to use this item")

                dgTarget.invalidateList();

                dataItems.refresh();

                event.preventDefault();

                return;

                }

                 

                if (event.dragInitiator==dgTarget) {

                if (event.currentTarget==dgTarget) {

                btnSave.enabled=true;

                return;

                }

                else {

                return;

                }

                }

                 

                var tempobj:Object = new Object();

                var dropTarget:DataGrid = DataGrid(event.currentTarget);

                var tempstring:String;

                 

                if (event.dragInitiator==dgSourceMenu) {

                event.preventDefault();

                tempobj.Type="Menu";

                tempstring=itemsArray[0].MenuId;

                tempobj.ItemId=tempstring;

                tempstring=itemsArray[0].MenuCode;

                tempobj.ItemCode=tempstring;

                tempstring=itemsArray[0].Description;

                tempobj.ItemName=tempstring;

                tempstring=itemsArray[0].ImagePath;

                tempobj.ImagePath=tempstring;

                }

                 

                if (event.dragInitiator==dgSourceAction) {

                event.preventDefault();

                tempobj.Type="Action";

                tempstring=itemsArray[0].ActionId;

                tempobj.ItemId=tempstring;

                tempstring=itemsArray[0].ActionCode

                tempobj.ItemCode=tempstring;

                tempstring=itemsArray[0].ActionName;

                tempobj.ItemName=tempstring;

                tempstring=itemsArray[0].ImagePath;

                tempobj.ImagePath=tempstring;

                 

                }

                 

                if (tempobj != null) {

                var dropLoc:int = dropTarget.calculateDropIndex(event);

                IList(dropTarget.dataProvider).addItemAt(tempobj,dropLoc);

                btnSave.enabled=true;

                }

                return;

                }

                 

                • 5. Re: Reading a datagrid after a drag'n drop
                  d3mac123 Level 1

                  oldMumpster (sorry for the first time),

                   

                  I found my issue. I was using dragDrop handler instead dragComplete. Now

                  it is working perfectly! Thanks again!

                  • 6. Re: Reading a datagrid after a drag'n drop
                    oldMster Level 3

                    Asouza,

                    Glad you found your problem!  No worries on the screen name - I just didn't want people to think I was arrogant enough to call myself a master!

                    Mark