Remove all the children of the selected node

4 posts, 0 answers
  1. RichardAD
    RichardAD avatar
    105 posts
    Member since:
    Feb 2012

    Posted 27 Feb 2012 Link to this post

    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

  2. RichardAD
    RichardAD avatar
    105 posts
    Member since:
    Feb 2012

    Posted 10 Mar 2012 Link to this post

    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();
    }
  3. Kendo UI is VS 2017 Ready
  4. Ugur
    Ugur avatar
    1 posts
    Member since:
    Oct 2014

    Posted 23 Jan 2015 in reply to RichardAD Link to this post

    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,
  5. Garry
    Garry avatar
    43 posts
    Member since:
    Sep 2012

    Posted 19 Apr 2015 in reply to Ugur Link to this post

     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());
            } 

     

     

Back to Top
Kendo UI is VS 2017 Ready