Solved

Create branch and leaf elements for Flex tree control

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

778 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