Skip navigation
Gug88
Currently Being Moderated

DataGrid Moving rows up and down

Mar 14, 2013 2:01 AM

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

        }

 
Replies
  • Currently Being Moderated
    Mar 14, 2013 6:25 AM   in reply to Gug88

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 15, 2013 10:59 AM   in reply to Gug88

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

    Or, do you have CUT and PASTE buttons?

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 15, 2013 12:49 PM   in reply to Gug88

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points