Wednesday, February 18, 2009

Flex Advanced Data Grid remove item

This shouldn't have taken me this long to figure this out... it is really quite simple actually.

I have an AdvancedDataGrid with a Hierarchical data provider. I have a delete button. The behavior I want is when the user clicks the delete button whatever row is highlighted (selected) in the ADG is removed.

To do this you need to use two methods.

The first is the removeChild method. Use this on the dataprovider of the ADG. It takes two arguments, the parent node, and the object to be removed. IF you're object is at the top mode, pass null for the parent node argument.

removeChild(parent,object)

The second is the getParentItem method. This method, you guessed it, returns the parent of the item passed to it.

getParentItem(Object);

Use them together to remove the selected item in your ADG.

myADG.dataProvider.removeChild( myADG.dataProvider.getParentItem(myADG.selectedItem), myADG.selectedItem );

Note, make sure your data provider is bound to the ADG.

8 comments:

Anonymous said...

thanks so much! your post is very helpful!

Al said...

Thank you, this was interesting.

Here's how I arranged for the Delete key to remove the selected row when the data provider is an ArrayCollection.

private function deleteSelected():void
{
var index:Number = planADG.selectedIndex;
var planACIndex:int = planAC.getItemIndex(planADG.selectedItem);
if(index >= 0){
if (planACIndex < 0){
// we got a parameter, so do nothing
// (My nodes represent functions, some of which
// have parameters. I don't want the user to be able
// to delete a parameter.
return;
}
planAC.removeItemAt(planACIndex);
if(index > 0 && planAC.length >= 1)
planADG.selectedIndex = (index - 1);
else if(index == 0 && planAC.length >=1)
callLater(selectFirst);
}
}

Al said...

Just to clarify, some of rows in the ADG represent functions; they can be expanded to show additional rows representing function parameters. Attempting to delete a parameter row must do nothing (but attempting to delete a leaf function row must succeed).

Anonymous said...

Thanks! Just what i needed

Anonymous said...

good work, really nice....

Anonymous said...

Great work, You've saved the day for me

MissLulu said...

thank you so much ..!!

santosh lodhi said...

Thank you, your's post very helpful