5 Replies Latest reply: Mar 15, 2013 12:49 PM by DeanLoganBH RSS

    DataGrid Moving rows up and down

    Gug88

      I have this function to move rows up in a datagrid , yet i am having problems when it comes to move to rows down. I have tried changing the following line to +1 but it dint work any hints please.

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

       

       

      This is the code

       

       

      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();

              }

        • 1. Re: DataGrid Moving rows up and down
          DeanLoganBH Community Member

          Try this.

           

          Get the item you are moving; "MoveObject" and the index "MoveObjectIndex"

          Get the object of the item you are moving the new item after;  "ObjectBefore"

          Then remove the old item. "MoveObjectIndex"

          Refresh the ArrayCollection

          Find the index of the "ObjectBefore"; "ObjectBeforeIndex"

          Then insert the "MoveObject" to the "ObjectBeforeIndex" +1

          • 2. Re: DataGrid Moving rows up and down
            Gug88 Community Member

            Thanks for your help! Unfortunately it dint work out as expected     ( i probably have something wrong.Can you please give me a helping hand !Thanks for your help

             

             


            public function move_down():void



            {














            if(ProssGrid.selectedIndex == 0)return;









            var MoveObjectIndex : Number = dg.selectedIndex;




            var MoveObject : Object = ac.getItemAt(MoveObjectIndex) as Object;









            var ObjectBefore : Object = ac.getItemAt(MoveObjectIndex+1) as Object;









            ac.removeItemAt(dg.selectedIndex);









            dg.invalidateDisplayList();









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











            }
            • 3. Re: DataGrid Moving rows up and down
              DeanLoganBH Community Member

              Do you have UP and DOWN arrow buttons for moving the item?

              Or, do you have CUT and PASTE buttons?

              • 4. Re: DataGrid Moving rows up and down
                Gug88 Community Member

                I have Up and down normal buttons... (s.button)

                 

                I have created the button and on click it triggers the function move_down() or move_up() for the up button..

                It is working for the move up but not working for the move down..

                 

                any ideas thanks

                • 5. Re: DataGrid Moving rows up and down
                  DeanLoganBH Community Member

                  Okay.  So, I made the code a little simpler than what I said. With my code, you could have selected a row, clicked a cut button, then selected a another row to past afterwards or whatever.  Here is the code for simply moving the row up and down.

                   

                  I added code to catch if the row was at the top or the bottom of the data.  Also, I created two indexes, one for the grid and one for the array collection.  It will probably work with the one index, but I was trying to see if I could do the move on a sorted datagrid, but that is basically pointless.

                   

                  protected function moveUpBtn_clickHandler(event:MouseEvent):void
                  {
                   var upSelectedIndex:int = myDataGrid.selectedIndex;
                   
                   // Move Item
                   if(upSelectedIndex > -1) {
                    if(upSelectedIndex > 0) {
                     // Get the selected Object 
                     var moveObject:Object = myArrayCollection.getItemAt(upSelectedIndex);
                     // Get actual Object location if grid is sorted
                     var upActualIndex:int = myArrayCollection.getItemIndex(moveObject);
                     // Remove selected item at actualIndex
                     myArrayCollection.removeItemAt(upActualIndex);
                     myArrayCollection.refresh();
                     // Move both indexes
                     upActualIndex--;
                     upSelectedIndex--;
                     // insert item at actualIndex
                     myDataGrid.dataProvider.addItemAt(moveObject, upActualIndex);
                     myArrayCollection.refresh();
                     // select grid at selectedIndex
                     myDataGrid.selectedIndex = upSelectedIndex;
                    } else {
                     Alert.show("Item is currently at the top of the list.", "Move Alert",  Alert.OK, this);
                    }
                   } else {
                    Alert.show("No Item Selected.", "Move Alert",  Alert.OK, this);
                   }
                   
                  }
                  protected function moveDownBtn_clickHandler(event:MouseEvent):void
                  {
                   var downSelectedIndex:int = myDataGrid.selectedIndex;
                   
                   if(downSelectedIndex > -1) { 
                    // Move Item
                    if(downSelectedIndex < myArrayCollection.length -1) {
                     // Get the selected Object 
                     var moveObject:Object = myArrayCollection.getItemAt(downSelectedIndex);
                     // Get actual Object location if grid is sorted
                     var downActualIndex:int = myArrayCollection.getItemIndex(moveObject);
                     // Remove selected item at actualIndex
                     myArrayCollection.removeItemAt(downActualIndex);
                     myArrayCollection.refresh();
                     // Move both indexes
                     downActualIndex++;
                     downSelectedIndex++;
                     // insert item at actualIndex
                     myDataGrid.dataProvider.addItemAt(moveObject, downActualIndex);
                     myArrayCollection.refresh();
                     // select grid at selectedIndex
                     myDataGrid.selectedIndex = downSelectedIndex;
                    } else {
                     Alert.show("Item is currently at the bottom of the list.", "Move Alert",  Alert.OK, this);
                    } 
                   } else {
                    Alert.show("No Item Selected.", "Move Alert",  Alert.OK, this);
                   }
                   
                  }
                  
                  

                   

                  Here is the blog post with the working DataGrid and Source View

                   

                  Message was edited by: DeanLoganBH  - Had to set the Down Move to look for the Length of the ArrayCollection -1, because the DataGrid row starts at 0, it is one less than the total length of the ArrayCollection.