1 Reply Latest reply on Dec 4, 2007 9:08 AM by ntsiii

    Accessing objects within repeaters when changing states

    wlee@unimelb Level 1
      I'm building a quiz canvas, where the user enters a number of responses into textInput fields, and then hits a Submit button to remove their textInput fields, and display the correct feedback for each item.

      I'm having trouble with being able to refer to repeated objects (created by a repeater) when changing states using MXML. Do I need to do it in ActionScript? Or is there some way of looping in MXML - do I need another repeater in the part where I change states to loop through and delete my textInput objects?

      Also, when I change states and use removeChild(), does that actually delete the object, or does it merely hide it?

      My (sad, sad) code:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="100%" height="100%" creationComplete="init();">
      <mx:states>
      <mx:State name="feedback">
      <mx:RemoveChild target="{submitButton}" />
      <mx:RemoveChild target="{inputBox[0]}" />
      </mx:State>
      </mx:states>
      <mx:Script>
      <![CDATA[
      import mx.core.Application;

      [Bindable]
      public var feedbackString:String;
      [Bindable]
      public var indexNo:int;
      [Bindable]
      public var xmlCopy:XML;



      public function init():void
      {
      xmlCopy = new XML(Application.application.getQuestion(indexNo));
      }

      public function giveFeedback() : void
      {
      var i:int;
      var numQs:int;

      numQs = inputBox.length;

      for(i = 0; i < numQs; i++)
      {
      feedbackText .visible = "true";
      }
      feedbackString = xmlCopy.child("questionFeedback");
      debug.text += feedbackString;
      currentState ='feedback';
      }


      ]]>
      </mx:Script>
      <mx:HBox id="myHBox" left="10" top="10">
      <mx:Image source="{xmlCopy.image}" id="theImage"/>
      <mx:VBox left="10" top="10" id="myVBox">
      <mx:Text id="questionText" text="{xmlCopy.questionStem}:" right="10"/>
      <mx:Repeater id="qParts" dataProvider="{xmlCopy.items.item}">
      <mx:VBox id="vBox">
      <mx:HBox id="hBox">
      <mx:Text text="{qParts.currentItem.label}" width="15"/>
      <mx:TextInput id="inputBox" width="300" height="20" />
      </mx:HBox>
      <mx:Text id="feedbackText" text="{qParts.currentItem.correctResponse}" left="20" visible="false"/>
      </mx:VBox>
      </mx:Repeater>
      <mx:Button id="submitButton" label="Get feedback" click="{giveFeedback()}"/>
      <mx:Text id="debug" text="Hello"/>
      </mx:VBox>
      </mx:HBox>

      </mx:Canvas>

      Any help would be most appreciated!

      Cheers,

      Billy
        • 1. Re: Accessing objects within repeaters when changing states
          ntsiii Level 3
          First, if you give a repeated component an id, you can reference a specific repeated instance of that component using the item index in the data Provider:
          var vbCurrent:VBox = vBox[n]; //where "n" is the index of the repeater item.

          Using this you can loop over all the repeated items.

          Additionally, when using a repeater, it often simplifies coding if you create a custom component, with all the repeated stuff in it, and repeat that. Pass in a reference to the entire dataProvider item. This lets you code normally within the component, to easily dispatch events, and to access the dataProvider item in the event handlers. Otherwise, programming interactions between the repeated controls is very messy, requiring getRepeaterItem(), among other extra work.

          Tracy