Remove node(index) requires multiple iterations?

2 posts, 0 answers
  1. Richard
    Richard avatar
    27 posts
    Member since:
    Sep 2008

    Posted 09 Oct 2009 Link to this post

    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


  2. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 12 Oct 2009 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top