iterate through all dataItems (all kendo ui treeview nodes)

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-07 02:42:26

问题


I am trying to simply loop through all my nodes in a kendo ui treeview.

Current GetNodes() is returning not a function.. how can I access that list?

I have also tried .get_allNodes(); - tree is accurate, just don't where to iterate trough current nodes.

let tv = $("#tree").data("kendoTreeView");

 let nodes = tv.GetNodes();
    console.log(nodes);
    for (var i = 0; i < nodes.length; i++) {
        if (nodes[i].get_nodes() != null) {
          console.log(nodes[i]);

            let item = tv.dataItem(nodes[i]);
            console.log(item);

            let hasChildren = item.hasChildren;    
            console.log(hasChildren);
  
            if (hasChildren = false){
              tv.remove(item);
            }
        }
    }

my consoled treeview i see this...


回答1:


I didn't know Kendo at all, so I read the documentation and hope it will help you.

I recreated a minimal example via the documentation and it works. I read the datasource with all its children recursively.

$(document).ready(function() {
    $("#treeView").kendoTreeView({
        dataSource: [
            {
                text: "Item 1",
                items: [
                    { text: "Item 1.1" },
                    { text: "Item 1.2" }
                ]
            },
            { text: "Item 2" }
        ]
    });
    
    
    var treeView = $("#treeView").data("kendoTreeView");
     
    function recursiveReading(nodes) {
      for (var i = 0; i < nodes.length; i++) {
        console.log(nodes[i].text);

        if (nodes[i].hasChildren)
          recursiveReading(nodes[i].children.view());

      }
    }

    recursiveReading(treeView.dataSource.view());
    
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://kendo.cdn.telerik.com/2020.3.915/styles/kendo.common.min.css" rel="stylesheet" />
<script src="https://kendo.cdn.telerik.com/2020.3.915/js/kendo.all.min.js"></script>


<ul id="treeView">

</ul>


来源:https://stackoverflow.com/questions/64080919/iterate-through-all-dataitems-all-kendo-ui-treeview-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!