如何在< table>内部显示数组值标签? [英] how to display array values inside <table> tag?

查看:122
本文介绍了如何在< table>内部显示数组值标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数组值。我想在HTML表格标记中显示这些值

 < script type =text / javascript> 
var orderArray = [
[1,2012年8月29日,Product1,client1],
[2,2012年8月29日 ,Product2,client2],
[3,2012年8月29日,Product3,client3],
[4, 2012,Product4,client4],
[5,2012年8月29日,Product5,client5]
];

函数display()
{
for(i = 0; i< ordertArray.length; i ++)
{
//如何显示值div或table标签内数组的内容???
}
}

< / script>




如何显示div或table标签内数组的值?

解决方案

orderArray 的项目代表< ; tr> 元素,其中每个元素表示一个< td> 元素。因此,您可以遍历 orderArray 创建< tr> s,然后循环遍历每个循环的元素, code>< td> s: http://jsfiddle.net/h7F7e/

  var table = document.getElementById(table); //将此设置为您的表

var tbody = document.createElement(tbody);
table.appendChild(tbody);
orderArray.forEach(function(items){
var row = document.createElement(tr);
items.forEach(function(item){
var cell = document .createElement(td);
cell.textContent = item;
row.appendChild(cell);
});
tbody.appendChild(row);
});


I have array values like this. I want to display these values in HTML table tag

<script type="text/javascript">
var orderArray = [
    ["1","29-Aug-2012", "Product1", "client1"],
    ["2","29-Aug-2012", "Product2", "client2"],
    ["3","29-Aug-2012", "Product3", "client3"],
    ["4","29-Aug-2012", "Product4", "client4"],
    ["5","29-Aug-2012", "Product5", "client5"]
    ];

function display()
{
    for(i=0;i<ordertArray.length;i++)
    {
    //How to display values of array inside the div or table tag ???
    }
}

</script>


How to display values of array inside the div or table tag ???

解决方案

orderArray's items represent <tr> elements, and each item inside represents a <td> element. So you can loop through orderArray creating <tr>s, and then loop through its elements on each loop creating <td>s: http://jsfiddle.net/h7F7e/.

var table = document.getElementById("table");  // set this to your table

var tbody = document.createElement("tbody");
table.appendChild(tbody);
orderArray.forEach(function(items) {
  var row = document.createElement("tr");
  items.forEach(function(item) {
    var cell = document.createElement("td");
    cell.textContent = item;
    row.appendChild(cell);
  });
  tbody.appendChild(row);
});

这篇关于如何在&lt; table&gt;内部显示数组值标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