2 回答
TA贡献1951条经验 获得超3个赞
在javascript中,afaik,“数组”和你在那里拥有的东西之间存在区别,我们称之为“对象”。在 PHP 中,我想它们都被称为“数组”,当你有数组值的键时,它被称为“关联数组”,但在 Javascript 中,我们称这些“对象”是为了将它们与看起来像的数组区分开来[1,2,3]
所以第一步是将你的对象真正变成一个数组。我会做这样的事情:
var obj = {
"15800175": {
"posID": "159469",
"scanID": "22597",
"anr": "15800175",
"TVanr": "",
"code": "G-09-27-2E",
"ean": "4710069680575",
"marke": "fox",
"bez": "jersey yellow",
"bez2": "size xl",
"menge": "1",
"ve": "St\u00fcck",
"Dimensionstext": "TT9857BG\r\n",
"Langtext": ""
},
"15100027": {
"posID": "159471",
"scanID": "22597",
"anr": "15100027",
"TVanr": "",
"code": "G-11-03-2A",
"ean": "4712511825411",
"marke": "fox",
"bez2": "jersey L",
"menge": "1",
"ve": "St\u00fcck",
"Dimensionstext": "blue\r\n",
"Langtext": ""
}
}
var arr = Object.keys(obj).map(key => obj[key]);
最终arr看起来像:
[{"posID":"159471","scanID":"22597","anr":"15100027","TVanr":"","code":"G-11-03-2A","ean":"4712511825411","marke":"fox","bez2":"jersey L","menge":"1","ve":"Stück","Dimensionstext":"blue\r\n","Langtext":""},{"posID":"159469","scanID":"22597","anr":"15800175","TVanr":"","code":"G-09-27-2E","ean":"4710069680575","marke":"fox","bez":"jersey yellow","bez2":"size xl","menge":"1","ve":"Stück","Dimensionstext":"TT9857BG\r\n","Langtext":""}]
然后,您可以arr使用 forEach 回调遍历每个对象,arr.forEach(item => {});并根据需要渲染它们。
TA贡献1852条经验 获得超1个赞
这里使用@Tkol 方法是一种将元素 1 一 1 附加到 html 表的解决方案;
html:
var obj = {
"15800175": {
"posID": "159469",
"scanID": "22597",
"anr": "15800175",
"TVanr": "",
"code": "G-09-27-2E",
"ean": "4710069680575",
"marke": "fox",
"bez": "jersey yellow",
"bez2": "size xl",
"menge": "1",
"ve": "St\u00fcck",
"Dimensionstext": "TT9857BG\r\n",
"Langtext": ""
},
"15100027": {
"posID": "159471",
"scanID": "22597",
"anr": "15100027",
"TVanr": "",
"code": "G-11-03-2A",
"ean": "4712511825411",
"marke": "fox",
"bez2": "jersey L",
"menge": "1",
"ve": "St\u00fcck",
"Dimensionstext": "blue\r\n",
"Langtext": ""
}
}
var counter = 0;
var arr = Object.keys(obj).map(key => obj[key]);
document.getElementById("btnAdd").onclick = function(event){
//variables
let tbody = document.getElementById("tbodyExample");
//logic
let element = arr[counter] != undefined ? arr[counter] : undefined;
if(element !== undefined){
var row = document.createElement('tr');//'<tr id="'+element.posID+'"></tr>';
row.setAttribute("id",element.posID);
row.innerHTML = '<td>'+element.posID+'</td><td>'+element.code+'</td><td>'+(element.bez == undefined? element.bez2 : element.bez)+'</td><td>'+element.anr+'</td><td>'+element.menge+'</td><td>'+element.ve+'</td>';
tbody.appendChild(row);
counter++;
}else{
alert("No more items found");
}
}
<h2>Table example</h2>
<table id="example" border=1>
<thead>
<th>Post</th>
<th>code</th>
<th>bez</th>
<th>anr</th>
<th>qty</th>
<th>ve</th>
</thead>
<tbody id="tbodyExample">
</tbody>
</table>
<br>
<input type="button" id="btnAdd" value="add Row">
希望能帮助到你
添加回答
举报