Loading ...
Sorry, an error occurred while loading the content.

problem with treeView nodes and applyParent

Expand Messages
  • arturejsmont
    Hi i wanted to make a simple script that would create nodes connected to root to have node objects ready with labels etc. and then when i have them all created
    Message 1 of 1 , Sep 3, 2008
    • 0 Attachment
      Hi i wanted to make a simple script that would create nodes connected
      to root to have node objects ready with labels etc. and then when i
      have them all created i would simply go through map id->parent to
      assign them one under another. Problem is that branches dont seem to
      be collapsable any more and layout looks funny. Code i used looks like
      this:


      var MyMenuTree = {
      // all the nodes come somehow from outside
      nodes: [
      {id:13, parent: 0, label: 'node1', node: null},
      {id:52, parent: 0, label: 'node2', node: null},
      {id:55, parent: 52, label: 'node3', node: null},
      {id:56, parent: 52, label: 'node4', node: null},
      {id:57, parent: 52, label: 'node5', node: null},
      {id:58, parent: 13, label: 'node6', node: null}
      ],
      // this is created to have the map stringId->nodesArrayElements
      idToNodeMap: {},
      // yahoo tree
      tree: null,

      // initialize
      treeInit: function() {
      MyMenuTree.buildNodes();
      },

      buildNodes: function() {
      // map ids to objects
      for(var i=0; i<this.nodes.length; i++){
      this.idToNodeMap[ this.nodes[i].id ] = this.nodes[i];
      }

      // instantiate the tree:
      this.tree = new YAHOO.widget.TreeView("treeDiv1");

      // add all the nodes under root first
      for (var i = 0; i < this.nodes.length; i++) {
      this.nodes[i].node = new
      YAHOO.widget.TextNode(this.nodes[i].label, this.tree.getRoot(), false);
      }

      // assign them to correct parents
      var parent = null;
      for(var i=0; i<this.nodes.length; i++){
      parent = this.idToNodeMap[ this.nodes[i].parent ];
      if( parent ){
      this.nodes[i].node.applyParent( parent.node );
      }
      }
      this.tree.draw();
      }
      }

      // Add an onDOMReady handler to build the tree when the document is ready
      YAHOO.util.Event.onDOMReady( MyMenuTree.treeInit );



      I thought it would be easiest, fast and would not involve any
      tree-like handling.
    Your message has been successfully submitted and would be delivered to recipients shortly.