Reputation: 1495
I have some json data requested from a ajax call, and insert all the data in a table. Right now as the code below, all data are in one row of cells, is there a way to loop through the table and split it into multiple rows and every row contains 4 cells?
<div id="gallery">
<table id="galleryTable"><tr></tr></table>
</div>
$.ajax({
type: "GET",
dataType: "jsonp",
cache: false,
url: linkurl,
success: function(data){
paginate = data.pagination.next_url;
var nextMaxId = data.pagination.next_max_id;
console.log(nextMaxId);
for(var i=0; i<15; i++){
var instaPics = data.data[i].images.low_resolution.url;
var avatar = data.data[i].user.profile_picture;
var like = data.data[i].likes.count;
var comment = data.data[i].comments.count;
console.log(comment);
$(#galleryTable tr').append('<td><img class="tagPics" src="'+instaPics+'"><a>'+like+"likes"
+'</a><img class="avatar" src="'+avatar+'" height="20" width="20" /><a>'+comment+'</a></img></td>');
};
}
});
Upvotes: 0
Views: 1279
Reputation: 1511
$.ajax({
type: "GET",
dataType: "jsonp",
cache: false,
url: linkurl,
success: function(data){
paginate = data.pagination.next_url;
var nextMaxId = data.pagination.next_max_id;
console.log(nextMaxId);
var tr;
for(var i=0; i<15; i++){
var instaPics = data.data[i].images.low_resolution.url;
var avatar = data.data[i].user.profile_picture;
var like = data.data[i].likes.count;
var comment = data.data[i].comments.count;
console.log(comment);
if(i % 4 === 0){
tr = $('<tr></tr>');
$('#galleryTable').append(tr);
}
tr.append('<td><img class="tagPics" src="'+instaPics+'"><a>'+like+"likes"
+'</a><img class="avatar" src="'+avatar+'" height="20" width="20" /><a>'+comment+'</a></img></td>');
};
}
});
Upvotes: 2
Reputation: 1550
Modulo (%) is your friend! This should get you started ...
var row = '';
for(var i=0; i<15; i++) {
// ...
if( i > 0 && i % 4 == 0 ) {
$('#galleryTable tr').append(row);
row = '';
}
row += '<td> ...';
}
$('#galleryTable tr').append(row);
Upvotes: 0