This is a migrated thread and some comments may be shown as answers.
Remove all the children of the selected node
3 Answers 261 Views
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 27 Feb 2012, 06:28 PM
How would I remove all the children of the selected node ?

on treeview node select
   window.selectedNode = $(e.node);

on menu button 'remove children' click
   uiTreeview.remove(window.selectedNode) ; // removes node and children, want to remove only children

3 Answers, 1 is accepted

Sort by
0
Richard
Top achievements
Rank 1
answered on 10 Mar 2012, 03:38 PM
Made a little headway.  With the following the children are removed, however the expandable icon is still visible.  How to get rid of that ?

$("#tree").kendoTreeView({select: onSelect});
and
function onSelect(e) {
    window.selectedNode = node;
}
and
$(#btnRemoveChildren").click(function(e) {
   $(window.selectedNode).children('.k-group').remove();
}
0
Ugur
Top achievements
Rank 1
answered on 23 Jan 2015, 05:09 PM
var  tree = $('#tree').data('kendoTreeView');
var selectedNode = $(window.selectedNode);
var selectedItem = tree.dataItem(selectedNode) 
if (selectedItem.hasChildren) {
           var items =  selectedItem.children.data();
            for (var i = 0,max = items.length;i < max;i++) {
                    var item = treeview.findByUid(items[0].uid);
                    tree.remove(item);                     
             }
 }

You have to keep index at 0, because when you remove, items are updated.
Without any problem, you can delete children and expandable icon will be invisible.
Cheers,
0
Garry
Top achievements
Rank 2
answered on 19 Apr 2015, 11:41 AM

 Thanks Ugur - your solution is correct as the remove method must be used on an 'item' not a node.

Here is my pure Javascript function:

function RemoveCheckedItems(treeview, nodes)
    {
        for (var i = 0; i < nodes.length; i++)
        {
            var node = nodes[i];
            if (node.checked)
            {
                var item = treeview.findByUid(node.uid);
                treeview.remove(item);
            }
            else if (node.hasChildren)
            {
                ctrlSkillsTree.RemoveCheckedItems(treeview, node.children.view());
            }
        }
    }

 

// It can be called like this:

var treeview = $("#ctrlSkillsTree-Destination").data("kendoTreeView");
        if (treeview)
        {
            ctrlSkillsTree.RemoveCheckedItems(treeview, treeview.dataSource.view());
        } 

 

 

Asked by
Richard
Top achievements
Rank 1
Answers by
Richard
Top achievements
Rank 1
Ugur
Top achievements
Rank 1
Garry
Top achievements
Rank 2
Share this question
or