0

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) {

    }

});
Mayur
  • 1,123
  • 3
  • 22
  • 38
  • why loop 'var element = library.Elements[j];' this do not make sense – caoglish Aug 12 '14 at 07:36
  • Thanks @caoglish the element is passed to label in jqtree. Bcoz the Elements is json array – Mayur Aug 12 '14 at 07:42
  • 1
    what I mean: in your code, this part will always get library.Elements[library.Elements.length-1], the go to 'var data = [ {...' – caoglish Aug 12 '14 at 07:59
  • i have removed the for loop for library.Elements but then also it is printing only 1 node. i am passing three node. – Mayur Aug 12 '14 at 08:47
  • the main point of your processing is that covert your data structure to jqtree data structure. so could you please remove ajax part and create jsfiddle for your only data processing part. also give the correct json object of the data. so I can see what I can help – caoglish Aug 12 '14 at 12:37

1 Answers1

1

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

$.each(response.elements, function(index, value){
    var element=value;
    alert(value);
});
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
lekhamani
  • 71
  • 7