PDA

View Full Version : [AS3] Combo Box and xml


stevethomas
11-04-2009, 10:21 PM
I have an xml that populates a combo box on the stage (instance name of ddList). It works okay, but when I choose an item from the dropdown and then go for another, the combo box gets populated again. So, if I have 4 items to start with, the next time I go to choose I have 8, then 12, etc.

Here is the code I have which I gleened from various tutorials/examples (which I'm sure is the problem considering I really don't know what I'm doing.)

Any help would be appreciated. And if you know of some code that is less extensive and gets the same job done, please post.

import fl.controls.ComboBox;
import fl.data.DataProvider;

ddList.width = 125;
ddList.prompt = "Counties";

var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("mn_counties.xml"));
xmlLoader.addEventListener(Event.COMPLETE, getXML);

var xml:XML;
var county:Array = new Array();
var objects_array=new Array();

ddList.addEventListener(Event.CHANGE, getCounties);
ddList.addEventListener(Event.CHANGE, getInfo);

function getXML(e:Event):void
{
xml = new XML(e.target.data);
var il:XMLList = xml.county;
for(var i:uint=0;i<il.length();i++)
{
county[i] = new Array(il.@name[i],il.@data[i]);
for(var x:uint=0;x<il[i].county.length();x++)
{
county[i].push(il[i].county[x]);
}
}
getCounties();
}

function getCounties(e:Event = null):void
{
for(var i:int=0;i<county.length;i++)
{
ddList.addItem({label:county[i][0],data:county[i][1]});
}
}

Here is my xml

<?xml version="1.0" encoding="utf-8"?>
<counties>
<county name="Aitkin" data="item1"/>
<county name="Anoka" data="item2"/>
<county name="Becker" data="item3"/>
<county name="Beltrami" data="item4"/>
</counties>

Second part of my question is how to populate a text box with info from the same xml by clicking on an item from the dropdown list. This is what I have so far (which doesn't work, of course)

function getInfo(event:Event):void
{
name_txt.text = xml.county.name.text();
client_txt.text = xml.county.data.text();

}

THANKS!

sugarengine
11-04-2009, 10:28 PM
Everytime you touch it, you are adding items to list with that Event Change listener.

You need to just do that once when you first make it, not everytime it is clicked on.

There must be some kind of initializing function that is only called once.

I think

Greg SS
11-04-2009, 11:22 PM
some recommendation, look into DataProvider.


import fl.controls.ComboBox;
import fl.data.DataProvider;

ddList.width = 125;
ddList.prompt = "Counties";
ddList.labelField = "name";

var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("mn_counties.xml"));
xmlLoader.addEventListener(Event.COMPLETE, getXML);

var xml:XML;

ddList.addEventListener(Event.CHANGE, getInfo);

function getXML(e:Event):void
{
xml = new XML(e.target.data);
ddList.dataProvider = new DataProvider(xml);
}

function getInfo(event:Event):void
{
name_txt.text = ddList.selectedItem.name;
client_txt.text = ddList.selectedItem.data;
}

stevethomas
11-05-2009, 12:08 AM
Greg SS,
Works like a charm! Thanks for the quick response. And thanks for the full code.
I knew it had to be simpler than what I had.

Still learning.

Greg SS
11-05-2009, 12:17 AM
Glad to be of service.

as3_novice
11-14-2009, 11:43 PM
I am working on something similar. I was wondering how would I access the the "name" attribute with the ddList.labelField = "name", if my xml had an additional outer tag like the following:


<?xml version="1.0" encoding="utf-8"?>
<outertag>
<counties>
<county name="Aitkin">
<data> item1 </data>
<stuff> works </stuff>
</county>

<county name="Anoka">
<data> item2</data>
<stuff>works</stuff>
</county>

<county name="Becker">
<data> item3</data>
<stuff>works</stuff>
</county>

<county name="Beltrami">
<data> item4</data>
<stuff>works</stuff>
</county>

</counties>
</outertag>


The dropdown list doesn't populate with the additional "parent" tag in the XML.

Greg SS
11-18-2009, 08:00 AM
In essence, you can't, not through the ComboBox properties.
But if you save a reference to the original XML, you will be able to retrieve it.


function handleChange(e:Event):void
{
var index:int = myComboBox.selectedIndex;
var countryName:String = myXML.country[index].@name;
}

as3_novice
11-23-2009, 11:01 AM
Yeah, after some hours of trial and error and searching the web, I figured I wouldn't be able to populate the list using the "labelField" property based on the way my XML is structured.

However, I'm not clear where your "handleChange" event listener comes into play. If I can't populate the list, then "handleChange" serves no purpose. Please let me know if I'm missing something. Thanks

as3_novice
11-23-2009, 11:03 AM
Would it be possible to specify a specific node as the "root" node when specifying the combobox data provider? So instead of ddList.dataProvider = new DataProvider(_xml); Is there some way I could get ddList.dataProvider = new DataProvider(_xml.counties); to work? Just trying to get some clarification on what nodes/elements the combobox properties can access regardless of XML structure.