使用JSON数组中的某些项填充HTML表 [英] Populate HTML Table with Certain Items from a JSON Array

查看:107
本文介绍了使用JSON数组中的某些项填充HTML表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的JSON数组:

I have a JSON array like the following:

[
{
    "_id": "5c3296f4c7728833bf7cf334",
    "title": "Update styling",
    "content": "Don't forget to update the styling on the website",
    "category": "Cat2",
    "createdAt": "2019-01-07T00:01:56.375Z",
    "updatedAt": "2019-01-07T00:01:56.375Z",
    "__v": 0
}
]

我想用标题",内容"和类别"字段填充html表.当前,我正在使用以下代码填充表,但是它将每个字段(而不是我想要的字段)都放在表中.

I want to populate a html table with the Title, Content and Category fields. Currently, I am using the following code to populate the table, but it puts every field in the table, not the ones I want.

const data = [{ "_id": "5c3296f4c7728833bf7cf334", "title": "Update styling", "content": "Don't forget to update the styling on the website", "category": "Cat2", "createdAt": "2019-01-07T00:01:56.375Z", "updatedAt": "2019-01-07T00:01:56.375Z", "__v": 0 }]
const axios = { get: (s) => Promise.resolve({ data }) };

window.addEventListener('load', function() {
  axios.get('http://localhost:2672/categories').then(function (myCategories) {
      // EXTRACT VALUE FOR HTML HEADER. 
      var col = [];
      for (var i = 0; i < myCategories.data.length; i++) {
          for (var key in myCategories.data[i]) {
              if (col.indexOf(key) === -1) {
                  col.push(key);
              }
          }
      }

      // CREATE DYNAMIC TABLE.
      var table = document.createElement("table");

      // CREATE HTML TABLE HEADER ROW USING THE EXTRACTED HEADERS ABOVE.

      var tr = table.insertRow(-1); // TABLE ROW.

      for (var i = 0; i < col.length; i++) {
          var th = document.createElement("th"); // TABLE HEADER.
          th.innerHTML = col[i];
          tr.appendChild(th);
      }

      // ADD JSON DATA TO THE TABLE AS ROWS.
      for (var i = 0; i < myCategories.data.length; i++) {

          tr = table.insertRow(-1);

          for (var j = 0; j < col.length; j++) {
              var tabCell = tr.insertCell(-1);
              tabCell.innerHTML = myCategories.data[i][col[j]];
          }
      }

      // FINALLY ADD THE NEWLY CREATED TABLE WITH JSON DATA TO A CONTAINER.
      var divContainer = document.getElementById("showCategories");
      divContainer.innerHTML = "";
      divContainer.appendChild(table);
  });
});

<div id="showCategories"></div>

有什么想法我将如何实现吗?

Any ideas how I would accomplish this?

推荐答案

您需要

You need to filter your array to only contain the columns you wish to insert into the table. You could filter afterwards with something like:

col = col.filter((data) => {
    return data === 'title' || data === 'content' || data == 'category';
});

或者您可以使用如下简单的if语句在for循环中进行过滤:

or you could filter within your for-loop with a simple if-statement like so:

var col = [];
for (var i = 0; i < myCategories.data.length; i++) {
    for (var key in myCategories.data[i]) {
        if (col.indexOf(key) === -1 && (key === 'title' || key === 'content' || key == 'category')) {
            col.push(key);
        }
    }
}

这篇关于使用JSON数组中的某些项填充HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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