Problems loading json data with jQuery

I have a data.json content file

[
{"Images" : "images/car1.jpg", "Model" : "model x", "Name" : "car a1"},
{"Images" : "images/car2.jpg", "Model" : "model y", "Name" : "car b2"},
{"Images" : "images/car3.jpg", "Model" : "model z", "Name" : "car c3"}
] 

      

here is my jQuery

$.getJSON("data.json", function (data) {
    var columns = header(data);


    for (var i = 0; i < data.length; i++) {
        var row$ = $('<tr/>');
              for (var colIndex = 0; colIndex < columns.length; colIndex++) {


        $.each(data, function () {
            data[i][columns[0]] = '<img src= "' + this.Images + '" width=240 height=160>';
        });

             var cellValue = data[i][columns[colIndex]];


            if (cellValue == null) { cellValue = ""; }               
            row$.append($('<td/>').html(cellValue));
        }
        $("#jsonTable").append(row$);
    }
});
function header(list) {
    var columnSet = [];
    var headerTr$ = $('<tr/>');

    for (var i = 0; i < list.length; i++) {
        var rowHash = list[i];
        for (var key in rowHash) {
            if ($.inArray(key, columnSet) == -1) {
                columnSet.push(key);
                headerTr$.append($('<th/>').html(key));
            }
        }
    }
    $("#jsonTable").append(headerTr$);

    return columnSet;
}

      

Now my html table (id = "jsonTable") seems to be loading the wrong data. I can see that only the car1 image is loaded in all 3 rows of the "Images" column ... I cannot see the car2 and car3 images.

+3


source to share


1 answer


This cycle $.each

is unnecessary. Delete$.each

Try it without $.each



data[i][columns[0]] = '<img src= "' + data[i].Images+ '" width=240 height=160>';

      

JSFIDDLE

0


source







All Articles