5 Replies Latest reply on Nov 27, 2009 6:46 AM by Nyca77

    Passing a <mx:Repeater> value to a click function

    Nyca77

      Hello everyone.

       

      I just started using Flex and ran into a Problem while parsing a XML file into a flex application.

      Everything works just fine, it's just a tiny little part of my application I can't seem to figure out nor can I find a solution via Google:

       

      <mx:Repeater id="events" dataProvider="{entries.currentItem.elements()}">
           <mx:Text text="{events.currentItem.name}" click="navigateToURL(new URLRequest(events.currentItem.url),'_blank')" />
      </mx:Repeater>

       

      Flexbuilder does not show any errors and the Application runs just fine until I click this text.

      Then I get an errormessage from the running Flash App:

          TypeError: Error #1010: A term is undefined and has no properties.

       

      I have tried a lot of things to pass the "events.currentItem.url" value (that I read from a XML file) to this Actionscript. Nothing seems to work. Is it even possible?

       

      Other things I tried:

         click="{navigateToURL(new URLRequest(events.currentItem.url),'_blank')}"

         No error in Flex, same error in live app. As far as I can tell this is the same thing anyway.

       

         click="navigateToURL(new URLRequest({events.currentItem.url}),'_blank')"

         Flexbuilder error since the click value area already is Actionscript, right?

       

         click="navigateToURL(new URLRequest('http://www.test.com'),'_blank')"

         Hardcoded URL which runs just fine, but doesn't help me since I need the URL from the XML

       

      Can anyone help or propose a simple alternative solution?

       

      Thanks a lot!

       

       

       


        • 1. Re: Passing a <mx:Repeater> value to a click function
          Nyca77 Level 1

          I should probably mention that {events.currentItem.url} does indeed show the URL value from the XML file if I display it as text:

              <mx:Text text="{events.currentItem.url}" />

          So it's not a datasource problem.

          • 2. Re: Passing a <mx:Repeater> value to a click function
            JAcques TRIPOZ

            Just an idea : 'events' might not be a "good" name...

             

             

            You could try something like this

             

            <mx:Text text="{events.currentItem.name}" click="{OnClickText(event);}" />

             

            and define the function

             

            function OnClickText(e:event):void

            {

                 var strUrl:String = e.currenttarget.events.currentItem.url;

                 navigateToURL(new URLRequest(strUrl),'_blank');

            }

             

            Where does currentItem come from ?

            1 person found this helpful
            • 3. Re: Passing a <mx:Repeater> value to a click function
              Nyca77 Level 1

              currentItem comes from a parent Repeater.

              Here's the relevant code with your proposed changes:

               

                  <mx:Script>
                      <![CDATA[
                          import flash.net.URLRequest;
                         
                          function OnClickText(e:Event):void {
                              var strUrl:String = e.currenttarget.ev.currentItem.url;
                              navigateToURL(new URLRequest(strUrl),'_blank');
                          }
                      ]]>
                  </mx:Script>


                  <mx:HTTPService id="calenderService" url="../html-template/data/data.xml" resultFormat="e4x"/>

                  <mx:XMLListCollection id="calenderCollection" source="{calenderService.lastResult.entry}"/>


                  <mx:Repeater id="entries" dataProvider="{calenderCollection}">

                       <mx:TextArea text="{entries.currentItem.day}" />

                       <mx:TextArea text="{entries.currentItem.month}"/>

                       <mx:Repeater id="ev" dataProvider="{entries.currentItem.elements()}">

                            <mx:Text text="{ev.currentItem.name}" click="{OnClickText(event);}" />

                       </mx:Repeater>

                   </mx:Repeater>

               

              This gives me a Flexbuilder error in your function:

              1119: Access of possibly undefined property currenttarget through a reference with static type flash.events:Event

               

              The XML structure looks like this:

               

              <pcalender>
                  <entry>
                      <day>27</day>
                      <month>November</month>
                      <event>
                          <name><![CDATA[Testevent 1]]></name>
                          <url><![CDATA[http://www.test.com]]></url>
                      </event>

                      <event>
                           <name><![CDATA[Testevent 2]]></name>
                           <url><![CDATA[http://www.test.com]]></url>
                       </event>

                  </entry>

                  <entry>
                       <day>28</day>
                       <month>November</month>
                       <event>
                           <name><![CDATA[Testevent 3]]></name>
                           <url><![CDATA[http://www.test.com]]></url>
                       </event>

                      <event>
                           <name><![CDATA[Testevent 4]]></name>
                           <url><![CDATA[http://www.test.com]]></url>
                       </event>

                  </entry>

              </pcalender>

               

              Thanks!

              • 4. Re: Passing a <mx:Repeater> value to a click function
                Subeesh Arakkan Level 4

                Hi,

                 

                After a Repeater component finishes repeating, do not use the Repeater.currentItem property to get the current item. Instead, call the getRepeaterItem() method of the repeated component itself.

                 

                Modify your event handler like this

                 

                function OnClickText(e:Event):void {

                     var strUrl:String = event.currentTarget.getRepeaterItem().url;

                     navigateToURL(new URLRequest(strUrl),'_blank');

                }

                • 5. Re: Passing a <mx:Repeater> value to a click function
                  Nyca77 Level 1

                  Thanks so much Subeesh, the getRepeaterItem() method finally solved my problem

                   

                  And thanks to Jacques for your function as well!

                   

                  Have a great weekend!