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

Cloning nodes client side

2 Answers 38 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Alice
Top achievements
Rank 1
Alice asked on 01 Oct 2015, 06:46 PM

Our solution is looking to clone items from a master list into a new list. We have this working on the server side but would like to move it client side if it is possible so we can prevent round trips taken. 

protected void HandleDrop(object sender, RadTreeNodeDragDropEventArgs e)
 {
     RadTreeNode sourceNode = e.SourceDragNode;
     RadTreeNode destNode = e.DestDragNode;
     RadTreeViewDropPosition dropPosition = e.DropPosition;
     //var currentlesson = sourceNode.Value;
     //RadTreeNode foundNode = RadTreeView2.FindNodeByValue(currentlesson);
 
     if (destNode != null)
     {
 
         if (sourceNode.TreeView.SelectedNodes.Count <= 1)
         {
             var currentlesson = sourceNode.Value;
             RadTreeNode foundNode = RadTreeView2.FindNodeByValue(currentlesson);
             //this checks if node already exists in new list
             if (foundNode == null)
             {
                 int? index = PerformDragAndDrop(dropPosition, sourceNode, destNode);
 
                 if (index != null)
                 {
                     //Console.Error.WriteLine("NodeDrop: " + e.SourceDragNode.Text + " to " + e.DestDragNode.Text);
                     //var currentindex = e.SourceDragNode.Index;
                     var currentindex = index;
                     var objectid = e.SourceDragNode.Value;
                     var parentid = destNode.Value;
                     var parentid2 = "Newlist";
                     var action = "moved";
                     if (parentid.Contains("LN"))
                     {
                         EventMovement.Add(action + currentindex + "," + objectid + "," + parentid2);
                         result.Text += action + "," + currentindex + "," + objectid + "," + parentid2 + Environment.NewLine;
                     }
                     else
                     {
                         EventMovement.Add(action + currentindex + "," + objectid + "," + parentid);
                         result.Text += action + "," + currentindex + "," + objectid + "," + parentid + Environment.NewLine;
                     }
                 }
             }
 
         }
         else if (sourceNode.TreeView.SelectedNodes.Count > 1)
         {
             int? index = PerformDragAndDrop(dropPosition, sourceNode, destNode);
 
             if (index != null)
             {
                 foreach (RadTreeNode currentNode in sourceNode.TreeView.SelectedNodes)
                 {
                     var currentlesson = currentNode.Value;
                     RadTreeNode foundNode = RadTreeView2.FindNodeByValue(currentlesson);
                     //this checks if node already exists in new list
                     if (foundNode == null)
                     {
                         PerformDragAndDrop(dropPosition, currentNode, destNode);
                         //Console.Error.WriteLine("NodeDrop: " + currentNode.Text + " to " + e.DestDragNode.Text);
                         var currentindex = index;
                         var objectid = currentNode.Value;
                         var parentid = destNode.Value;
                         var action = "group-moved";
                         EventMovement.Add(currentindex + "," + objectid + "," + parentid);
                         result.Text += action + "," + currentindex + "," + objectid + "," + parentid + Environment.NewLine;
                     }
                 }
             }
         }
         destNode.Expanded = true;
     }
 }
 private int? PerformDragAndDrop(RadTreeViewDropPosition dropPosition, RadTreeNode sourceNode, RadTreeNode destNode)
 {
     int? index = null;
 
     if (sourceNode.Equals(destNode) || sourceNode.IsAncestorOf(destNode))
     {
         return null;
     }
     // clone the sourceNode
     List<RadTreeNode> sourceNodeClone = CloneNode(sourceNode);
     if (remove == false)
     {
         sourceNode.Owner.Nodes.Remove(sourceNode);
     }
     // add source node as a child of the destination node
     //if (!sourceNode.IsAncestorOf(destNode))
     //{
     //    destNode.Nodes.Add(sourceNodeClone);
     //    sourceNodeClone.AllowDrop = false;
     //}
 
     // this prevents items from being added as children when moved
     switch (dropPosition)
     {
         case RadTreeViewDropPosition.Over:
             // child
             if (!sourceNode.IsAncestorOf(destNode))
             {
                 foreach (RadTreeNode node in sourceNodeClone)
                     destNode.Nodes.Add(node);
 
             }
             break;
 
         case RadTreeViewDropPosition.Above:
             // sibling - above                   
             foreach (RadTreeNode node in sourceNodeClone)
                 destNode.InsertBefore(node);
             break;
 
         case RadTreeViewDropPosition.Below:
             // sibling - below
             foreach (RadTreeNode node in sourceNodeClone)
                 destNode.InsertAfter(node);
             break;
     }
     var currentlesson = sourceNode.Value;
     RadTreeNode foundNode = RadTreeView2.FindNodeByValue(currentlesson);
     if (foundNode != null)
     {
         index = foundNode.Index;
     }
     return index;
 }
 private List<RadTreeNode> CloneNode(RadTreeNode sourceNode)
 {
 
     List<RadTreeNode> clonedNode = new List<RadTreeNode>();
     RadTreeNode clone = new RadTreeNode();
     clone.Text = sourceNode.Text;
     clone.Value = sourceNode.Value;
     clone.ImageUrl = sourceNode.ImageUrl;
     clone.ExpandedImageUrl = sourceNode.ExpandedImageUrl;
     clone.AllowDrop = sourceNode.AllowDrop; //this carries over the allow drop from the main list
 
     //check if parent node -- for each statement didnt work here
     if (sourceNode.ParentNode != null)
     {
         if (sourceNode.Nodes.Count > 0)
         {
             //grab and move children too
             for (int i = 0; i < sourceNode.Nodes.Count - 1; i++)
             {
                 clone.Nodes.Add(sourceNode.Nodes[i]);
             }
 
 
         }
     }
     clonedNode.Add(clone);
     return clonedNode;
 }

2 Answers, 1 is accepted

Sort by
0
Hristo Valyavicharski
Telerik team
answered on 06 Oct 2015, 08:00 AM
Hi Alice,

It is similar to the server side:
<telerik:RadTreeView runat="server" EnableDragAndDrop="true" OnClientNodeDropping="dropping" >
               <Nodes>
                   <telerik:RadTreeNode Text="Node 1">
                       <Nodes>
                           <telerik:RadTreeNode Text="Node 1.1"></telerik:RadTreeNode>
                       </Nodes>
                   </telerik:RadTreeNode>
                   <telerik:RadTreeNode Text="Node 2"></telerik:RadTreeNode>
               </Nodes>
           </telerik:RadTreeView>

<script type="text/javascript">
    function dropping(sender, args) {
        var sourceNode = args.get_sourceNode();
        var destNode = args.get_destNode();
 
        sender.trackChanges();
        var node = new Telerik.Web.UI.RadTreeNode();
        node.set_text(sourceNode.get_text());
        node.set_value(sourceNode.get_value());
 
        destNode.get_nodes().add(node);
        sender.commitChanges();
    }
</script>

Working with Nodes on the Client

Regards,
Hristo Valyavicharski
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Alice
Top achievements
Rank 1
answered on 06 Oct 2015, 12:26 PM

Will this also work with node children the way it did server side?

 

Tags
TreeView
Asked by
Alice
Top achievements
Rank 1
Answers by
Hristo Valyavicharski
Telerik team
Alice
Top achievements
Rank 1
Share this question
or