tags:

views:

36

answers:

1

Hello all,

I've created an mxml page that displays a tree control, however the data is only being displayed as branches. Even items that are leaves are being displayed as branches. What am i doing wrong? Please help!

thanks, tone

<mx:Script>

    <![CDATA[

        import mx.collections.ArrayCollection;

        public function init():void {
            var item:Object;
            var array:Array = new Array();

            var xml:XML =      
                <course>          
                    <section>              
                        <title>Introduction to Actionscript</title>              
                        <section>              
                            <title>Lesson 1: Variables</title>              
                            <section>                  
                                <title>Topic 1: Data types</title>              
                            </section>              
                        </section>          
                    </section>      
                </course>;    



            item = parseStructure(xml);  

            array.push(item);

            var arrColl:ArrayCollection = new ArrayCollection(array);

            Tree.dataProvider = arrColl;

        }


        private function parseStructure(xml:XML):Object{    
            var obj:Object = new Object();
            obj.label = xml.title;
            if(xml.section != null) {
                obj.children = new ArrayCollection();
                for each (var child:XML in xml.section) {
                    obj.children.addItem(parseStructure(child));         
                }
            }

            return obj;

        }

    ]]>


</mx:Script>


<mx:HBox>
    <mx:Tree id="Tree" width="300"/>       
</mx:HBox>

A: 

I suspect that when you debug your code, you'll see that xml.section isn't null. XML handling in flex is silly at times and tries to be helpful. It's probably doing something daft like returning the entire XML object when the xml.section is null...

Gregor Kiddie
You are right. I changed it to xml.section.length() > 0.
tonedigital