tags:

views:

1505

answers:

3

I've got a subclass of AdvancedDataGrid showing a tree-like data structure. How can I, given the index returned by calculateDropIndex, get the item at that index?

After reading through reams of code, it seems like the least terrible way is:

var oldSelectedIndex:int = this.selectedIndex;
var mouseOverIndex:int = this.calculateDropIndex(event);
this.selectedItem = mouseOverIndex;
var item:* = this.selectedItem;
this.selectedIndex = oldSelectedIndex;

The other option seems to be tinkering around with the iterator property... But, judging by the way I've seen it used, that will get pretty harry pretty quickly too.

So, how can I get the item at a particular index in an advanced datagrid without going insane?

A: 
this.dataProvider.getItemAt(calculateDropIndex(event));

Looking through the docs, it seems you may be able to use openNodes, which returns an Array of all open nodes, which should correspond with your index?

this.dataProvider.openNodes[calculateDropIndex(event)];
CookieOfFortune
Sorry, but this doesn't work on hierarchical data. `this.dataProvider` is an instance of 'HierarchicalCollectionView', which doesn't have a 'getItemAt' method.
David Wolever
Is the index returned by calculateDropIndex the flattened index of the item? In that case, I suppose you would have to count which child it's pointing to... which I suppose isn't less hairy than what you're suggesting...
CookieOfFortune
maybe try openNodes.
CookieOfFortune
A: 

How are you putting the data into your HierarchicalCollectionView ? It's probably still an ArrayCollection, no? i.e. this.dataProvider.source.source. is probably still an ArrayCollection, on which you can use getItemAt.

Dan
I should clarify- this answer assumes knowledge from the previous answer- that if this.dataProvider is a HierarchicalCollectionView, you can use this.dataProvider.source.source to get the ArrayCollection, on which you can use getItemAt
Dan
Thanks for the answer… But unfortunately it's a tree data structure (eg, { name: "foo", children: [ { name: "bar" }, { name: "baz" } ]}), so traversing it (taking into consideration which nodes are "open" and "closed") would not be trivial.
David Wolever
I'm with you. I found this thread because I wanted to get the selected Row, given the index provided by 'selectedCells'. This index doesn't work with Hierarchical data since if you expand the children in the grid, the index is thrown off.I was able to get the correct selected row using this, hopefully it will help you too:var cursor:IViewCursor = myADG.dataProvider.createCursor();cursor.seek(CursorBookmark.FIRST, myADG.selectedCells[0]["rowIndex"]);var row:Object = cursor.current;
Dan
+1  A: 

You could try:

// Get the dropIndex from the dragEvent
var index:int = this.calculateDropIndex(event);
// Get the itemRenderer from the index
var renderer:IListItemRenderer = this.indexToItemRenderer(index);
// Get your item from the data property of the itemRenderer
var item:Object = renderer.data;
DenisH