0

我从 ajax 调用中请求了一些 json 数据,并将所有数据插入到表中。现在就像下面的代码一样,所有数据都在一行单元格中,有没有办法循环遍历表格并将其拆分为多行,每行包含 4 个单元格?

<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>');
        }; 
      }          
    });
4

2 回答 2

2
$.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>');
        }; 
      }          
    });
于 2013-01-08T08:33:27.030 回答
0

模数 (%) 是您的朋友!这应该让你开始......

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);
于 2013-01-08T08:33:24.257 回答