views:

1388

answers:

1

Hello guys.

Back again this time working with data providers.

Well i been doing a bit of training with Flex, and I've searched, and i managed to get a ComboBox being populated through XML data. Its works pretty well, he gets the LabelField for each item from XML, but the ID associated to each item he doesn't get then from the XML.

Code:

<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="355" height="465" creationComplete="getPaises.send();"
xmlns:ns1="com.*" title="Perfil" fontWeight="normal">

<mx:HTTPService id="getPaises" url="com-handler/paises.php" result="paisesHandler()"/>

    <mx:Script>
        <![CDATA[
            private function paisesHandler():void
            {   
                pais.dataProvider = getPaises.lastResult.paises.pais;
                pais.data = "id";
                pais.labelField = "nome";

            }
       ]]>

    </mx:Script>

<mx:ComboBox x="121" y="328" width="200" id="pais">
</mx:ComboBox>

</mx:TitleWindow>


And now the ouput XML from PHP:

<?xml version="1.0" encoding="utf-8"?>
<paises>

    <pais>
     <id>1</id>
     <nome>Portugal</nome>
    </pais>

    <pais>
     <id>2</id>

     <nome>Espanha</nome>
    </pais>

</paises

Well this is what it happens, i does gets the Country names from the XML (<nome></nome>) but he doesn't place the associated ID (<id</id>).


I now that because i placed a Label bindable to the ComboBox.selectedIndex

<mx:Label x="121" y="403" text="{pais.selectedIndex}"/>

And as you also see i used pais.data = "id"; that according to examples i saw in the web, it should include the ID from XML to each item NOME in the ComboBox.

I new to Flex, so probably didn't expressed things the right way.

Any help is appreciated. Thanks.

+1  A: 

You don't need this line:

pais.data = "id";

change the label to

<mx:Label x="121" y="403" text="{pais.selectedItem.id}"/>

EDIT: The code can be simplified to

<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
  width="355" height="465" creationComplete="getPaises.send();"
  xmlns:ns1="com.*" title="Perfil" fontWeight="normal">

  <mx:HTTPService id="getPaises" url="com-handler/paises.php" resultFormat="e4x"/>

  <mx:ComboBox x="121" y="328" width="200" id="pais" labelField="nome" 
    dataProvider="{XML(getPaises.lastResult).pais}"/>
</mx:TitleWindow>

Edited the data provider. Thanks

Amarghosh
I'm so blind.. I been trying everything with .selectedItem, .selectedIndex ... Never reminded of this :S. Thanks friend.
Fábio Antunes
Thanks more simple. PS: {XML(getPaises.lastResult).pais} didn't work it worked this way: {(getPaises.lastResult).paises.pais}. Thanks once again.
Fábio Antunes
edited the post to reflect it :)
Amarghosh
lool. {XML(getPaises.lastResult).paises.pais}. Remove the 'XML' :)
Fábio Antunes
Done. Got it. If you add resultFormat="e4x" to the HTTPService declaration XML(getPaises.lastResult).pais should work.
Amarghosh
The result format is "object" by default. Hence paises is treated as the lone property of the result. When you make it "e4x", result is treated as xml and we can apply normal e4x to it.
Amarghosh
Yes it did worked that way. Thanks. By the way. How to specified the selected item in the combobox once its populated, where its ID = 2 (Espanha)?
Fábio Antunes
pais.selectedItem = XML(getPaises.lastResult).pais.(@id == "2");
Amarghosh