How do you find an item's dataProvider index from inside an itemRenderer?

The List based classes of Flex all allow you to implement your own custom itemRenderer.

I was working on one of the DotComIt Flex Components. The component is a DataSorter component and allows for easy sorting of data in a manner similar to a Netflix movie queue or YouTube playlists. This component includes buttons for "move to top," "move to bottom," "move up", and move down.

For the first item in the dataProvider, there is no 'up' to move the item to. For the last item in the dataProvider, there is no down to move the items too. If we are displaying the first, or last item in the dataProvider wanted to make the relevant buttons invisible. To do that I needed to know the index location of my data in the dataProvider.

First I looked at the rowIndex in the listData. Unfortunately that display the index of the renderer being displayed, not the index of the item in the dataProvider. I settled on this approach:


ListCollectionView(this.listOwner.dataProvider).getItemIndex(this.data)

It appears to be working great for me. Is there an easier way?

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Derek Wischusen's Gravatar I am sure it does basically the same thing, but you can use:

itemRendererToIndex(itemRenderer:IListItemRenderer):int

Derek
# Posted By Derek Wischusen | 11/4/08 11:44 AM
Jeffry Houser's Gravatar Derek,

I had no idea that was there. I guess you learn something new every day. I'm not sure that the getItemIndex does under the hood, but itemRendererToIndex uses the itemRenderer.name and looks for it in a rowMap.

In my travels I haven't needed to 'touch' the rowMap yet. I wonder how itemRenderers are named.

Cool; thanks for the tip!
# Posted By Jeffry Houser | 11/4/08 4:08 PM
Jeffry Houser's Gravatar An answer to this came up on the Flex Coder's list; and here is the answer from the horses mouth, so to speak:

If you implement IDropInListItemRenderer, then listData.rowIndex + owner.verticalScrollPosition should be your position

Source: http://tech.groups.yahoo.com/group/flexcoders/mess...
# Posted By Jeffry Houser | 2/27/09 5:03 PM
sarah's Gravatar Whereabouts did you put this line of code?
I have a customitemrenderer but it is defined in a seperate mxml file. I have a horizontal list that uses the custom itemrenderer. I want to display the a button for each item in the horizontal list as a number so item 1 would have the button label 1, item 2 would have the button label 2 and so on.
can anybody help me? I'm pretty new to this.
# Posted By sarah | 8/17/09 1:42 AM
Jeffry Houser's Gravatar Sarah,

The line of code would need to be in your renderer class.

I would listen to the dataChange event and change the button's label in the dataChangeHandler/
# Posted By Jeffry Houser | 8/21/09 2:40 PM
All Content Copyright 2005, 2006, 2007, 2008, 2009 Jeffry Houser. May not be reused without permission
BlogCFC was created by Raymond Camden. This blog is running version 5.9.2.002.