简体   繁体   English

如何将对象从数组显示到HTML表中

[英]How to display objects from array into HTML table

enter image description here I am working on a Shopping Cart problem and I have a table in HTML to input various objects in an Array. 在这里输入图像描述我正在处理购物车问题,并且我有HTML表格可以在数组中输入各种对象。 I want to create a "edit" button to display the specific array info back on the table. 我想创建一个“编辑”按钮以将特定的阵列信息显示回表中。

I am working on just Visual Studio Code and Javascript. 我只在Visual Studio Code和Javascript上工作​​。 No other tools used as this is the fundamentals for my course. 没有其他工具可以使用,因为这是我课程的基础。

So what should I code in editAdminFunc to display the objects in HTML? 那么我应该在editAdminFunc中编写什么代码以显示HTML中的对象?

Please see my code for info. 请参阅我的代码以获取信息。

 // create a flower array var flowerArray=[]; // declare a member object function flower(id, name, desc, price, stock, image){ this.id = id; //id this.name = name; //name this.desc= desc; //description this.price = price; //price this.stock = stock; //quantity this.image = image; //image } function addFlower(){ alert("addFlower function is being triggered!"); var newFlower = new flower( document.getElementById("memId").value, document.getElementById("memName").value, document.getElementById("memDesc").value, document.getElementById("memPrice").value, document.getElementById("memStock").value, document.getElementById("memImage").src, ); flowerArray.push(newFlower); console.log(flowerArray); } //this function is to show the flowers in list format (for admin view) function listFlowerFunc(){ alert("listFlowerFunc is being triggered!"); var displayText = "<table border=1, align = center>"; displayText += "<tr><th>Product ID</th><th>Name</th><th>Description</th><th>Unit Price</th><th>Stock</th><th>Image</th><th>Actions</th></tr>"; for (var i=0; i<flowerArray.length; i++){ displayText = displayText + "<tr><td>"+flowerArray[i].id+"</td><td>"+flowerArray[i].name+"</td><td>"+flowerArray[i].desc+"</td><td>"+flowerArray[i].price+"</td><td>"+flowerArray[i].stock+"</td><td>"+flowerArray[i].src+"</td><td><button onclick =editAdminFunc()>edit</button><button onclick =deleteAdminFunc()>delete</button></td></tr>" } displayText = displayText+"</table>"; document.getElementById("productlist").innerHTML = displayText; } //editAdminFunc function editAdminFunc(i){ alert("editAdminFunc() has been triggered!") document.getElementById("memId").value = ""; //to display array objects in the form document.getElementById("memName").value = ""; document.getElementById("memDesc").value= ""; document.getElementById("memPrice").value= ""; document.getElementById("memStock").value= ""; document.getElementById("memImage").src= ""; } 
 <div id="admin" align="center"> <p> <button onclick='showProductForm()'>Add Products</button> | <button id ="flowerListId" onclick="listFlowerFunc()">List Products</button> </p> <div id="productlist"> </div> <br> <div id="productForm" align="center"> <table id = "prodTable" border ="1"> <tr><td align="right">Product ID:</td><td><input type="text" id="memId" size="35"/></td></tr> <tr><td align="right">Name:</td><td><input type="text" id="memName" size="35"/></td></tr> <tr><td align="right">Description:</td><td><input type="text" id="memDesc" size="35"/></td></tr> <tr><td align="right">Unit Price($):</td><td><input type="text" id="memPrice" size="35"/></td></tr> <tr><td align="right">Stock:</td><td><input type="text" id="memStock" size="35"/></td></tr> <tr><td align="right">Image:</td><td><input type="file" id="memImage"/></td></tr> <tr><td>&nbsp</td> <td><button id="clearFormBtn" onclick="clearFormFunc()">Clear</button>&nbsp;&nbsp; <button id="addProdBtn" onclick="addFlower()">Add</button>&nbsp;&nbsp; <button id="updateProdBtn" onclick="editHtmlTableSelected()">Update</button> </td></tr> </table> </div> </div> 

the right way to create an element in javascript, would be something like that 在javascript中创建元素的正确方法是这样的

<div class="some"></div>


function addElement () { 
  // create new "p" element
  var newP = document.createElement("p"); 
  // add content --  the data you want display
  var newContent = document.createTextNode("hello, how are you?"); 

  newP.appendChild(newContent); //add content inside the element. 

  // add the element and content to DOM 
  var currentDiv = document.getElementById("some"); 
  document.body.insertBefore(newP, currentDiv); 
}
addElement();

https://developer.mozilla.org/es/docs/Web/API/Document/createElement https://developer.mozilla.org/es/docs/Web/API/Document/createElement

Now, if we adapt that information to the context of the tables, we can do it on this way to generate the data dynamically 现在,如果我们使信息适应表的上下文,则可以通过这种方式进行操作,以动态生成数据

   arr = [
  'item 1',
  'item 2',
  'item 3',
  'item 4',
  'item 5'
         ];

function addElement () { 

arr.forEach(function(el,index,array){
  let tableRef = document.getElementById('some');

  // Insert a row at the end of the table
  let newRow = tableRef.insertRow(-1);

  // Insert a cell in the row at index 0
  let newCell = newRow.insertCell(0);

  // Append a text node to the cell
  let newText = document.createTextNode(el);
  newCell.appendChild(newText);
});

}
addElement();

https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement/insertCell https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLTableRowElement/insertCell

link to demo: Fiddle 演示链接: 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM