flex datagrids with drag/drop enabled

I have two flex datagrids with drag/drop enabled. There are two buttons to handle moving the items between the datagrids one by one. I would like to enable moving multiple items using same buttons handler. Please advice


Thanks.


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp()">
      <mx:Script>
            <![CDATA[
                  import mx.collections.ArrayCollection;
                  
                  [Bindable]
                  private var DestData:ArrayCollection = new ArrayCollection();

                  private function initApp():void
                  {
                        SourceDG.dataProvider = SourceData;
                  }


                  private function moveRightHandler():void
                  {
                           DestDG.dataProvider.addItem(SourceDG.selectedItem);
                           SourceDG.dataProvider.removeItemAt(SourceDG.selectedIndex)
                  }


                  private function removeHandler():void
                  {
                           SourceDG.dataProvider.addItem(DestDG.selectedItem);
                           DestDG.dataProvider.removeItemAt(DestDG.selectedIndex)                         
                  }
            ]]>
      </mx:Script>

      <mx:ArrayCollection id="SourceData">
         <mx:Object>
            <mx:ItemID>1</mx:ItemID>
            <mx:ItemTitle>Title1</mx:ItemTitle>
         </mx:Object>
         
         <mx:Object>
            <mx:ItemID>2</mx:ItemID>
            <mx:ItemTitle>Title2</mx:ItemTitle>
         </mx:Object>
         
         <mx:Object>
            <mx:ItemID>3</mx:ItemID>
            <mx:ItemTitle>Title3</mx:ItemTitle>
         </mx:Object>
         
         <mx:Object>
            <mx:ItemID>4</mx:ItemID>
            <mx:ItemTitle>Title4</mx:ItemTitle>
         </mx:Object>
                 
         <mx:Object>
            <mx:ItemID>5</mx:ItemID>
            <mx:ItemTitle>Title5</mx:ItemTitle>
         </mx:Object>
                                                   
           <mx:Object>
            <mx:ItemID>6</mx:ItemID>
            <mx:ItemTitle>Title6</mx:ItemTitle>
         </mx:Object>
         
            <mx:Object>
            <mx:ItemID>7</mx:ItemID>
            <mx:ItemTitle>Title7</mx:ItemTitle>
         </mx:Object>
                                 
           <mx:Object>
            <mx:ItemID>9</mx:ItemID>
            <mx:ItemTitle>Title9</mx:ItemTitle>
         </mx:Object>  
     
           <mx:Object>
            <mx:ItemID>10</mx:ItemID>
            <mx:ItemTitle>Title10</mx:ItemTitle>
         </mx:Object>
               
      </mx:ArrayCollection>
     
      <mx:HBox width="100%"  horizontalAlign="center">

            <mx:DataGrid id="SourceDG"
                               height="280"
                               width="300"
                               y="20"
                               allowMultipleSelection="true"
                               allowDragSelection="true"
                               doubleClickEnabled="true"
                               doubleClick="moveRightHandler()"
                               dragEnabled="true"
                               dropEnabled="true"
                               dragMoveEnabled="true">
                  <mx:columns>
                        <mx:DataGridColumn headerText="Item ID" width="100" dataField="ItemID"/>
                        <mx:DataGridColumn headerText="Item Title" dataField="ItemTitle"/>
                  </mx:columns>
            </mx:DataGrid>
            <mx:VBox height="100%" verticalAlign="middle">
                  <mx:Button id="moveRightBtn"
                                 label=" To Right "
                                 click="moveRightHandler()"
                                 width="80"
                                />
                  <mx:Button id="btnRemove"
                                 label=" To Left "
                                 click="removeHandler()"
                                 width="80"
                                 />
            </mx:VBox>
            <mx:DataGrid id="DestDG"  dataProvider="{DestData}"
                               height="280"
                               width="300"
                               doubleClickEnabled="true"
                               doubleClick="removeHandler()"
                               dragEnabled="true"
                               dropEnabled="true"
                               dragMoveEnabled="true">
                  <mx:columns>
                        <mx:DataGridColumn headerText="Item ID" width="100" dataField="ItemID"/>
                        <mx:DataGridColumn headerText="Item Title" dataField="ItemTitle"/>
                  </mx:columns>
            </mx:DataGrid>


      </mx:HBox>


</mx:Application>

LVL 1
myBlueSkyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vindys80Commented:
Try this and see if this is the one you looking for. May be you can do some optimizations.

