Actionscript – Flex DataGrid: Get Data Object from row index & vice versa

actionscriptapache-flexdatagriddataprovider

Using a standard Flex DataGrid, is there an easy way to determine the grid row index given the data object or index of the data object in the data provider? On the flip side, is there a way to get the index of an object in the data provider based on the DataGrid row index? Thanks in advance!

Best Answer

Assuming I understand what you're asking for correctly and assuming your dataProvider is an ArrayCollection:

To get the selected row's index in your dataProvider:

var rowIndex:int = myArrayCollection.getItemIndex(myDataGrid.selectedItem);

To get the object in your dataProvider from the DataGrid:

var obj:Object = myDataGrid.selectedItem;

Since DataGrid's recycle their itemRenderers to improve memory performance, there's no specific row index for a given object in your dataProvider. As you scroll and records are no longer visible, those records itemRenderers are reused for new records that scroll into view. You can read up more on itemRenderers and recycling here.

EDIT: Here's a link to an example on how to filter your data in a dataGrid:

http://www.flex-blog.com/arraycollection-filter-example/

Related Topic