Search code examples
apache-flexactionscript-3flex3itemrenderer

Display List ItemRenderer Index in Flex3


Is there a direct way to get the item index of the data inside an itemRenderer? I need to display the item number against each item. I am currently doing a workaround and won't allow reuse of my itemRenderer component.

var index:int = model.dataColl.getItemIndex(data) + 1;
itemNo = index.toString();

This is what i am using now, it works, but the concepts of component reuse and data abstraction are compromised.

I am using Flex 3.


Solution

  • The first answer seems to be working, but slow. It takes O(n^2) time as it runs through dataProvider array to get item index each time. We can access rowIndex from listData - it represents index of the current visible item. Vertical scroll position from parent List represents the amount of scrolled items:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" implements="mx.controls.listClasses.IDropInListItemRenderer">
    <mx:Script>
        <![CDATA[
            import mx.controls.listClasses.BaseListData;
            import mx.controls.listClasses.ListBase;
    
            [Bindable] private var index:int = 0;
    
            private var _listData:BaseListData;
            public function get listData():BaseListData
            {
                return _listData;
            }
            public function set listData(value:BaseListData):void
            {
                _listData = value;
            }
    
            override public function set data(value:Object):void
            {
                super.data = value;
                if (data && listData)
                    index = _listData.rowIndex + ListBase(_listData.owner).verticalScrollPosition;
                else
                    index = 0;
            }
        ]]>
    </mx:Script>
    <mx:Label text="{index}"/>
    <mx:Label text="{data.toString()}"/>
    </mx:HBox>