11 Replies Latest reply on Feb 29, 2008 4:45 PM by ntsiii

    Dynamic IDs

    HooMaster
      I have a database table with questions and question ID's that I'd like to loop over, but I'm having trouble figuring out a way to give the radiobuttongroup's unique ID's. Here's what I have, but Flex is saying that "{QuestionList.currentItem.qid} is not a valid identifier".

      Assuming that an integer can't be used as an ID, I modified my query so that it pulls "qid" which is "q_" + question_ID. Is it possible to use a variable in an ID, or is there a better way to accomplish this?


      <mx:RemoteObject id="questionManager" destination="ColdFusion" source="EmpApp.components.legalQuestions">
      <mx:method name="getQuestions" fault="server_fault(event)"/>
      </mx:RemoteObject>

      <mx:Form width="100%" height="100%">
      <mx:Repeater id="QuestionList" dataProvider="{questionManager.getQuestions.lastResult}">
      <mx:HBox width="100%" paddingTop="10">
      <mx:RadioButtonGroup id="{QuestionList.currentItem.qid}"/>
      <mx:RadioButton label="Yes"/>
      <mx:RadioButton label="No"/>
      <mx:Text width="100%" text="{QuestionList.currentItem.question}"/>
      </mx:HBox>
      </mx:Repeater>
      </mx:Form>
        • 1. Dynamic IDs
          m_hartnett Level 3
          Just for clarification, does this work without the qid issue? Is the QuestionList.currentItem.question being displayed properly?

          If it is then is the name of the of the qid actually q_id?

          Lastly I notice that there is not a FormItem tag within the Form object. Do you want each item to be a form item? I have never tried to build a Form without the FormItems so I am not sure it is possible.

          Overall what you are trying to do is valid.
          • 2. Re: Dynamic IDs
            HooMaster Level 1
            Yes, it outputs the 10 questions properly, but you can only select 1 answer for all 10.

            The qid's values are ( q_1 , q_2 , q_ 3 , etc. ) but the variable name is just qid.

            I thought the form/formitem tags were just for layout/formatting purposes, but I put them in there and gave it a try. (didn't help)

            My only guess is that Flex doesn't like bound ID values.

            Thank you for the response.
            • 3. Re: Dynamic IDs
              m_hartnett Level 3
              I believe you are right.
              I tried to set the id in a repeater and I got the same thing.

              However, you can set the name property of the RadioButtonGroup and then access the name using the getChildByName method if that helps.
              • 4. Re: Dynamic IDs
                HooMaster Level 1
                It can't resolve 'name' as a property of RadioButtonGroup. :(

                I'm at a loss for how to resolve this issue. It looks like I will be hard-coding all of my questions.

                If this really isn't possible, then this seems like a major limitation of Flex.

                Thanks for your help.
                • 5. Re: Dynamic IDs
                  m_hartnett Level 3
                  There are many ways around this issue.
                  You can add a FormItem to the repeater so that each question becomes an FormItem of its own. You can give the formItem a name that is dynamically defined as I described it above. Or the HBox that is surrounding the questions can have a name attribute.

                  When you are getting the values for the questions you can get the children of the form and and utilize the name of the surrounding container to identify what needs to be sent to the server.

                  I have found very few limitations with Flex so far. Sometimes it does take a creative approach to get thing done but once you start thinking in 'Flex terms' almost anything is possible.
                  • 6. Re: Dynamic IDs
                    HooMaster Level 1
                    I haven't tried it yet, but I appreciate you giving me a plan of attack. I will definitely post my results. Thanks.
                    • 7. Dynamic IDs
                      m_hartnett Level 3
                      Here is some sample code from our application I carved up to give you some ideas.

                      The xml is hardcoded but you can follow through and see how the question id is maintained and how to get the children.

                      Keep pluggin on Flex. It is an awsome language. You really can do almost anything with it.
                      One of the best ways to really get familiar with Flex is to learn to use the debugger. Stop it at some point in the script and take a look at all the properties in the Variables tab.

                      Good luck


                      <?xml version="1.0" encoding="utf-8"?>
                      <!-- Tree control example. -->
                      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">

                      <mx:XML id="questions">
                      <root>
                      <questionItem>
                      <question>Do you like dogs?</question>
                      <qid>q_1</qid>
                      </questionItem>
                      <questionItem>
                      <question>Do you like cats?</question>
                      <qid>q_2</qid>
                      </questionItem>
                      </root>
                      </mx:XML>

                      <mx:XMLListCollection id="questionList" source="{questions.questionItem}"/>


                      <mx:Script>
                      <![CDATA[
                      import mx.containers.HBox;
                      import mx.controls.Button;
                      private function getAnswers(e:Event) : void {

                      var a : Array = questionForm.getChildren();
                      for(var i:int = 0;i<a.length;i++) {
                      var lgTxt : String = "";
                      var hBx : HBox = a as HBox;
                      lgTxt = "Question Id: " + hBx.name;
                      var hbxChldrn : Array = hBx.getChildren();
                      lgTxt = lgTxt + " question: " + hbxChldrn[2].text;
                      lgTxt = lgTxt + " Yes: " + hbxChldrn[0].selected;
                      lgTxt = lgTxt + " No: " + hbxChldrn[1].selected;
                      lgTxt = lgTxt + "\r";
                      logText.text = logText.text + lgTxt


                      }

                      }




                      ]]>
                      </mx:Script>
                      <mx:Form id="questionForm" width="100%" height="223">
                      <mx:Repeater id="QuestionList" dataProvider="{questionList}">
                      <mx:HBox width="100%" paddingTop="10" name="{QuestionList.currentItem.qid}">
                      <mx:RadioButton label="Yes" groupName="{QuestionList.currentItem.qid}"/>
                      <mx:RadioButton label="No" groupName="{QuestionList.currentItem.qid}"/>
                      <mx:Text width="100%" text="{QuestionList.currentItem.question}"/>
                      </mx:HBox>
                      </mx:Repeater>
                      </mx:Form>
                      <mx:Button label="Get Answers" click="getAnswers(event)"/>
                      <mx:Text width="528" height="206" id="logText" fontSize="14"/>


                      </mx:Application>
                      • 8. Re: Dynamic IDs
                        ntsiii Level 3
                        Repeater automatically generates ids of repeated components into an Array.

                        So give the RadioButton an id, say "rb". then you can access that component like this:
                        var rbCur:RadioButton = rb[itemIndex];

                        When working with Repeater, I advise putting all of the components into a custom component, and repeating that. Pass in a reference to currentItem. Now you can code the repeated objects normally, instead of worrying about casting currentItem to avoid binding warnings, and you won't need to use getRepeaterItem() in your event handlers.

                        Tracy
                        • 9. Re: Dynamic IDs
                          HooMaster Level 1
                          I'm attempting to work through the example that you provided using the XML, but I'm getting an error...

                          ----TypeError: Error #1009: Cannot access a property or method of a null object reference-----

                          I think it's on this line: lgTxt = "Question Id: " + hBx.name;

                          Does it not think that the children of "questionForm" have names?
                          • 10. Re: Dynamic IDs
                            HooMaster Level 1
                            Tracy, your solution makes a lot of sense. If I put the radioButton in its own component, then using the same ID should no longer be an issue. I'm a bit of a novice, so I will be racking my brain with the details of passing the data in and out of the repeater/component, but I'm already gaining appreciation of Flex's cabilities given a little creative programming.

                            Thanks for your help.
                            • 11. Re: Dynamic IDs
                              ntsiii Level 3
                              See my example, the data flow is simple. Pass in the entire currentItem. Store it in a public property. dispatch an event from the component, and access the data item in the handler via the target property.

                              My example is XML, but this will work the same way for an ArrayCollection of mx:Objects, or your own VOs.

                              Tracy

                              Tracy