apologies for how simple this question is.... function tablesfromDb(); iterates and correctly generates data from the data array above- in console.log , however the javaScript table generator only displays the last iteration. Whhhhhyyyyyyy does the javascript dynamic HTML table generator not display all the iterations as in console.log ??????? It's taken me 3 days to get to this point, and I can't get past this....
var data =
[
{
"deviceOne":
{
"input": ["oneInOne", "oneInTwo","oneInThree","oneInFour"],
"output": ["oneOutOne", "oneOutTwo", "oneOutThree"]
}
},
{
"deviceTwo":
{
"input": ["twoInOne", "twoInTwo","twoInThree","twoInFour"],
"output": ["twoOutOne", "twoOutTwo", "twoOutThree", "twoOutFour", "twoOutFive"]
}
}
]
function tablesFromDb(){
for(j=0; j<data.length; j++){
var tableTop = "<form><table>";
var tableHeader = "<tr><th>No</th><th>Input</th><th>Output</th>";
var tableBottom = "</table></form>";
var insideArray = data[j];
for(deviceTitle in insideArray){
var fbdCaption = "<caption>" + deviceTitle + "</caption>\n";
console.log(deviceTitle);
for(k=0; k<insideArray[deviceTitle]["input"].length; k++){
var fdbCellData = "<tr><td>" + (k+1) + "</td>" + "<td>" + insideArray[deviceTitle]["input"][k] + "</td>" + "<td>" + insideArray[deviceTitle]["output"][k] + "</td></tr>\n";
console.log(insideArray[deviceTitle]["input"][k]);
console.log(insideArray[deviceTitle]["output"][k]);
}
}
document.getElementById('container').innerHTML = tableTop + fbdCaption + tableHeader + fdbCellData + tableBottom;
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body onload="tablesFromDb();">
<div id="container"></div>
<script src=js.js></script>
</body>
</html>
UPDATE: I have it working partially, but I'm still having trouble understanding how this loop actually works. How to get the last for loop to iterate through (what I guess is) the above loop to display the correct contents of both tables.... I'm not very good at this....
var data = [
{
deviceOne: {
input: ["oneInOne", "oneInTwo", "oneInThree", "oneInFour"],
output: ["oneOutOne", "oneOutTwo", "oneOutThree"]
}
},
{
deviceTwo: {
input: ["twoInOne", "twoInTwo", "twoInThree", "twoInFour"],
output: ["twoOutOne", "twoOutTwo", "twoOutThree", "twoOutFour"]
}
}
];
//var fdbCellData = "";
function tablesFromDb() {
var tableTop = "";
var tableBottom = "";
var fdbCellData = "";
for (j = 0; j < data.length; j++) {
var insideArray = data[j];
//var fdbCellData = "";
for(var deviceTitle in insideArray){
tableTop += "<form><table border=1><caption>" + deviceTitle + "</caption>";
tableTop += "<tr><th>No</th><th>Input</th><th>Output</th>";
tableBottom += "</table></form>";
console.log(deviceTitle);
//var fdbCellData = "";
for (k = 0; k < insideArray[deviceTitle].input.length; k++) {
fdbCellData += "<tr><td>" + (k + 1) + "</td>";
fdbCellData += "<td>" + insideArray[deviceTitle].input[k] + "</td>";
fdbCellData += "<td>" + insideArray[deviceTitle].output[k] + "</td></tr>";
}
}
}
document.getElementById("container").innerHTML =
tableTop + fdbCellData + tableBottom;
}
<body onload="tablesFromDb();">
<div id="container"></div>
</body>
fbdCaptiongets reassigned in your for loop