Flex Tree with infinite parents and children

Posted by Tempname on Stack Overflow See other posts from Stack Overflow or by Tempname
Published on 2010-05-17T20:27:50Z Indexed on 2010/05/17 20:30 UTC
Read the original article Hit count: 224

Filed under:
|
|

I am working on a tree component and I am having a bit of the issue with populating the data-provider for this tree.

The data that I get back from my database is a simple array of value objects. Each value object has 2 properties. ObjectID and ParentID. For parents the ParentID is null and for children the ParentID is the ObjectID of the parent.

Any help with this is greatly appreciated.

Essentially the tree should look something like this:

Parent1
    Child1
        Child1
    Child2
        Child1
        Child2
Parent2
    Child1
    Child2
    Child3
         Child1

This is the current code that I am testing with:

public function setDataProvider(data:Array):void
        {
            var tree:Array = new Array();               
            for(var i:Number = 0; i < data.length; i++)
            {

                // do the top level array
                if(!data[i].parentID)
                {
                    tree.push(data[i], getChildren(data[i].objectID, data));
                }
            }
            function getChildren(objectID:Number, data:Array):Array
            {
                var childArr:Array = new Array();
                for(var k:Number = 0; k < data.length; k++)
                {
                    if(data[k].parentID == objectID)
                    {
                        childArr.push(data[k]);
                        //getChildren(data[k].objectID, data);
                    }
                }
                return childArr;
            }

            trace(ObjectUtil.toString(tree));
        }

Here is a cross section of my data:

   ObjectID                 ParentID
   1                            NULL            
   10                           NULL
   8                            NULL
   6                            NULL
   4                            6
   3                            6
   9                            6
   2                            6
   11                           7
   7                            8
   5                            8

© Stack Overflow or respective owner

Related posts about flex

Related posts about actionscript-3