Cloning nodes client side

3 posts, 0 answers
  1. Alice
    Alice avatar
    4 posts
    Member since:
    Aug 2015

    Posted 01 Oct 2015 Link to this post

    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. Hristo Valyavicharski
    Admin
    Hristo Valyavicharski avatar
    975 posts

    Posted 06 Oct 2015 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Alice
    Alice avatar
    4 posts
    Member since:
    Aug 2015

    Posted 06 Oct 2015 Link to this post

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

     

Back to Top