Home > Flex 4 > Displaying the row number or index in a spark List

Displaying the row number or index in a spark List

The ItemRenderer class in Flex 4 has an itemIndex property that was added after beta2. You can use this property within your item renderer to display the row number or index of an item in a spark List.

The following example demonstrates a List with a custom item renderer that uses this itemIndex property. If you look at the commented out code in the item renderer you will also see a way of getting this information using the getItemIndex() method of the dataProvider.

View Source

Note: This sample requires Flex SDK 4.0.0.12093 or higher. You can get the latest SDK builds from opensource.adobe.com.

  1. February 9th, 2010 at 08:46 | #1

    Has Flex 4 solved the issue that sometimes the set data function will get called before the component is instantiated? If not, it seems like using the setter to directly reference the labels could result in NPE errors.

  2. Steven Shongrunden
    February 9th, 2010 at 14:20 | #2

    @Amy – The data setter gets called after the renderer is added to the display list and all of the renderer’s children (such as the Label) are created. This should be the same behavior as in halo. Do you have an example of a case where this failed for you in Flex 3?

  3. florian
    January 14th, 2011 at 03:34 | #3

    exactly what I was looking for.

  4. baki
    February 9th, 2011 at 09:21 | #4

    hello, I have may items to be displayed in the list. I can see 6 of them in the list and I need to scroll down to see others. In this case 7th item get index 0 and 8th item get index 1 and goes on… it is related to the displaying items in a list I think, how can I prevent this.

  5. baki
    February 9th, 2011 at 09:26 | #5

    I just found the solution, I changed useVirtualLayout property to false, not it works like here

  6. Steven Shongrunden
    February 20th, 2011 at 13:56 | #6

    @baki – Make sure that your renderer doesn’t have any logic on the creationComplete event when in a virtual layout.

  7. aandhu
    March 30th, 2011 at 17:52 | #7

    after making deletions how can I save a custom list as xml along with row numbers

  1. No trackbacks yet.