Search code examples
jqueryjsonjqtree

how to add dynamic data to jq tree


i am getting json data from ajax response but when i am providing this data to jq tree its only printing one node. How to give data to jq tree to completely display a tree by for loop.

Here is my sample json.

{"libraries":[{"Elements"["CustomerTable","EmployeeGrid"],"LibraryName":"test.rptlibrary"},

{"Elements":["CustomerTable","EmployeeGrid","Employeetable"],"LibraryName":"test2.rptlibrary"}]}

Below is my code:

$.ajax({
    type : "post",
    url : 'GetXYZElement',
    success : function(response) {
        var obj = JSON.parse(response);
        for (var i = 0; i < obj.libraries.length; i++) {
            var library = obj.libraries[i];
            var libraryName = library.LibraryName;

            for (j = 0; j < library.Elements.length; j++) {
                var element = library.Elements[j];
            }
            var data = [ {
                label : libraryName,
                children : [ {
                    label : element
                }, ]
            }, ];
            $('#tree1').tree({
                data : data
            });

        }

    },
    error : function(ts, e) {

    }

});

Solution

  • $.each() function can be used for this.

    $.each(response.elements, function(index, value){
        var element=value;
        alert(value);
    });