单击按钮时循环遍历数组

我有一个数组,我通过 json_encode() 放入我的文件中;来自我的 .php 文件。该数组如下所示:


{

  "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": ""

  },

例如,我想在看起来像这样的表中显示值


Pos | code | bez | anr | qty | ve

我希望它首先显示数组的第一个位置,这将等于 15800175,对吗?然后当我按下“下一步”按钮时,它应该切换 div,隐藏第一个位置并显示第二个位置的值。


我如何通过一键按下来显示相应的值?真的坚持这一点。谢谢!


至尊宝的传说
浏览 285回答 2
2回答

饮歌长啸

在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 => {});并根据需要渲染它们。

小怪兽爱吃肉

这里使用@Tkol 方法是一种将元素 1 一 1 附加到 html 表的解决方案;html:var obj = {&nbsp; "15800175": {&nbsp; &nbsp; "posID": "159469",&nbsp; &nbsp; "scanID": "22597",&nbsp; &nbsp; "anr": "15800175",&nbsp; &nbsp; "TVanr": "",&nbsp; &nbsp; "code": "G-09-27-2E",&nbsp; &nbsp; "ean": "4710069680575",&nbsp; &nbsp; "marke": "fox",&nbsp; &nbsp; "bez": "jersey yellow",&nbsp; &nbsp; "bez2": "size xl",&nbsp; &nbsp; "menge": "1",&nbsp; &nbsp; "ve": "St\u00fcck",&nbsp; &nbsp; "Dimensionstext": "TT9857BG\r\n",&nbsp; &nbsp; "Langtext": ""&nbsp; },&nbsp; "15100027": {&nbsp; &nbsp; "posID": "159471",&nbsp; &nbsp; "scanID": "22597",&nbsp; &nbsp; "anr": "15100027",&nbsp; &nbsp; "TVanr": "",&nbsp; &nbsp; "code": "G-11-03-2A",&nbsp; &nbsp; "ean": "4712511825411",&nbsp; &nbsp; "marke": "fox",&nbsp; &nbsp; "bez2": "jersey L",&nbsp; &nbsp; "menge": "1",&nbsp; &nbsp; "ve": "St\u00fcck",&nbsp; &nbsp; "Dimensionstext": "blue\r\n",&nbsp; &nbsp; "Langtext": ""&nbsp; }}var counter = 0;var arr = Object.keys(obj).map(key => obj[key]);document.getElementById("btnAdd").onclick = function(event){//variableslet tbody = document.getElementById("tbodyExample");//logiclet element&nbsp; = arr[counter] != undefined ? arr[counter] : undefined;if(element !== undefined){var row = document.createElement('tr');//'<tr id="'+element.posID+'"></tr>';&nbsp; row.setAttribute("id",element.posID);&nbsp; 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>';&nbsp; tbody.appendChild(row);&nbsp; counter++;}else{alert("No more items found");}}<h2>Table example</h2><table id="example" border=1>&nbsp; <thead>&nbsp; &nbsp; <th>Post</th>&nbsp; &nbsp; <th>code</th>&nbsp; &nbsp; <th>bez</th>&nbsp; &nbsp; <th>anr</th>&nbsp; &nbsp; <th>qty</th>&nbsp; &nbsp; <th>ve</th>&nbsp; </thead>&nbsp; <tbody id="tbodyExample">&nbsp; &nbsp;&nbsp;&nbsp; </tbody></table><br><input type="button" id="btnAdd" value="add Row">希望能帮助到你
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript