Hi all,

I am trying to dynamically build and display a sorted tree, i.e. 
create branches/leaves purely in actionscript.  I *thought* that 
using XML would make this easy due to its hierarchical nature and 
Flex's e4x extensions (which should allow me to easily locate nodes 
in the tree etc without having to code a load of recursion).  An 
example of the kind of structure I am trying to build looks something 
like this:

<group id="1" name="group 1" parent="" displayOrder="1">
    <group id="1_1" name="group 1_1" parent="1" displayOrder="1">
        <group id="1_1_1" name="group 1_1_1" parent="1_1" 
displayOrder="1" />
    </group>
    <group id="1_2" name="group 1_2" parent="1" displayOrder="2" />
</group>
<group id="2" name="group 2" parent="" displayOrder="2" />
<group id="3" name="group 3" parent="" displayOrder="3">
    <group id="3_1" name="group 3_1" parent="3" displayOrder="1" />
</group>

I am using an XMLListCollection as my tree data provider as this 
provides the sorting capabilities I need to retain the sort order as 
I add new nodes.  As new group objects come in the plan is to turn 
them into XML and add them to the tree in the appropriate location 
(based on their parent attribute).  The groups may come in in any 
order so I make use of the sort ability of the XMLListCollection.

After going round in circles for a couple of days trying to get it to 
work, I found the following article on Peter Ent's blog:

http://weblogs.macromedia.com/pent/archives/2006/10/tree_control_da.cf
m

This has proven invaluable (thanks Peter!).  However I am still 
having problems.  In Peter's example he adds top-level nodes via the 
XMLListCollection's addItem method but child nodes via the 
appendChild method of the underlying XML/XMLList object.  Now, while 
this allows me to build a tree, the child nodes added directly to the 
XML are not sorted according to the XMLListCollection's sort order - 
only the top-level nodes are sorted.

Does anyone know how I can build this tree whilst maintaining the 
sort order?  I do not want to hard-code any particular sort function 
as I want the sort field and direction etc. to remain configurable.

Alternatively, is there any way to add child nodes directly through 
the XMLListCollection object?  It surprises me that a collection 
designed for hierarchical XML data doesn't seem to support this (or 
e4x for that matter).  Why should it be necessary to access the 
underlying XML data directly to add child nodes (causing the sort 
order to break)?

Somebody please help - this is driving me nuts!

Nick.


Reply via email to