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

Re: TreeView: Remove Node

Expand Messages
  • Chris
    I encountered a problem implementing this provided code on a tree with lots of branches. Some of the branches would be removed, yes, but not others--and when
    Message 1 of 6 , May 2 10:47 AM
    • 0 Attachment
      I encountered a problem implementing this provided code on a tree
      with lots of branches. Some of the branches would be removed, yes,
      but not others--and when I would go through and add other branches,
      I would get odd results. Specifically, sub branches that appeared
      in the tree object itself (as displayed using Firebug for Firefox)
      would not render upon executing the .draw() command.

      I resolved this problem by changing the provided prototype code in
      the _deleteNode function as follows:

      Original (first few lines):
      YAHOO.widget.TreeView.prototype._deleteNode = function(node) {
      var p = node.parent;
      for (var i=0;i<node.children.length;++i) {
      this._deleteNode(node.children[i]);
      }


      Modified (same few lines, i.e., removed the for-loop):
      YAHOO.widget.TreeView.prototype._deleteNode = function(node) {
      var p = node.parent;
      while (node.children.length > 0) {
      this._deleteNode(node.children[0]);
      }

      This new code seems to work closer to the intended behaviour. I
      believe the for loop was not deleting all of the branches because
      the length of the children collection would change on every
      iteration of the loop.

      - Chris

      --- In ydn-javascript@yahoogroups.com, "David Prusak" <dprusak@...>
      wrote:
      >
      > --- In ydn-javascript@yahoogroups.com, Adam Moore <adamoore@>
      > wrote:
      > >
      > > On Wed, Feb 15, 2006 at 03:39:01PM -0000, ryanborn82 wrote:
      > > > Has anyone written a function or know of an easy way to remove
      a
      > > > TreeNode in the new UI Widgets? I am just using basic
      TextNodes
      > in
      > > > the FolderView example. I have found a way to addNodes by
      > pressing
      > > > buttons on a page, but now I need a way to remove one (and
      thus,
      > all
      > > > the children underneath it). Of course, this would need to
      make
      > the
      > > > "folder" icon a "file" icon if there are no more children, but
      > the
      > > > "draw" function should do this no problem.
      > >
      > > This will be included in the next release. You can add the
      > following to get
      > > it to work now:
      > >
      > > YAHOO.widget.TreeView.prototype.removeNode = function(node) {
      > >
      > > // Don't delete the root node
      > > if (node.isRoot()) {
      > > return false;
      > > }
      > >
      > > var p = node.parent;
      > > p = p && p.parent;
      > >
      > > // Delete the node and its children
      > > this._deleteNode(node);
      > >
      > > // Refresh the parent of the parent
      > > p.loadComplete();
      > >
      > > return true;
      > > };
      > >
      > > YAHOO.widget.TreeView.prototype._deleteNode = function(node) {
      > > var p = node.parent;
      > > for (var i=0;i<node.children.length;++i) {
      > > this._deleteNode(node.children[i]);
      > > }
      > >
      > > // Update the parent's collection of children
      > > var a = [];
      > >
      > > for (i=0;i<p.children.length;++i) {
      > > if (p.children[i] != node) {
      > > a[a.length] = p.children[i];
      > > }
      > > }
      > >
      > > p.children = a;
      > >
      > > // Update the node collection in the tree
      > > delete this._nodes[node.index];
      > > };
      > >
      >
      > Hi Adam,
      >
      > I'm not too familiar with this type of coding.
      >
      > var p = node.parent;
      > p = p && p.parent;
      >
      > Does it return a bool? (true/false). I wouldn't assume so as you
      > call p.loadComplete(); after that which wouldn't then work.
      >
      > --David
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.