3 Replies Latest reply on Mar 13, 2013 10:03 AM by Gug88

    Moving datagrid rows up and down

    iamcootis Level 1

      I have a datagrid and I want to be able to move one row at a time, up or down. What would be the best way to make this happen? Is there a built in function for this?

       

      I currently want to be able to select a row and then push an up or down button to move it. Could I just take the dataProvider and get the items current index and do an addItemAt(new index);

       

      Thanks for your help.

        • 1. Re: Moving datagrid rows up and down
          VRPDeveloper Level 3

          There is no built in function for this... Make copy of your data provider You have to fetch row number and then copy the new dataprovider till that row number and then change the data... Do not forget to refresh data provider,,

           

           

          Hope you got my point!!!

           

          If this post answers your question or helps, please kindly mark it as such.

          • 2. Re: Moving datagrid rows up and down
            ShardulSingh

            Hi,

             

             

            I have written this code simply for the up. You can write similarly for the down. Pls check this code.Let  me know if you have any issue.

             

            MainApplication.mxml

             

            <?xml version="1.0"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >
            <mx:Script>
                <![CDATA[
                    import mx.controls.Alert;
                    import mx.collections.ArrayCollection;
                   
                    [Bindable]
                    private var ac : ArrayCollection = new ArrayCollection([
                    {name : 'Smith',age : '45'},
                    {name : 'Jake',age : '44'},
                    {name : 'Carls',age : '43'},
                    {name : 'Robert',age : '33'},
                   
                    ])
                   
                    public function up():void
                    {
                        if(dg.selectedIndex == -1)
                        {
                            Alert.show("Select the row which you want to up.");
                            return;
                        }
                       
                        if(dg.selectedIndex == 0)return;
                       
                        var selectedRowInx : Number = dg.selectedIndex;
                       
                        var itemToShift : Object = ac.getItemAt(selectedRowInx) as Object;
                       
                        ac.addItemAt(itemToShift,dg.selectedIndex-1);
                        ac.removeItemAt(dg.selectedIndex);
                        dg.invalidateDisplayList();
                    }
                ]]>
            </mx:Script>

             


               <mx:DataGrid id="dg" width="350" dataProvider="{ac}">
                  <mx:columns>
                     <mx:DataGridColumn dataField="name" />
                     <mx:DataGridColumn dataField="age" />
                  </mx:columns>
               </mx:DataGrid>

             

            <mx:Button label="Up the Row" click="up()"/>

             

            </mx:Application>

            • 3. Re: Moving datagrid rows up and down
              Gug88 Level 1

              Hey i know this is an old post but i was using it and was having some problems when it comes to move down ...

               

              Wouldn't the code be similar just having a difference in this line..

               

              ac.addItemAt(itemToShift,dg.selectedIndex+1);

               

              Cheers can somebody confirm please(cause its not working for me

               




              Thanks