3 Replies Latest reply on Jul 30, 2009 4:17 PM by patniemeyer

    Binding PopupMenuButton to XML dataProvider question...

    patniemeyer

      I am having a little trouble with binding a PopupMenuButton to an XML dataProvider.

       

      If I provide a simple array like the following, it works as expected and I get three simple items in the menu:

       

              <mx:dataProvider>
                  <mx:Array>
                      <mx:String>China</mx:String>
                      <mx:String>Japan</mx:String>
                      <mx:String>India</mx:String>
                  </mx:Array>
              </mx:dataProvider>

       

      However if I bind to an XML source like the following what I get is *two* levels of menus that repeat the country name, e.g. Japan->Japan and China->China.

       

                    <somexml>

                          <countries>
                              <country loc="ja">Japan</country>
                              <country loc="zh">China</country>
                          </countries>

                   </somexml>

       

      dataProvider="{somexml.countries.country}"

       

      I know (from trace()) that the result of my expression is an XMLList that looks like this:


      <country loc="ja">Japan</country>
      <country loc="zh">China</country>

       

      Can someone tell me why that produces two levels of menu items?

      Do I need to set a labelFunction of some kind?

       

      I also tried showRoot=false, but that didn't help.

       

      thanks,

      Pat

        • 1. Re: Binding PopupMenuButton to XML dataProvider question...
          David_F57 Level 5

                        <somexml>

                              <countries>
                                  <country loc="ja">Japan</country>
                                  <country loc="zh">China</country>
                              </countries>

                       </somexml>

           

          dataProvider="{somexml.countries.country}"

          Hi Pat,

           

          try  <mx:PopUpMenuButton x="10" y="10" dataProvider="{somexml.countries}" labelField="country">

           

          David.

          • 2. Re: Binding PopupMenuButton to XML dataProvider question...
            Peter deHaan Level 4

            These are a little bit thrown together, but one of these solutions may help:

             

            <?xml version="1.0" encoding="utf-8"?>
            <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                           xmlns:s="library://ns.adobe.com/flex/spark"
                           xmlns:mx="library://ns.adobe.com/flex/halo">
            
                <fx:Declarations>
                    <!-- #1. Change the nodes to use attributes instead of nested text nodes -->
                    <fx:XML id="countriesXML0">
                        <somexml>
                            <countries>
                                <country loc="ja" name="Japan" />
                                <country loc="zh" name="China" />
                            </countries>
                        </somexml>
                    </fx:XML>
            
                    <!-- #2. Add isBranch="false", so the XML menu doesnt try converting that text node -->
                    <fx:XML id="countriesXML1">
                        <somexml>
                            <countries>
                                <country loc="ja" isBranch="false">Japan</country>
                                <country loc="zh" isBranch="false">China</country>
                            </countries>
                        </somexml>
                    </fx:XML>
            
                    <!-- #3. Your XML, but this time the PopUpMenuButton dataProvider will only look for text nodes -->
                    <fx:XML id="countriesXML2">
                        <somexml>
                            <countries>
                                <country loc="ja">Japan</country>
                                <country loc="zh">China</country>
                            </countries>
                        </somexml>
                    </fx:XML>
                </fx:Declarations>
                <s:VGroup x="10" y="10">
                    <mx:PopUpMenuButton showRoot="false"
                            dataProvider="{countriesXML1.countries}"
                            labelField="@name" />
                    <mx:PopUpMenuButton showRoot="false"
                            dataProvider="{countriesXML1.countries}"
                            labelField="country" />
                    <mx:PopUpMenuButton showRoot="false"
                            dataProvider="{countriesXML2.countries.country.text()}" />
                </s:VGroup>
            </s:Application>
            

             

             

            Personally, I like #1. You could probably also create some custom label functions for each approach to change the label to something like "China (zh)" if you wanted more control over the label's text.

             

            Peter

            • 3. Re: Binding PopupMenuButton to XML dataProvider question...
              patniemeyer Level 1

              Thank you very much for your detailed options.

               

              My initial instinct was to try adding ".text" to the binding expression to get the content of the text node, but I was missing the parens: ".text()".  I was thinking properties, not xpath.  I like that solution because it doesn't require modifying the XML.  I like attributes too, but for things with more limited domains that won't contain odd characters or structure, etc...

               

               

              thanks!

              Pat