1 Reply Latest reply on Dec 4, 2009 10:43 AM by Gregory Lafrance

    Retrieve the current index from a Event in a Repeater

    Johnking08 Level 1

      I'm using a Repeater to display several Button dynamically and need to pass the index of the clicked button to the clickHandler:

       

      <mx:Repeater id="r" dataProvider="{aAC}" startingIndex="0">

           <mx:Button id="btn" height="15" fontSize="9" label="Click It" click="clickHandler(event.currentTarget.getRepeaterItem().currentIndex);"/>

      </mx:Repeater>

       

      aAC is an ArrayCollection. The index should be the index of aAC.

       

      I also tried "r.currentIndex", "event.target.index", .... None of them worked. What parameter should I use in the clickHandler?

      Thanks.

        • 1. Re: Retrieve the current index from a Event in a Repeater
          Gregory Lafrance Level 6

          This code might be adaptable for your purposes:

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
            <mx:Script>
              <![CDATA[
                import mx.collections.ArrayCollection;
                import mx.controls.Alert;
               
                [Bindable] private var aAC:ArrayCollection = new ArrayCollection([
                  "item one", "item two", "item three", "item four", "item five"
                ]);

                private function clickHandler(evt:MouseEvent):void{
                  Alert.show(aAC.getItemAt(evt.currentTarget.name).toString());
                }
              ]]>
            </mx:Script>
            <mx:Repeater id="r" dataProvider="{aAC}" startingIndex="0">
              <mx:Button id="btn" height="15" fontSize="9" label="Click It"
                click="clickHandler(event);" name="{r.currentIndex}"/>
            </mx:Repeater>
          </mx:Application>


          If this post answers your question or helps, please mark it as such.


          Greg Lafrance
          www.ChikaraDev.com

          Flex Development and Support Services