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

Get Nodes count on onNodeDropping() function

7 Answers 80 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Jo Bert
Top achievements
Rank 1
Jo Bert asked on 06 Apr 2010, 11:48 AM
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

7 Answers, 1 is accepted

Sort by
0
Veronica
Telerik team
answered on 09 Apr 2010, 01:29 PM
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.
0
Jo Bert
Top achievements
Rank 1
answered on 14 Apr 2010, 03:10 AM
Thanks Veronica!!! this solution worked. :)
0
Jo Bert
Top achievements
Rank 1
answered on 04 May 2010, 03:17 PM
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
0
Veronica
Telerik team
answered on 07 May 2010, 01:10 PM
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.
0
Jo Bert
Top achievements
Rank 1
answered on 07 May 2010, 02:10 PM
Thanks Veronica for this... it worked!!! :)
0
Jo Bert
Top achievements
Rank 1
answered on 12 May 2010, 07:10 AM
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
0
Veronica
Telerik team
answered on 18 May 2010, 01:18 PM
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.
Tags
TreeView
Asked by
Jo Bert
Top achievements
Rank 1
Answers by
Veronica
Telerik team
Jo Bert
Top achievements
Rank 1
Share this question
or