// you’re reading...

life

An Easy way to Implement Custom Tree Models

I’m not even going to pretend to think this is a best practice….but…

In a quick hack based way it worked and allowed me to use custom classes for my Tree in Flex !! I have an array of custom elements that are in many cases nested several levels deep like a set of directories. I was having a fit trying to get my custom ITreeDataDescriptor to work and had used up my attention span on multiple approaches. Finally a simple solution came to me.

If you go into each of the class elements that you are using as a data model and you add a public property “children” and you use it to reference whatever ArrayCollection of child objects then it works the same as if you had done all of the custom code and matching custom DataDescriptor etc. It allows for the node.children to return a boolean value as well. As one extra detail- I added the children=myRealChildren within a conditional so that it would only be initialized if there were actually child nodes.

I haven’t tested this beyond making it work and I would love to write a better explanation at a later time.

PS. If anyone has done this and come across a major problem that I haven’t seen yet please let me know!

Discussion

3 comments for “An Easy way to Implement Custom Tree Models”

  1. I know it’s a few months away, but I plan to speak about custom data descriptors in my session at Flex Maniacs this June. Check out “Under the Hood of an Advanced Flex Component” if you go. :)

    Posted by Josh Tynjala | March 8, 2007, 4:02 pm
  2. I hit the same frustration as you with the ITreeDataDescriptor and was very glad to find your post. I’ve given it a try and so far, so good. Adding a “label” property has that value automatically show up in the Tree control too.

    To be honest, I didn’t follow what you were saying with this statement: “I added the children=myRealChildren within a conditional…” Can you elaborate?

    I’ll let you know if I run into any issues on this with my app.

    Thanks!

    Rick

    Posted by Rick Pechter | April 27, 2007, 7:35 am
  3. @Rick
    Turns out that all I was doing is implementing a piece of the ITreeDataDescriptor Interface.

    in my code I had put a statement “children=content”
    with content being my real array of children. This allowed the original DefaultDataDescriptor to do its job.
    I have since dug quite a bit deeper into the TreeDataDescriptor and plan to post about my findings. Just been too busy and lazy to do it lately.

    Posted by diamondtearz | May 22, 2007, 2:44 pm

Post a comment