简体   繁体   English

如何用数据数组填充HTML表?

[英]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.

相关问题 如何从存储在我 PC 上的文件中获取 XML 数据并使用 javascript 以 HTML 格式填充表格? - How can I fetch XML Data from a file stored on my PC and populate a table in HTML using javascript? 如何将以下字符串拆分为可用于填充 html 表的数组 - How can I split the following string into an array that I can use to populate a html table 用数组数据的 javascript 数组填充 html 表? - Populate html table with javascript array of array data? 如何从CSV文件中提取数据并用它填充HTML表? - How can I pull data from a CSV file and populate an HTML table with it? 如何在我的 html 中使用 json 脚本<script> tag and populate the data in the <img src=???/> - How can I use the json script on my html in the <script> tag and populate the data in the <img src=???/> 将数组中的数据填充到html表中 - Populate data from an array into an html table 如何使用从 GitHub API 获取数据动态填充我的表? - How can I dynamically populate my table with fetch data from GitHub API? 如何使用 JavaScript 创建和填充 HTML 表格 - How can I create and populate an HTML table using JavaScript 如何使用 javascript 填充由 Django 创建的 HTML 表? - How can I populate an HTML table created by Django using javascript? 如何获取所有数据以填充我的下拉列表? - How can I get ALL of my data to populate my dropdown?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM