XML data in Combo and List comps
Kristtee Jul 10, 2012 4:12 AMHi there!
I am too new to scripting. Yet I am trying simple ones. I have fla with a ComboBox[mycb], a List Comp[mylist], and a Textarea[myta] on stage. I have a simple external xml file as below:
My intension is to list the categories in combobox (distict lables). on selecting one, list comp lists all edibles under that category in combo. Then on selecting an edible in the list component, it just displays the content in the textarea.
--------------
edibles.xml
----------------
<?xml version="1.0" encoding="UTF-8"?>
<edibles>
<item id="1">
<cate>Fruit</cate>
<nam>Apple</nam>
</item>
<item id="2">
<cate>Fruit</cate>
<nam>Pine Apple</nam>
</item>
<item id="3">
<cate>Fruit</cate>
<nam>Wood Apple</nam>
</item>
<item id="4">
<cate>Fruit</cate>
<nam>Custard Apple</nam>
</item>
<item id="5">
<cate>Vegetable</cate>
<nam>Bitter Groud</nam>
</item>
<item id="6">
<cate>Vegatable</cate>
<nam>Carrot</nam>
</item>
<item id="7">
<cate>Vegetable</cate>
<nam>Pumkin</nam>
</item>
<item id="8">
<cate>Vegetable</cate>
<nam>Kale Greens</nam>
</item>
</edibles>
------------------------------
here is my as3 code
--------------------------
import flash.net.URLLoader;
import flash.events.Event;
import flash.net.URLRequest;
var xmldata:XML = new XML();
var cateAr:Array = new Array();
var nameAr:Array = new Array();
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, loadData);
loader.load(new URLRequest("edibles.xml"));
function loadData(e:Event):void{
xmldata = new XML(e.target.data);
//trace(xmldata..cate.length());
for(var i:uint=0;i<xmldata..cate.length();i++){
cateAr.push(xmldata..cate[i]);
nameAr.push(xmldata..nam[i]);
mycb.addItem( { label: cateAr[i], data:i } );
mycb.addEventListener(Event.CHANGE, listit);
}
parseData(xmldata);
}
function parseData(nams:XML):void{
var namlist:XMLList = nams.nam;
for each(var nlist:XML in namlist){
mylist.addItem({label:nlist, data:nlist});
}
}
function listit(e:Event):void{
var no:Number = Number(mycb.selectedItem.data);
trace(nameAr[no]);
}
-----------------------------------
The problem I have is:
1. ComboBox displayes all items. not distict items. I mean it lists all with duplicates.
2. LIst component does not list any item on selecting a category in combo
3. onselecting a combo item it outputs the item.
Please help me. Appreciate your time and help.
Thank you
Kristtee
here is the fla file:https://docs.google.com/open?id=0ByHOlDbL5njbdEp4bkdYQ2RpNFE


