3 Replies Latest reply on Sep 20, 2007 4:16 PM by Senor_Roberto

    Self Reference

    SynthesisMod
      I'm using a repeater and have need to know which of it's children is being clicked. How do you do that in Flex?
        • 1. Re: Self Reference
          Senor_Roberto Level 1
          Can you post some MXML of your repeater?

          The event.target should tell you which object it was dispatched from.
          • 2. Re: Self Reference
            SynthesisMod Level 1
            Hi Senor Roberto, thanks for responding to my post. This is my first Flex app and I'm still trying to wrap my mind around it. I appreciate the help.

            I tried event.target and it did bring me closer to what I need to do. However, it returns exactly what you clicked in. Sometimes it returns a child element when what I need is the componenent itself. Is there a way to filter that?

            Here is my sample code of the repeater, a function for testing event.target, and the trace output from the console:

            ----- Repeater Code -----

            <mx:Repeater id="productRepeater" dataProvider="{searchData}">
            <comp:ProductTile addedEffect="transition1" id="productTile" click="showClick(event.target)"
            productName="{productRepeater.currentItem.search.shortName}"
            productPic="{productRepeater.currentItem.search.smallPic.source}"
            productCapLow="{productRepeater.currentItem.search.capacity.low}"
            productCapHigh="{productRepeater.currentItem.search.capacity.high}" />
            </mx:Repeater>


            ----- Function for testing return of "event.target" -----

            public function showClick(whatClick:Object):void
            {
            trace(whatClick);
            }


            ----- Console Output from trace(whatClick) -----

            IF_LoadCells0.productGrid.Canvas60._Tile1.productTile[1]
            IF_LoadCells0.productGrid.Canvas60._Tile1.productTile[2]._Image1.FlexLoader178
            IF_LoadCells0.productGrid.Canvas60._Tile1.productTile[6]._Label1.UITextField158

            ----------

            What I need returned ever time is the id of the component, productTile[1], productTile[2], etc.

            Thanks again for the help,

            [d]
            • 3. Re: Self Reference
              Senor_Roberto Level 1
              When you say you need the id of the component, do you mean index?

              You can't have an id property on a component inside a repeater since there's more than one, but you can have one in the dataProvider object which backs it.

              I've just looked at the documentation (I know, crazy) and I think you should try:

              event.currentTarget.getRepeaterItem();

              This will return you the Object in the dataProvider of the repeater corresponding to the component you clicked. You can take any property you want off it, including an internal id.

              If you don't want the id but actually want the index, you need to set it when the repeater is initialised. In your example, you would add a new property to your ProductTile compponent called productIndex (or similar). Then in your MXML you would have:

              <comp:ProductTile ...productIndex="{productRepeater.currentIndex}"...

              This would inject the index of the repeater into your component when the repeater initialises. In the click handler you could then do this:

              event.currentTarget.getRepeaterItem().productIndex

              And get the index of the repeater which was clicked.