【问题标题】:JavaScript populate data dynamically into HTML tableJavaScript 将数据动态填充到 HTML 表中
【发布时间】:2018-01-11 20:10:23
【问题描述】:

我正在尝试用数据动态填充表格单元格。我关注了这个JSfiddle example

这是我的代码:

var heading = new Array();
        heading[0] = "Type"
        heading[1] = "Item Name"
        heading[2] = "Brand"
        heading[3] = "Unit Price"
        heading[4] = "Quantity"

        var stock = new Array();
        for(var i = 0; i < topProductList.length; i++){
            stock[0].push(topProductList[i].type);
            stock[1].push(topProductList[i].itemName);
            stock[2].push(topProductList[i].brand);
            stock[3].push(topProductList[i].unitprice);
            stock[4].push(topProductList[i].quantity);

            console.log(topProductList[i].type + ' ' + topProductList[i].itemName + ' ' + topProductList[i].brand + ' ' + topProductList[i].unitprice + ' ' + topProductList[i].quantity);
        }

我尝试通过循环遍历我的数组来为每一列添加数据。但是,我收到了Cannot read property 'push' of undefined。我打印出console.log 来检查我是否正确检索并且数据检索没有问题。只是我尝试将每个数据添加到列中的部分遇到了问题。

有什么想法吗?

topProductList 的示例数据:

var topProductList = [{type: 'home appliance', name: 'Sound Teoh Tv Cable 5m Hl3599 Soundteoh', unitprice: 9.90, quantity: 5},
{type: 'kitchen appliance', name: ' Powerpac Mini Rice Cooker Pprc09 Powerpac', unitprice: 19.90, quantity: 5},
{type: 'kitchen appliance', name: ' Sona 2-slice Bread Toaster Sto2201 Sona', unitprice: 39.90, quantity: 5},
{type: 'home appliance', name: ' Simply Living. 10" Wall Clock Simply Living', unitprice: 9.90, quantity: 5},
{type: 'kitchen appliance', name: ' Morries Pie Maker Ms-8028pm Morries', unitprice: 29.90, quantity: 4}];

【问题讨论】:

  • topProductList 的示例数据是无效的 JSON,因为您没有关闭 name 键的单引号,我已经更新了 JSON ...看看。

标签: javascript html arrays html-table


【解决方案1】:

给你http://jsfiddle.net/4pEJB/488/

function addTable() {
    var myTableDiv = document.getElementById("metric_results")
    var table = document.createElement('TABLE')
    var tableBody = document.createElement('TBODY')

    table.border = '1'
    table.appendChild(tableBody);

    var heading = new Array();
    heading[0] = "Type"
    heading[1] = "Item Name"
    heading[2] = "Unit Price"
    heading[3] = "Quantity"


    var stock = new Array()
    var topProductList = [{type: 'home appliance', name: 'Sound Teoh Tv Cable 5m Hl3599 Soundteoh', unitprice: 9.90, quantity: 5},
{type: 'kitchen appliance', name: ' Powerpac Mini Rice Cooker Pprc09 Powerpac', unitprice: 19.90, quantity: 5},
{type: 'kitchen appliance', name: ' Sona 2-slice Bread Toaster Sto2201 Sona', unitprice: 39.90, quantity: 5},
{type: 'home appliance', name: ' Simply Living. 10" Wall Clock Simply Living', unitprice: 9.90, quantity: 5},
{type: 'kitchen appliance', name: ' Morries Pie Maker Ms-8028pm Morries', unitprice: 29.90, quantity: 4}];
    

     for(var i = 0; i < topProductList.length; i++){
        var temp = [];
        temp.push(topProductList[i].type);
        temp.push(topProductList[i].name);
        temp.push(topProductList[i].unitprice);
        temp.push(topProductList[i].quantity);
        stock.push(temp);
     }

    //TABLE COLUMNS
    var tr = document.createElement('TR');
    tableBody.appendChild(tr);
    for (i = 0; i < heading.length; i++) {
        var th = document.createElement('TH')
        th.width = '75';
        th.appendChild(document.createTextNode(heading[i]));
        tr.appendChild(th);
    }

    //TABLE ROWS
    for (i = 0; i < stock.length; i++) {
        var tr = document.createElement('TR');
        for (j = 0; j < stock[i].length; j++) {
            var td = document.createElement('TD')
            td.appendChild(document.createTextNode(stock[i][j]));
            tr.appendChild(td)
        }
        tableBody.appendChild(tr);
    }  
    myTableDiv.appendChild(table)
}
<div id="metric_results">
    <input type="button" id="create" value="Click here" onclick="Javascript:addTable()">
</div>

您需要将一个值(数组)推送到库存 stock.push("abc");

arrayarray 中的 stock 变量。 内部 array 包含列级数据,其中外部 array行级数据。 p>

另一种减少代码行数的方法,用下面的代码替换 for 循环。

var keys = Object.keys(topProductList[0]);
 for(var i = 0; i < topProductList.length; i++){
   var temp = [];
   for(var key in keys){
    temp.push(topProductList[i][keys[key]]);
   }
   stock.push(temp);
 }

【讨论】:

  • 但是topProductList里面的数据是一行一行的。我已经更新了这个问题。使用建议的代码,该表没有填充任何内容
  • 是的,这也有效。非常感谢!尽管我使用了 Fazal 的建议并进行了一些修改,以使其成为一条供所有人循环的班轮。 :)
  • @EmmaHannah ...检查我的编辑答案。您可以获取键并循环遍历键,而不是指定每个键。即使将来增加列数,您也不必更改代码..
  • 酷我想我会使用你的版本,因为它支持可扩展性。谢谢老哥!
【解决方案2】:

stock[0] 未定义,您需要像这样初始化数组

var stock = [ [], [], [], [], [], [] ];

【讨论】:

    【解决方案3】:

    您的stock 变量是一个数组,但不是stock[0]

    只是做

    stock.push();
    

    或直接在 as- 上的索引上设置该值-

    stock[0] = topProductList[i].type;
    

    ** 在 cmets 之后编辑 **

    看起来您的股票是一个多维数组。在这种情况下,如果您遵循 jsfiddle 示例,请按照 -

    stock[0] = new Array(topProductList[i].type);
    

    【讨论】:

    • 如果我把它改成 stock[0] = topProductList[i].type; ,它变成了我刚刚添加的屏幕截图的结果。有什么想法吗?
    • 是的,这可以解决问题。我意识到应该用 i 替换索引。现在已经解决了,非常感谢!
    【解决方案4】:

    数组元素没有push。但是,您可以将 push 与数组一起使用。请查看this example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 1970-01-01
      • 2017-06-28
      • 2022-12-19
      • 2021-08-17
      • 1970-01-01
      相关资源
      最近更新 更多