在JSON对象JQuery中访问子节点

时间:2019-05-10 07:52:37

标签: jquery json ajax datasource

我需要遍历我作为静态数据源对象创建的JSON数据。

我可以访问创建的DOM,但是我需要遍历数据源中的对象以执行某些操作。

我已将代码发布在下面的小提琴中

  

HTML:

<div class="wrapper">        
    <div class="heading facility"><a href="#" class="listDelete">X</a>Facility</div>
    <div class="heading building"><a href="#" class="listDelete">X</a>Building</div>
    <div class="heading department"><a href="#" class="listDelete">X</a>Department</div>
    <div class="heading line"><a href="#" class="listDelete">X</a>Line</div>
</div>
<div id="treeview"></div>
  

数据:

var facilityTree = new kendo.data.HierarchicalDataSource({
    data: [{
        Level: 1, LocationId: 1, LocationName: "Facility 1", expanded: true, HasChildren: true,
        Children: [
        {
            Level: 2, LocationId: 11, LocationName: "Line 1", expanded: true, HasChildren: true,

        }]
       }],

    schema: {
        model: {
            children: "Children",
            expanded: false,
        }
    }
});
  

jQuery:

var facilityTree = new kendo.data.HierarchicalDataSource({
    data: [{
        Level: 1, LocationId: 1, LocationName: "Facility 1", expanded: true, HasChildren: true,
        Children: [
        {
            Level: 2, LocationId: 11, LocationName: "Line 1", expanded: true, HasChildren: true,

        }]
       }],

    schema: {
        model: {
            children: "Children",
            expanded: false,
        }
    }
});

$(document).ready(function () {
        $(".preloader").fadeIn();
        $("#treeview").kendoTreeView({
             $(".preloader").fadeOut();
            dataSource: facilityTree,
            dataTextField: ["LocationName"],
            loadOnDemand: true,
            dataBound: myTreeViewDataBound,
            template: kendo.template($('#treeview-template').html())
        });

        function myTreeViewDataBound(data){

              var treeview = $("#treeview").data('kendoTreeView');

                }

        });

        // Removing Facility

            $(document).on('click', '.listDelete', function(){

            var myTreeView = $("#treeview").data('kendoTreeView').dataSource;
            for(i = 0; i < myTreeView.length; i++) {
                var facilityLevels = myTreeView[i].LocationName;
                alert(facilityLevels);
            }
        });

JSFIDDLE

0 个答案:

没有答案