In AS3/Flex, how can I get from flat data to hierarchical data?

Posted by Dave S on Stack Overflow See other posts from Stack Overflow or by Dave S
Published on 2010-12-23T21:16:16Z Indexed on 2010/12/23 21:54 UTC
Read the original article Hit count: 180

Filed under:
|
|

I have some data that gets pulled out of a database and mapped to an arraycollection. This data has a field called parentid, and I would like to map the data into a new arraycollection with hierarchical information to then feed to an advanced data grid.

I think I'm basically trying to take the parent object, add a new property/field/variable of type ArrayCollection called children and then remove the child object from the original list and clone it into the children array? Any help would be greatly appreciated, and I apologize ahead of time for this code:

private function PutChildrenWithParents(accountData : ArrayCollection) : ArrayCollection{
    var pos_inner:int = 0;
    var pos_outer:int = 0;
    while(pos_outer < accountData.length){
        if (accountData[pos_outer].ParentId != null){
            pos_inner = 0;
            while(pos_inner < accountData.length){
                if (accountData[pos_inner].Id == accountData[pos_outer].ParentId){
                    accountData.addItemAt(
                        accountData[pos_inner] + {children:new ArrayCollection(accountData[pos_outer])}, 
                        pos_inner
                    );
                    accountData.removeItemAt(pos_outer);
                    accountData.removeItemAt(pos_inner+1);
                }
                pos_inner++;
            }
        }
        pos_outer++;
    }
    return accountData;                 
}

© Stack Overflow or respective owner

Related posts about flex

Related posts about flash