|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
Flex Using a Hierarchy of Components
...to Populate a Flex Tree Control
By: Steve House
Oct. 28, 2006 02:00 PM
This article is about creating a Flex tree control that uses a component hierarchy as the data provider. As with most of my Flex development, after struggling for days and then finally getting something to work, I later find out that there is a much easier way to do it that none of my searches ever turned up. I am a beginner Flex 2 developer, so any constructive feedback would be greatly appreciated.
iSubjectId = 1 In the above example, the top level subjects have null for their parent IDs and then second+ levels use the ID of their parent. When using CFCs to represent this hierarchy I would do something like this:
Subject.cfc Above I have a nested hierarchy of subjects where the top level subject contains an array named children that has child subjects in it and so on and so on down the hierarchy. (Figure 1) My SubjectGateway component would contain a function named readSubjectHierarchy that would return an array of top level Subject components, each containing its child hierarchy. Like so:
Returned Array: OK, if you are still with me...
Enter Flex 2 This was working but didn't seem ideal at all. The Flex 2 documentation surrounding Hierarchical Data kept pointing me to the ITreeDataDescriptor interface (http://livedocs.macromedia.com/flex/2/langref/mx/ controls/treeClasses/ITreeDataDescriptor.html), which I tried using in so many ways I lost count, but it never seemed to work right. Most of the time, I would end up with either a flat list of items or all items showing up as folders including the bottom levels which had nothing beneath them. Since I only wanted the user to select the bottom levels, this was not allowing them to select anything. Not good. Hmmmmm... What I kept seeing, was this reference to a property of the object called children which was supposed to be an array of the child objects. Well wait a minute, I have that, but why does it think that all the levels have children? Then it hit me, I wondered if the presence of the array (even when empty) was making it think that there are children. ColdFusion has no NULL right, so I am always initializing an array to get it to return correctly from the gette method. Maybe this is the problem. So how can I get the getter to not return an array and not error. Well here is my solution:
Subject.cfc (important sections shown) Notes From the Above Code:
package extensions.components.org.mydomain.model So the magic is in not returning anything with getChildren when there are no children in the array as well as not initializing it to an empty array from the start as I tend to do. (Figure 2) The tree control looks like this:
<mx:Tree id="Subject" dataProvider="{subjectHierarchy}" labelField="Subject" showRoot="false" width="300? Tune in for Part 2 (which is a ways out since I am waiting on my company to purchase me a license of Flex Builder 2 now that my trial has expired) where I will show how I created a hybrid dropdown/tree control that provides the convenience of dropdown size and selection display with the hierarchy of a tree control. Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||