Get Nodes count on onNodeDropping() function

8 posts, 0 answers
  1. Jo Bert
    Jo Bert avatar
    41 posts
    Member since:
    Aug 2008

    Posted 06 Apr 2010 Link to this post

    Hello,

    Can anyone help me with my problem.

    I want to prevent users from dragging a node when the destination node is already the 4th tier. I tried using the code below. yes it works when i tried to add a node into the 4th node. But my problem here is that when the sourcenode have a child node with a grandchild node, the scenario here is that the alert will not pop up because the condition is still valid where the sourcenode is dragged into the 2nd or 3rd node, with this scenario, my treeview will have a node with 5 or 6 tiers.

    can anyone give me an approach?

     if (args.get_destNode().get_level() > 2) {
                            alert('Cannot drop node here');
                            args.set_cancel(true); // Cancel the event  
                            }

    Thanks
  2. Veronica
    Admin
    Veronica avatar
    1213 posts

    Posted 09 Apr 2010 Link to this post

    Hi Jo Bert,

    The point is that you'll have to check the level of the last-tier child of the sourceNode and add it to the level of the destination node. If the result is bigger than 2 then alert should appear and event must be canceled.

    I get the level of the last-tier child of the destination node via recursion:

    function getLevel(currentNode) {
               var level;
               while (currentNode.get_nodes().get_count() != 0) {
                   level = currentNode.get_nodes().getNode(0).get_level();
                   currentNode = currentNode.get_nodes().getNode(0);
               }
               return level;
           }

    You may find the full code in the attached .zip file.

    Hope this helps.

    Best wishes,
    Veronica Milcheva
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jo Bert
    Jo Bert avatar
    41 posts
    Member since:
    Aug 2008

    Posted 13 Apr 2010 Link to this post

    Thanks Veronica!!! this solution worked. :)
  5. Jo Bert
    Jo Bert avatar
    41 posts
    Member since:
    Aug 2008

    Posted 04 May 2010 Link to this post

    Hi veronica,

    One thing I noticed here is that when i tried to drag the third node to another node which only has 1 child, it displays the node cannot drop here? can you send me the fix for this?

    Thanks
  6. Veronica
    Admin
    Veronica avatar
    1213 posts

    Posted 07 May 2010 Link to this post

    Hi Jo Bert,

    The problem was that I have a wrong logic when getting the level of the deepest child of the current node. I was checking it awlays according to the root node. In your case we need to get the level of the deepest level of the node on the 3-rd level. If it is Grand Child RadTreeNode 1.1.1 - the level of the deepest child is 3. This summed with the destination node level (Root RadTreeNode 2 for example) will be 3 as a result. I show the alert when level is > 2. That was the reason for showing an  inappropriate message : "Can not drop here".

    The solution is to subtract the deepest child node level from the source node level:

    function clientSideEdit(sender, args) {
                var destinationNode = args.get_destNode();
                var destLevel = destinationNode.get_level();
      
                if (destinationNode) {
                    var treeView = $find('RadTreeView1');
      
                    treeView.trackChanges();
                    var sourceNodes = args.get_sourceNodes();
                    for (var i = 0; i < sourceNodes.length; i++) {
                        var sourceNode = sourceNodes[i];
                        var realLevel = getLevel(sourceNodes[i]) - sourceNodes[i].get_level();
                        if ((realLevel + destLevel) > 2) {
                            alert('Cannot drop node here');
                            args.set_cancel(true); // Cancel the event 
                        }
                        else {
                            if (args.get_dropPosition() == "above") {
                                insertBefore(destinationNode, sourceNode);
                            }
                            if (args.get_dropPosition() == "below") {
                                insertAfter(destinationNode, sourceNode);
                            }
                            if (args.get_dropPosition() == "over") destinationNode.get_nodes().add(sourceNode);
                        }
                    }
                    destinationNode.set_expanded(true);
                    treeView.commitChanges();
                }
            }

    Find the full code in the attached .zip file.

    Please let me know if this was helpful.

    Regards,
    Veronica Milcheva
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  7. Jo Bert
    Jo Bert avatar
    41 posts
    Member since:
    Aug 2008

    Posted 07 May 2010 Link to this post

    Thanks Veronica for this... it worked!!! :)
  8. Jo Bert
    Jo Bert avatar
    41 posts
    Member since:
    Aug 2008

    Posted 12 May 2010 Link to this post

    Hi again veronica,

    your code worked but if I am not mistaken, the getlevel function only loops through the first child. what if the parent has 3 childs and the second child has another child. this will not be checked since the code only deals with the first child of the source node.

    Thanks
  9. Veronica
    Admin
    Veronica avatar
    1213 posts

    Posted 18 May 2010 Link to this post

    Hello Jo Bert,

    You are right. My code worked only for the first child of the sourceNode. I changed the code and now it works properly. Even for multiple selection of nodes.

    You may find the full code in the attached .zip file.

    Hope this helps.

    All the best,
    Veronica Milcheva
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017