Solved

Create branch and leaf elements for Flex tree control

Posted on 2010-09-24
3
619 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c# LinkButton OnClientClick 2 34
SharePoint 2013 List with Ratings 6 36
angular2 2 way binding on load 7 39
window.setInterval not firing 7 31
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 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

21 Experts available now in Live!

Get 1:1 Help Now