开发者

Looping through data and inserting into a table

开发者 https://www.devze.com 2023-01-20 14:42 出处:网络
I am trying to get data into a table with jquery. The data is in attributes and an array and I would need to go trough that array to extract the information that I require.

I am trying to get data into a table with jquery. The data is in attributes and an array and I would need to go trough that array to extract the information that I require.

Using the following, I put some content into a array:

$(Stuff).find("desc").each(function(index) {
    stuffArray[index]=$(this).text();
});

Here I put it into a table

$("#table").append('<td><a href="'+ siteRoot+'/'+item.url'"></a><td>'+item.title'</td><td开发者_如何转开发>' + stuffArray[i+1] + '</td>');

I am guessing that I should loop the stuffArray but I don't know how to do append things like that.


you should probably enclose the table construction in a for loop then. The structure is like:

 for(var i = 0; i < stuffArray.length; i++) {
    $("#table").append('<tr><td><a href="'+ siteRoot+'/'+item.url'"><td>'+item.title'</td><td>' + stuffArray[i+1] + '</td></tr>');

}


If you want to loop round the stuffArray and add the contents to the table you can do something like:

for(var x=0;x<stuffArray.length;x++){
    $("#table").append('<tr><td>'+stuffArray[x]+'<td></td>'+stuffArray[x]+'</td></tr>');
}

see a stripped down example here

Update In response to your comments: You still want to loop round but rather than create a new row for each element in StuffArray, add a new <td> element to the table, like:

var markup = '<tr>';
for(var x=0;x<stuffArray.length;x++){
  markup += '<td>'+stuffArray[x]+'</td>';
}
markup+='</tr>';
$('#table').append(markup);

Updated Example

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号