This is a migrated thread and some comments may be shown as answers.

Remove node(index) requires multiple iterations?

1 Answer 47 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 2
Richard asked on 09 Oct 2009, 10:07 AM
Hi, can anybody see/explain what I'm doing wrong here? Or let me know a better way to do this. I've tried several approaches, not just the one shown here, but all approaches turn up the same problem.

I have a tree with several levels. I want to remove some nodes from a given level, but leave others: i.e. selectively remove nodes based on their values, all client-side javascript. If I do this:

   var t = $find(ClientID_RadTree1);
   var p = t.findNodeByValue(""); // gets parent node
   for (var i=0; i<p.get_nodes().get_count();i++)   
   {         
       var node = p.get_nodes().getNode(i);         
       console.log(node.get_text());
       if (node.get_value() != '1') p.get_nodes().remove(node);   // only leave the node with value='1'
   }

I have to run in THREE times to get the intended results. Apparently, the node indexes are being changed upon each "remove" call, or something similar.

Any  tips (or source code for the TreeView Javascript)?

Thanks!
R


1 Answer, 1 is accepted

Sort by
0
Veselin Vasilev
Telerik team
answered on 12 Oct 2009, 09:09 AM
Hello Richard,

You can try a reverse for loop:

var t = $find(ClientID_RadTree1);
   var p = t.findNodeByValue(""); // gets parent node
   for (var i = p.get_nodes().get_count() - 1; i > -1; i--)  
   {        
       var node = p.get_nodes().getNode(i);        
       console.log(node.get_text());
       if (node.get_value() != '1') p.get_nodes().remove(node);   // only leave the node with value='1'
   }


Kind regards,
Veselin Vasilev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
TreeView
Asked by
Richard
Top achievements
Rank 2
Answers by
Veselin Vasilev
Telerik team
Share this question
or