copy node + children

2 posts, 0 answers
  1. Thomas
    Thomas avatar
    19 posts
    Member since:
    Aug 2013

    Posted 29 Jun 2015 Link to this post

    dear community,

     I'm facing the following issue right now:

     within my TreeView it should be possible, that every node and all its children can be copied within the same tree. Therefore I've setup a button which calls a function that tries to copy the node + its children.

    The function looks like this:

    function CopyTreeViewItems() {
            var treeview = $('#treeview').data('kendoTreeView');
            var selectedNode = treeview.select();
     
            var data = treeview.dataItem(selectedNode);
     
            var newData = {
                id: GenerateGuid(),
                Name: data.Name + "tmp"
            };
     
            treeview.insertAfter(newData, selectedNode);
            var parentItem = treeview.dataItem(treeview.findByText(newData.Name)); //<--- this part is returning 'undefined'
     
                if (data.hasChildren) {
                    var children = data.children.data();
     
                    for (var i = 0; i < children.length; i++) {
                        var add = { id: GenerateGuid(), Name: children[i].Name };
                        treeview.append(add, parentItem);
                    }
                }
    }

    The parent node is copied without any problems. If there are children of a parent node I'm not able to append them to the corresponding new parent.

    Any advices?

    Kind regards

    Tom

  2. Thomas
    Thomas avatar
    19 posts
    Member since:
    Aug 2013

    Posted 30 Jun 2015 in reply to Thomas Link to this post

    I ended up getting the uid from the dS of the newly created parent and assigning the children by tree.findByUid() like this:
    function CopyTreeViewItems() {
     
            var treeview = $('#AdminPart_BaseLines_TreeView').data('kendoTreeView');
            var selectedNode = treeview.select();
     
            var data = treeview.dataItem(selectedNode);
     
            var newData = {
                id: GenerateGuid(),
                Name: data.Name + "tmp",
            };
             
     
            treeview.insertAfter(newData, selectedNode);
            var treeviewDS = treeview.dataSource.data();
            var uid;
            for (var i = 0; i < treeviewDS.length; i++) {
                if (treeviewDS[i].Name === newData.Name) {
                    uid = treeviewDS[i].uid;
                }
            }
            var parentItem = treeview.findByUid(uid);
     
                if (data.hasChildren) {
                    var children = data.children.data();
     
                    var added = [];
     
                    for (var i = 0; i < children.length; i++) {
                        added.push({ id: GenerateGuid(), Name: children[i].Name + "tmp" });
                    }
                    treeview.append(added, parentItem);
                }
     
    }
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top