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

TreeView: Remove Node

Expand Messages
  • ryanborn82
    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
    Message 1 of 6 , Feb 15, 2006
    • 0 Attachment
      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.

      Thanks,
      -Ryan
    • Adam Moore
      ... 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) {
      Message 2 of 6 , Feb 15, 2006
      • 0 Attachment
        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];
        };
      • David Prusak
        ... in ... pressing ... all ... the ... the ... following to get ... Hi Adam, I m not too familiar with this type of coding. var p = node.parent; p = p &&
        Message 3 of 6 , Mar 8, 2006
        • 0 Attachment
          --- 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
        • Adam Moore
          ... Hi David, that code is actually incorrect. Sorry about that. Instead: var p = node.parent; if (p.parent) { p = p.parent; } The syntax in the original
          Message 4 of 6 , Mar 8, 2006
          • 0 Attachment
            On Wed, Mar 08, 2006 at 10:53:53PM -0000, David Prusak wrote:
            >
            > 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.

            Hi David, that code is actually incorrect. Sorry about that. Instead:

            var p = node.parent;
            if (p.parent) {
            p = p.parent;
            }

            The syntax in the original code is shorthand taking advantage of the way
            the language handles the && operator. This behavior is documented here:

            http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Operators:Logical_Operators


            -Adam
          • dprusak@pacbell.net
            ... in ... pressing ... all ... the ... the ... following to get ... Hi Adam, Do you have a similar routine that would keep the current node in question and
            Message 5 of 6 , Mar 27, 2006
            • 0 Attachment
              --- 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,

              Do you have a similar routine that would keep the current node in
              question and remove its children?

              What I'm trying to accomplish is to gracefully catch an error
              condition with connection manager and treeview. Because we've added
              segmented loading, if during the loading process, the browser can't
              connect to the server, we'd like to return the treeview to its
              previous state which would be:

              1. Remove all added children from the current node in question.
              2. Return the node to Dynamic so that if the user tries again, the
              treeview would know not to attempt to load HTML it thinks it has and
              to grab new data.

              Thanks!
              --David
            • 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 6 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.