如何创建用Javascript数组填充HTML表的循环

时间:2019-09-17 12:28:10

标签: javascript html arrays html-table populate

我的任务是使用javascript数组填充HTML表,但是在理解如何获取数组以填充实际的HTML表方面遇到了很多麻烦。我必须创建一个包含5个菜单项图像,5个菜单项描述及其5个价格的表。我在下面提供了我的数组,但是不知道我需要执行哪些函数来获取数组以填充HTML表。您如何建议我将其填充到HTML表中?

此外,根据作业,我必须使用数字数组存储价格,并使用字符串数组存储图像文件。

我认为我必须执行某种“ for”循环,但是我不确定。

感谢您的所有帮助,请让我知道如何改进此问题。

谢谢

//my arrays

var item = ["Chicken soup", "Chicken tacos", "Chicken quesadilla", "Chicken burrito", "Chicken enchilada"];

var itemDescription = ["Delicious creamy chicken soup", "Homemade tacos", "Cheesy chicken quesadillas", "Hearty stuffed chicken burrito", "World famous chicken enchilada"];

var itemPrice = [14.99, 17.99, 15.75, 22.95, 12.55];

var imgArray = new Array();

imgArray[0] = new Image();
imgArray[0].src = 'chickensoup.jpg';

imgArray[1] = new Image();
imgArray[1].src = 'chickentaco.jpg';

imgArray[2] = new Image();
imgArray[2].src = 'chickenque.jpg';

imgArray[3] = new Image();
imgArray[3].src = 'chickenburrito.jpg';

imgArray[4] = new Image();
imgArray[4].src = 'chickenenchilada.jpg';

4 个答案:

答案 0 :(得分:1)

对于初学者,我建议将数组合并为一个,以避免有关索引的任何错误。

标准html格式为:

<table>
    <thead>
        <tr>
            <th>name</th>
            <th>description</th>
            <th>price</th>
            <th>imageSrc</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Chicken soup</td>
            <td>Delicious creamy chicken soup</td>
            <td>14.99</td>
            <td><img src="chickensoup.jpg" alt="chickensoup.jpg"></td>
        </tr>
    </tbody>
</table>

因此,您需要一个循环来打印所有标题。 您需要一个循环才能为每个项目创建不同的行。 您需要在行循环内的最后一个循环将每个单元格(食物属性)添加到行中。

var items = [{
  name: "Chicken soup",
  description: "Delicious creamy chicken soup",
  price: 14.99,
  imageSrc: "chickensoup.jpg"
}, {
  name: "Chicken tacos",
  description: "Homemade tacos",
  price: 17.99,
  imageSrc: "chickentaco.jpg"
}, {
  name: "Chicken quesadilla",
  description: "Cheesy chicken quesadillas",
  price: 15.75,
  imageSrc: "chickenque.jpg"
}, {
  name: "Chicken burrito",
  description: "Hearty stuffed chicken burrito",
  price: 22.95,
  imageSrc: "chickenburrito.jpg"
}, {
  name: "Chicken enchilada",
  description: "World famous chicken enchilada",
  price: 12.55,
  imageSrc: "chickenenchilada.jpg"
}];

var propertyList = ["name", "description", "price", "imageSrc"];

var table = document.createElement("table");
var tableHeader = document.createElement("thead");
var tableHeaderRow = document.createElement("tr");
var tableBody = document.createElement("tbody");

table.setAttribute("border", "1");

document.body.appendChild(table);
table.appendChild(tableHeader);
tableHeader.appendChild(tableHeaderRow);
table.appendChild(tableBody);

propertyList.forEach(function(key) {
  var headerCell = document.createElement("th");
  tableHeaderRow.appendChild(headerCell);
  headerCell.textContent = key;
});


items.forEach(function(foodItem) {
  var foodRow = document.createElement("tr");
  tableBody.appendChild(foodRow);
  propertyList.forEach(function(propertyName) {
    var foodProperty = document.createElement("td");
    foodRow.appendChild(foodProperty);
    if (propertyName === "imageSrc") {
      var image = document.createElement("img");
      foodProperty.appendChild(image);
      image.src = foodItem[propertyName];
      image.alt = foodItem[propertyName];
    } else {
      foodProperty.textContent = foodItem[propertyName];
    }
  });
});

如果无法合并数组,则可以使用它。

var item = ["Chicken soup", "Chicken tacos", "Chicken quesadilla", "Chicken burrito", "Chicken enchilada"];

var itemDescription = ["Delicious creamy chicken soup", "Homemade tacos", "Cheesy chicken quesadillas", "Hearty stuffed chicken burrito", "World famous chicken enchilada"];

var itemPrice = [14.99, 17.99, 15.75, 22.95, 12.55];

var imgArray = new Array();

imgArray[0] = new Image();
imgArray[0].src = 'chickensoup.jpg';

imgArray[1] = new Image();
imgArray[1].src = 'chickentaco.jpg';

imgArray[2] = new Image();
imgArray[2].src = 'chickenque.jpg';

