11 Replies Latest reply on Aug 15, 2006 9:21 PM by jpwrunyan

    Getting at selectedItem in a ComboBox

    Matlus Level 1
      I'm using HTTPService to get an xml document from the server. the resultFomat is set to "e4x".
      I then assign the lastResult.xxx.xxx to the dataProvider property of a ComboBox and the labelField property of the comboBox is set to the "description" attribute (comboBox.labelField = @description).
      The ComboBox show the expected list of items. So far so good.

      My problem is getting at the selectedItem. What I see is an xmlNode like so:
      <item id="1" description="Hello" />

      I need to get the id attribute.
      I've tried:


      and various other things with no luck.

      comboBox.value also retuns the complete xmlNode

      Can someone please tell me what I need to do in order to be able to get the id attribute?

        • 1. Re: Getting at selectedItem in a ComboBox
          peterent Level 2
          If this is your data:
          <mx:XML id="products" xmlns="">
          <product id="14" name="Widgets">
          <product id="22" name="Gizmos">
          <product id="7" name="Gadgets">
          <product id="11" name="Thingys">

          and this is your ComboBox definition:
          <mx:ComboBox x="100" y="50" dataProvider="{products.product}" labelField="@name"

          then this change event handler should do the trick:
          private function selectHandler( event:flash.events.Event ) : void
          var item:XML = event.target.selectedItem as XML;
          output.text = "id: "+item.@id;

          where output is the id of some Label control.
          • 2. Re: Getting at selectedItem in a ComboBox
            Matlus Level 1

            Thank you for your reply!

            I am able to get the selected item in the event like you've suggested. However, how would I get the selected item without access to the event object. In orther words, how would I get access to the selectedItem using the comboBox as a reference? I need to get at the selectedItem of various comboboxes at the time of "submitting" the form

            Something like:

            I'll try casting the "selectedItem" property as XML and see if it works that way. Unless there is a better/reccomended way to do this?
            • 3. Re: Getting at selectedItem in a ComboBox
              peterent Level 2
              Give all of your ComboBox controls an id then reference the ComboBox with the id:

              <mx:ComboBox id="productCombo" dataProvider="{products.product}" />

              productCombo.selectedItem.@id will address the id attribute. Ideally you should cast it:

              • 4. Re: Getting at selectedItem in a ComboBox
                Matlus Level 1

                As you can see from the first post in this thread, I tried


                where "comboBox" is the "id" of this combobox. So here is what I found since your last post.

                If I "watch" comboBox.selectedItem.@ID I get an error in the watch editor that say:
                "Error during evaluation". and syntax error in expression.

                Which led me to believe that I can't use this syntax to get at the selectedItem of the combobox.

                However, if I assign this to a variable like you suggested it actually works!

                So is the debugger not able to handle this kind of thing? If so it kind of makes an already sub-standard deubgger less useful :-))

                • 5. Re: Getting at selectedItem in a ComboBox
                  peterent Level 2
                  The debugger's job is to show you things as it sees them. If you opened the variable associated with the ComboBox and the scroll down to selectedItem, what type did it display?

                  The getter function of selectedItem property is typed to return an Object. Since an Object is a dynamic type, selectedItem.@id is perfectly Ok and you don't get a syntax error. However, the code the compiler generates is based on the type declared, not what it may or may not be at runtime. So the code is for Object, not for XML. When you datatype it or case it to be an XML, then the compiler will generate the correct code from the e4x library and be able to extract @id.
                  • 6. Re: Getting at selectedItem in a ComboBox
                    Matlus Level 1
                    The debugger shows the complete xml node (<item id="xxx" description="yyy" />).

                    Are you saying the errors I see are as designed? I even tried casting selectedItem to XML and it gave me some other error.

                    If it's as deisgned, I'm not sure what help it can be :). I mean I use debuggers everyday of my life and I've yet to be clueless as I am when using to Flex 2 debugger :).

                    Maybe we need an evaluator? Kind of like the immediate window in the VS.NET debugger, where in I can (while debugging) evaluate expressions so I know what will work and what won't?

                    Oh, well. thanks a lot for your replies. You've been very helpful!


                    • 7. Re: Getting at selectedItem in a ComboBox
                      peterent Level 2
                      I guess I don't understand the problem you are having.

                      The debugger is build on the standard Unix debugger, which was packaged in Flex 1.5 as fdb.
                      • 8. Re: Getting at selectedItem in a ComboBox
                        I3 Level 1
                        Another way of doing this (if you don't mind my chiming in) would be to convert the XML data you are pulling from (meaning the results of your parse) using the mx:Model tag in Flex.

                        In fact, when you use XML as a data provider (or even XMLListCollection), you will run into problems with selectedItem down the road

                        See the following from the flexcoders. This could help you out as I was facing a similar problem:

                        • 9. Re: Getting at selectedItem in a ComboBox
                          Matlus Level 1

                          Thanks for your input. By the way would you be so kind as to elaborate this statement further?
                          ">you will run into problems with selectedItem down the road"

                          • 10. Re: Getting at selectedItem in a ComboBox
                            I3 Level 1
                            Absolutely. The problems are more headaches for you as a developer with selectedItem not returning the results it should on a List based component using XML as a DataProvider. For some reason, although the XMLListCollection and ArrayCollection are similar, ArrayCollection just plays nicer with them and returns expected results.

                            I spent an hour once trying to figure out why selectedItem in a List (based on an XML dp) kept returning empty strings even when I would hard code the attributes in.

                            Then, I stumbled across that thread over on flexcoders, and I saw why.

                            There is also a thread here on these forums which explains a little further:
                            http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=60&catid=585&threadid =1175918

                            • 11. Getting at selectedItem in a ComboBox
                              jpwrunyan Level 1
                              I was looking at that thread on Flex Coders and I wonder if I missed something... their example for putting the result into a model assumes an external .xml file. I couldn't find an example using .xml returned from a webservice. My problem is that I get the following from Struts:
                               <Item data="1" label="label1" />
                               <Item data="2" label="label2" />
                              and I want to ignore the root tag no matter what name it is (depending on the action, the root tag name is different). This was easily done by implementing the following:

                              var rootXML:XML = new XML(e.result);
                              someComp.dataProvider = rootXML.children(); // an XMLList instance

                              but now the data has been internalized as XML and I want to get it back to being an object/array. I'm not sure I see how this is accomplished with <mx:Model>
                              Can you clarify?

                              PS when I say "back to an object/array" I mean back to working like this:
                              trace (e.result.rootTagName.Item[0].label) //outputs "label1" -- original behavior before I turn it into XML
                              and ideally, trace (someComp.dataProvider[0].label) //outputs "label1" -- currently unobtainable behavior