Solved

Create branch and leaf elements for Flex tree control

Posted on 2010-09-24
3
613 Views
Last Modified: 2012-05-10
Hello All,

I am using a flex tree control, however, the way i have it programmed, it only displays branch elements and not leaf elements. It is displaying leaf elemnts as empty branch folders. How can I correct this using my code below? Please help!

Thanks in advance.

Tonih
<?xml version="1.0"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

				initialize="init()">

	

	<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>

</mx:Application>

Open in new window

0
Comment
Question by:toneDigital
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
petiex earned 250 total points
ID: 33755530
The code that decides whether you get a leaf or a branch is in the isBranch fuction of mx.controls.treeClasses.DefaultDataDescriptor, and, for objects, it is this:

                if (node.children != undefined)
                {
                    branch = true;
                }

The condition in your parseStructure function, (xml.section != null) will always evaluate to true, because xml.section will always return an XMLList object. If there is no section element, it will be an empty XMLList object, not a null one. Here is the corrected parseStructure:


private function parseStructure(xml:XML):Object{    

                                var obj:Object = new Object();

                                obj.label = xml.title;

                                if(xml.section.length() > 0) {

                                        obj.children = new ArrayCollection();

                                        for each (var child:XML in xml.section) {

                                                obj.children.addItem(parseStructure(child));         

                                        }

                                }

                                

                                return obj;

                                

                        }

Open in new window

0
 

Author Comment

by:toneDigital
ID: 33755745
Works like a charm! Thank you very much!
0
 

Author Closing Comment

by:toneDigital
ID: 33755761
Works like a charm!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now