How to check level 3 and more child nodes in Kendo Tree View

1 Answer 26 Views
TreeView
Utpal
Top achievements
Rank 1
Utpal asked on 31 Jan 2022, 05:34 PM | edited on 31 Jan 2022, 05:36 PM

Hi ,

Need to check programmatically (based on condition) level 3 (grand child) or more (gran grand child) nodes.

I am able to check till level 2. But can you help me to check the nodes when we have nodes in more that 2 levels.

The following code works up to 2 level:

 

 var treeview = $("#treeview").data("kendoTreeView");
    var myNodes = treeview.dataSource.view();
    var dataSource = treeview.dataSource;

 

    for (var i = 0; i < nodes.length; i++) {

            for (var j = 0; j < nodes[i].items.length; j++) {

                if (nodes[i].items[j].ischecked == true) {



                    for (var k = 0; k < myNodes.length; k++) {
                        myNodes[k].load();
                        if (myNodes[k].hasChildren) {
                     
                            var children = myNodes[k].children.view();
                           
                            if (children) {

                                for (var l = 0; l < children.length; l++) {
                                    if (children[l].id === nodes[i].items[j].id) {
                                        children[l].set("checked", true);

                             
                                        var dataItem = dataSource.get(nodes[i].id);
                                        var node = treeview.findByUid(dataItem.uid);
                                        treeview.expand(node);
                                    }
                                }
                            }
                        }

                    }
                }
            }

        }

1 Answer, 1 is accepted

Sort by
0
Neli
Telerik team
answered on 03 Feb 2022, 01:26 PM

Hello Utpal,

I am not sure if I understand correctly the exact requirements. However, as far as I understand from the provided information you need to check children (at level 3) when the parent (at level 2) is checked. I used the provided snippet to prepare the Dojo example linked here. I changed the logic in the third for-loop in order to iterate over the current node (of level 2) child items. I marked the mentioned part in yellow below:

 for (var i = 0; i < nodes.length; i++) {

          for (var j = 0; j < nodes[i].items.length; j++) {            

            if (nodes[i].items[j].checked == true) {

              var current = nodes[i].items[j]

              for (var k = 0; k < current.items.length; k++) {

                if (current.hasChildren) {
                  var children = current.children.view();

                  if (children) {

                    for (var l = 0; l < children.length; l++) {
                      if (children[l].id === nodes[i].items[j].id) {
                        children[l].set("checked", true);

                        var dataItem = dataSource.get(nodes[i].id);
                        var node = treeview.findByUid(dataItem.uid);
                        treeview.expand(node);
                      }
                    }
                  }
                }

              }
            }
 }

If you need to check the children every time a parent is checked you could also enable the checkChildren property. 

I hope this helps.

Regards,
Neli
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
TreeView
Asked by
Utpal
Top achievements
Rank 1
Answers by
Neli
Telerik team
Share this question
or