imgArray[3] = new Image();
imgArray[3].src = 'chickenburrito.jpg';

imgArray[4] = new Image();
imgArray[4].src = 'chickenenchilada.jpg';

var foodList = {
  name: item,
  description: itemDescription,
  price: itemPrice,
  imageSrc: imgArray
};

var propertyList = ["name", "description", "price", "imageSrc"];

var table = document.createElement("table");
var tableHeader = document.createElement("thead");
var tableHeaderRow = document.createElement("tr");
var tableBody = document.createElement("tbody");

table.setAttribute("border", "1");

document.body.appendChild(table);
table.appendChild(tableHeader);
tableHeader.appendChild(tableHeaderRow);
table.appendChild(tableBody);

propertyList.forEach(function(key) {
  var headerCell = document.createElement("th");
  tableHeaderRow.appendChild(headerCell);
  headerCell.textContent = key;
});

for (var index = 0; index < item.length; index++) {

  var foodRow = document.createElement("tr");
  tableBody.appendChild(foodRow);
  propertyList.forEach(function(propertyName) {
    var foodProperty = document.createElement("td");
    foodRow.appendChild(foodProperty);
    if (propertyName === "imageSrc") {
      foodProperty.appendChild(foodList[propertyName][index]);
    } else {
      foodProperty.textContent = foodList[propertyName][index];
    }
  });
}

答案 1 :(得分:1)

尝试一下:

let itemObject = [];

itemObject.push({
    item: 'Chicken soup',
    description: 'Delicious creamy chicken soup',
    price: 14.99,
    image: 'chickensoup.jpg'
});
itemObject.push({
    item: 'Chicken tacos',
    description: 'Chicken taco',
    price: 17.99,
    image: 'chickentaco.jpg'
});
itemObject.push({
    item: 'Chicken quesadilla',
    description: 'Chicken quesadilla',
    price: 15.75,
    image: 'chickenque.jpg'
});
itemObject.push({
    item: 'Chicken burrito',
    description: 'Chicken burrito',
    price: 22.95,
    image: 'chickenburrito.jpg'
});
itemObject.push({
    item: 'Chicken enchilada',
    description: 'Chicken enchilada',
    price: 12.55,
    image: 'chickenenchilada.jpg'
});


let tbody = document.getElementById('tbody');

for (let rows of itemObject) {
    let tr = document.createElement('TR');
    let td, tdText;
    for (let value of Object.values(rows)) {
        td = document.createElement("TD");
        td.appendChild(document.createTextNode(value));
        tr.appendChild(td);
    }
    tbody.appendChild(tr);
};
<table>
   <thead>
      <tr>
         <td>Item</td>
         <td>Description</td>
         <td>Price</td>
         <td>Image</td>
      </tr>
   </thead>
   <tbody id="tbody">
   </tbody>
</table>

答案 2 :(得分:0)

查看此小提琴链接https://jsfiddle.net/j5mnto3g/2/

在HTML中创建表格

<table border="1">
  <thead>
    <tr><th>Item Image</th><th>Item Name</th><th>Description</th><th>Price</th></tr>
  </thead>
  <tbody id="tbl-body"> </tbody>
</table>

并通过简单的for循环在表中添加行

var item = ["Chicken soup", "Chicken tacos", "Chicken quesadilla", "Chicken burrito", 
            "Chicken enchilada"];

var itemDescription = ["Delicious creamy chicken soup", "Homemade tacos", "Cheesy chicken quesadillas", "Hearty stuffed chicken burrito", "World famous chicken enchilada"];

var itemPrice = [14.99, 17.99, 15.75, 22.95, 12.55];

var imgArray = new Array();

imgArray[0] = new Image();
imgArray[0].src = 'chickensoup.jpg';

imgArray[1] = new Image();
imgArray[1].src = 'chickentaco.jpg';

imgArray[2] = new Image();
imgArray[2].src = 'chickenque.jpg';

imgArray[3] = new Image();
imgArray[3].src = 'chickenburrito.jpg';

imgArray[4] = new Image();
imgArray[4].src = 'chickenenchilada.jpg';
var rows = '';
for(i = 0; i < item.length; i++){
    rows += '<tr><td><img src="'+imgArray[i]+'" width="20" height="20"></td><td>'+item[i]+'</td><td>'+itemDescription[i]+'</td><td>'+itemPrice[i]+'</td></tr>';
}
document.getElementById('tbl-body').innerHTML = rows;

使用正确的图像路径和大小

答案 3 :(得分:-1)

我不会显示整个代码,但这是在逻辑上做到这一点的最佳方法

//JS
let array = ["item 1", "item2"];

//start a string (of html)
let htmlDisplayed = "<div id='htmlString'><ul>";

//loop each array item as a list item
for (let i=0; i< array.length; i++) {
  htmlDisplayed = htmlDisplayed.concat(`<li>${array[i]}</li>`);
}

//close your html tags off
htmlDisplayed = htmlDisplayed.concat("</ul></div>");

//display it
document.getElementById("yourElement").innerHTML = htmlDisplayed;