Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create branch and leaf elements for Flex tree control

Posted on 2010-09-24
3
Medium Priority
?
651 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
petiex earned 1000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

715 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