Thanks,
Vindys
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp()">
      <mx:Script>
            <![CDATA[
                  import mx.collections.ArrayCollection;
                  
                  [Bindable]
                  private var DestData:ArrayCollection = new ArrayCollection();

                  private function initApp():void
                  {
                        SourceDG.dataProvider = SourceData;
                  }


                  private function moveRightHandler():void
                  {
                  	var selectedArr:Array = SourceDG.selectedItems;
                  	var selectedIndices:Array = SourceDG.selectedIndices;
                  	selectedIndices.sort(Array.NUMERIC | Array.DESCENDING );
                  	for( var i:int = 0; i < selectedArr.length; i++ ){
                  		SourceDG.dataProvider.removeItemAt(selectedIndices[i])
                  		DestDG.dataProvider.addItem(selectedArr[i]); 
                  	}
                  }


                  private function removeHandler():void
                  {
                  	var selectedArr:Array = DestDG.selectedItems;
                  	var selectedIndices:Array = DestDG.selectedIndices;
                  	selectedIndices.sort(Array.NUMERIC | Array.DESCENDING );
                  	for( var i:int = 0; i < selectedArr.length; i++ ){
                  		DestDG.dataProvider.removeItemAt(selectedIndices[i])
                  		SourceDG.dataProvider.addItem(selectedArr[i]); 
                  	}
                  }
            ]]>
      </mx:Script>

      <mx:ArrayCollection id="SourceData">
         <mx:Object>
            <mx:ItemID>1</mx:ItemID>
            <mx:ItemTitle>Title1</mx:ItemTitle>
         </mx:Object>
         
         <mx:Object>
            <mx:ItemID>2</mx:ItemID>
            <mx:ItemTitle>Title2</mx:ItemTitle>
         </mx:Object>
         
         <mx:Object>
            <mx:ItemID>3</mx:ItemID>
            <mx:ItemTitle>Title3</mx:ItemTitle>
         </mx:Object>
         
         <mx:Object>
            <mx:ItemID>4</mx:ItemID>
            <mx:ItemTitle>Title4</mx:ItemTitle>
         </mx:Object>
                  
         <mx:Object>
            <mx:ItemID>5</mx:ItemID>
            <mx:ItemTitle>Title5</mx:ItemTitle>
         </mx:Object>
                                                    
           <mx:Object>
            <mx:ItemID>6</mx:ItemID>
            <mx:ItemTitle>Title6</mx:ItemTitle>
         </mx:Object>
         
            <mx:Object>
            <mx:ItemID>7</mx:ItemID>
            <mx:ItemTitle>Title7</mx:ItemTitle>
         </mx:Object>
                                 
           <mx:Object>
            <mx:ItemID>9</mx:ItemID>
            <mx:ItemTitle>Title9</mx:ItemTitle>
         </mx:Object>   
      
           <mx:Object>
            <mx:ItemID>10</mx:ItemID>
            <mx:ItemTitle>Title10</mx:ItemTitle>
         </mx:Object>
               
      </mx:ArrayCollection>
      
      <mx:HBox width="100%"  horizontalAlign="center">

            <mx:DataGrid id="SourceDG"
                               height="280"
                               width="300"
                               y="20"
                               allowMultipleSelection="true"
                               allowDragSelection="true"
                               doubleClickEnabled="true"
                               doubleClick="moveRightHandler()"
                               dragEnabled="true"
                               dropEnabled="true"
                               dragMoveEnabled="true">
                  <mx:columns>
                        <mx:DataGridColumn headerText="Item ID" width="100" dataField="ItemID"/>
                        <mx:DataGridColumn headerText="Item Title" dataField="ItemTitle"/>
                  </mx:columns>
            </mx:DataGrid>
            <mx:VBox height="100%" verticalAlign="middle">
                  <mx:Button id="moveRightBtn"
                                 label=" To Right "
                                 click="moveRightHandler()"
                                 width="80"
                                />
                  <mx:Button id="btnRemove"
                                 label=" To Left "
                                 click="removeHandler()"
                                 width="80"
                                 />
            </mx:VBox>
            <mx:DataGrid id="DestDG"  dataProvider="{DestData}"
                               height="280"
                               width="300" allowMultipleSelection="true"
                               doubleClickEnabled="true"
                               doubleClick="removeHandler()"
                               dragEnabled="true"
                               dropEnabled="true"
                               dragMoveEnabled="true">
                  <mx:columns>
                        <mx:DataGridColumn headerText="Item ID" width="100" dataField="ItemID"/>
                        <mx:DataGridColumn headerText="Item Title" dataField="ItemTitle"/>
                  </mx:columns>
            </mx:DataGrid>


      </mx:HBox>


</mx:Application>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
halfbloodprinceCommented:
0
myBlueSkyAuthor Commented:
Thanks vindys for the solution
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Flex

From novice to tech pro — start learning today.