0 Replies Latest reply on Oct 8, 2010 9:33 AM by elbhorgo

    Load tree dynamically from database


      Hi all!


      Im creating an Flex application where I'm creating a tree during runtime - i can add and remove nodes at any place - which I later save to a db.
      My problem is re-creating this tree from the db.


      The important data saved in nodes and db is:
      Every node have a unique id (node_id).
      Every node has info regarding which node that is its parent (parent_id) - this id is the nodeID for its parent.
      Root node parentID=-1.
      "level" tells how many levels away from the root is a node.


      Then my plan was to:
      Put the root node (get node with parent id=-1).
      Then to find the children of the root node and put them into the tree (using tree.dataDescriptor.addChildAt(papaNode,newNode,0))
      Then go through the tree, level by level, matching a node from the db with its parent node in the tree.


      With my limited skills I get lost in the Tree/XMLList djungle...
      My problem is finding the correct node/parent,that has allready been moved from db and put into the tree, for a new node (look under "whos your daddy" in the code).


      Tree/XML structure:
      <mx:Tree id="blockTree">
      <mx:XMLListCollection id="blockList">
         <block label="X" block_id="1" parent_id="-1" level="0"> 
          <block label="XX" block_id="2" parent_id="1" level="1">

      Code structure:

      //nrlvl = number of levels in tree (calculated from db above)
      //the root node has been put above with blockList.addItem(newBlock);

      // going through all the levels in the stored tree. Starting on the level under the root
      for (var i:int=1;i<=nrlvl;i++){
      block_state.sqlConnection = connection;
      block_state.parameters[0] = i;
      var nr:int = blockArray2.length; //number of nodes found for a specific level
      // going through all nodes found at a specific level of the tree
      for (var j:int=0;j<nr;j++){
        newBlock = <block/>; //structuring the new node
        newBlock.@label = blockArray2[j].BLOCKNAME;
        newBlock.@block_id = blockArray2[j].BLOCK_ID;
        newBlock.@parent_id = blockArray2[j].PARENT_ID;
        newBlock.@level = blockArray2[j].LEVEL;
        // whos your daddy
        if (i == 1){ //first level of nodes are under the root and thus we dont yet have a tree structure. Just put 'em under the root.
         blockTree.dataDescriptor.addChildAt(papa,newBlock,0); //THIS WORKS!!!
        } else { 
         nodeList = XMLList(story); //translate out XMLListCollection into an XMLList
         papa = XML(nodeList.descendants().(@block_id==blockArray2[j].PARENT_ID)); //Because a block can only have one parent the xmllist going to be 1 value.
         blockTree.dataDescriptor.addChildAt(papa,newBlock,0); //THIS DOESNT REALLY WORK THAT WELL! Guess that my papa is bad to the core!


      Any good ideas of how to find and set the parent (papa) in a tree and attach a child to that node?

      The first level of nodes are set ok. Going through the values of papa for the other levels everything seems fine, but these nodes will not "attach" themselves to papa.


      Or any better way of creating a tree from a db?