Looping multidimensional array into html table displaying incorrectly
Hello All,
I'm trying to take a multi-dimensional array and publish it into an HTML table. The solution I come up seems to be publishing the HTML incorrectly.
Here's the function I am using;
Code:
function test() {
$("#test").html("<table>");
for (i = 0; i < oneLen; i++) {
$("#test").append("<tr>");
for (j = 0; j < oneRow[i].length; j++) {
$("#test").append("<td>" + oneRow[i][j] +"</td>");
}//end j
$("#test").append("</tr>");
}//end i
$("#test").append("</table>");
}//end test
When running the function I am expecting the html information to look as below;
The application is reading a CSV file from a server and turning it into an array so it can be seen in the users browser as a table. Each datapoint/row is going to be assigned a class eventually in the looping process for the CSS. I just want to make sure what I'm writing is correct so my conditional's are correct when I add in those sub line assignments. I've only been doing this for about 90 days so if I'm missing something obvious please let me know.
Found a solution that works differently; I've included the code below as this seems to work pretty well. The difference is that I've created a single variable and modified with my loops.
Code:
function test() {
var table = '<table>';
for (i = 0; i < oneLen; i++) {
for (j = 0; j < oneRow[i].length; j++) {
if (j == 0) {
table += '<tr><td>' + oneRow[i][j] + '</td>';
} else if (j == oneRow[i].length - 1) {
table += '<td>'+oneRow[i][j]+'</td></tr>';
} else {
table += '<td>' + oneRow[i][j] + '</td>';
}//end if
}//end j
}//end i
table += '</table>';
$("#test").html(table);
}//end test
Bookmarks