Skip navigation
Currently Being Moderated

XML data in Combo and List comps

Jul 10, 2012 4:12 AM

Tags: #xml #list #combobox #contorl

Hi 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

 
Replies
  • Currently Being Moderated
    Jul 10, 2012 4:59 AM   in reply to Kristtee

    The code does what you tell it to do.  The loop that you have for parsing the data is filling up the lists directly whereas what you want to do is manage the data a bit more before you store it, and then process it a bit more when you select it.

     

    You should store each set as an object, meaning you parse on the item node rather than the cate/nam nodes.  Store all of these objects in an array. 

     

    After that array is filled, go thru it and create another array of all of the different categories, using the Array.indexOf() method on this second array to determine if an item (cate) already exists in it so that you do not duplicate any.  Use this second array to fill the combobox.

     

    Then, when you select one of the items in the combobox, have your code search thru the first array for all objects that have the selected category and add them to the list component (probably add the entire object as the data element of each).

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 10, 2012 8:01 PM   in reply to Kristtee

    To try to help you think thru what I offered, where you have the following lines in your loop....

     

            cateAr.push(xmldata..cate[i]); 

            nameAr.push(xmldata..nam[i]);

     

    I am saying you should store that data differently, as objects....

     

            dataAr.push( { category: xmldata..cate[i], name:xmldata..nam[i], id: i } );

     

    and only store unique categories in the array cateAr and use it to fill the combobox...

     

            if(cateAr.indexOf(xmldata..cate[i]) == -1){ 

                  cateAr.push(xmldata..cate[i]);

            }

     

    ten, when you select an item from the combobox you loop thru the objects in the dataAr array to see which ones match the category to include in the list.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points