I have an issue where I have an array of strings (that are image urls) coming back from an ajax callback and I need to render the images into a html table. The issue is that I don't know the length of the list of course but I need to render a table with 6 columns always. Is it better to build up the table ( and then fill in the cells or build up the table on the fly (loop through number of columns within each .each
statements below). The second one seems more dynamic but not very elegant.
var tableHTML = [];
tableHTML.push("<table>");
$.each(data.ImageURLs, function(index, imageURL) {
// create table content
if (index % 1 == 0) {
tableHTML.push("<tr>");
}
tableHTML.push("<td>" + imageURL + "</td>");
if (index % 6 == 0) {
tableHTML.push("</tr");
}
});
tableHTML.push("</table>");
$("#myTable").html(tableHTML.join(""));