Solved

Create branch and leaf elements for Flex tree control

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

860 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