[英]How can I populate my HTML table with an array of data?
I'm building a table with HTML and then have created a function which allows me to select a cell. 我正在用HTML构建一个表,然后创建了一个函数,允许我选择一个单元格。 I also have a text field, with a function which will both change the innerText of the cell to the value in the text field and push that value into an array of the data in each table cell. 我也有一个文本字段中,与将两者改变细胞的的innerText在文本字段中的值和推该值到数据的每个表格单元的阵列的功能。 I am then saving it to localStorage. 然后将其保存到localStorage。
Here I encounter my error: how can I properly retrieve the data from the localStorage variable and repopulate the table with the values in the array? 在这里,我遇到了一个错误:如何正确地从localStorage变量中检索数据,并使用数组中的值重新填充表?
Here's my code 这是我的代码
omitting the table creation, textEntering, functions because they appear to be working fine 省略表的创建,textEntering,功能,因为它们似乎运行良好
function save() {
localStorage.setItem("tblArrayJson", JSON.stringify(tblArray));
document.getElementById("lblInfo").innerHTML = "Spreadsheet saved.";
}
This is working because in a test function I can print the tblArrayJson
in an alert. 这是tblArrayJson
,因为在测试功能中,我可以在警报中打印tblArrayJson
。 Next I am trying to load the data back to the table: 接下来,我尝试将数据加载回表:
function loadSheet() {
var loadedData = JSON.parse(localStorage.getItem("tblArrayJson"));
var loadedTable = document.getElementById("SpreadsheetTable");
var currRow, cell;
alert("the retrieved JSON string contains: " + loadedData);
for (var i = 0; currRow = loadedTable.rows[i]; i++) {
for (var j = 0; cell = currRow.cells[j]; j++) {
cell.innerHTML = loadedData.shift();
}
}
}
The alert displaying the loadedData is working correctly. 显示loadingData的警报正常运行。 So what is it about my loop that is failing to insert the array values back into the table? 那么我的循环到底是什么原因而未能将数组值重新插入到表中呢? They just stay blank. 他们只是保持空白。
Is there a problem with the way I'm using the function? 我使用该功能的方式是否有问题? Here is my button with the onclick: 这是带有onclick的按钮:
<input id="btnLoad" type="button" onclick="loadSheet();" value="Load" />
localStorage
is an object . localStorage
是一个对象 。
A loop through an object for(var key in object) {}
is different to a loop through an array for(var i = 0; i < arr.length; i += 1) {}
. 通过对象for(var key in object) {}
的循环不同于通过数组for(var i = 0; i < arr.length; i += 1) {}
的循环。
If the data looks like 如果数据看起来像
var loadedData = {
0: ["cell1", "cell2", "cell3"],
1: ["cell1", "cell2", "cell3"]
}
the loops should look like 循环看起来像
for(var key in loadedData) {
var row = loadedTable.insertRow();
for(var i = 0; i < loadedData[key].length; i += 1) {
var cell = row.insertCell(i);
cell.innerHTML = loadedData[key][i];
}
}
Example 例
var loadedData = { 0: ["cell1", "cell2", "cell3"], 1: ["cell1", "cell2", "cell3"] } function loadSheet() { //var loadedData = JSON.parse(localStorage.getItem("tblArrayJson")); var loadedTable = document.getElementById("SpreadsheetTable"); for (var key in loadedData) { var row = loadedTable.insertRow(); for (var i = 0; i < loadedData[key].length; i += 1) { var cell = row.insertCell(i); cell.innerHTML = loadedData[key][i]; } } } loadSheet();
<table id="SpreadsheetTable"></table>
Your for
loop is missing the terminating condition. 您的for
循环缺少终止条件。 I added that to mine, you're free to modify to fit your needs. 我补充说,您可以根据自己的需要进行修改。
var data = { 0: ["cell1", "cell2", "cell3"], 1: ["cell4", "cell5", "cell6"] }; // localStorage.tblArrayJson = JSON.stringify(loadedData); function loadSheet() { // var data = JSON.parse(localStorage.tblArrayJson); var table = document.getElementById("SpreadsheetTable"); var row, cell; // Used Object.keys() to get an array of all keys in localStorage for (var key of Object.keys(data)) { row = table.insertRow(); for (var i = 0; i < data[key].length; i++) { cell = row.insertCell(i); cell.innerHTML = data[key][i]; } } } loadSheet();
<table id="SpreadsheetTable"></table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.