我有一个如下的JSON数组:
[
{
"_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表。当前,我正在使用以下代码填充表,但是它将每个字段(而不是我想要的字段)都放在表中。
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>
有什么想法我将如何实现?
答案 0 :(得分:0)
您需要filter数组仅包含要插入表中的列。之后,您可以使用以下内容进行过滤:
col = col.filter((data) => {
return data === 'title' || data === 'content' || data == 'category';
});
或者您可以使用如下简单的if语句在for循环中进行过滤:
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);
}
}
}
答案 1 :(得分:0)
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 res = data.map(({title, content, category})=>{
return `<tr>
<td>${title}</td>
<td>${content}</td>
<td>${category}</td>
</tr>`;
}).join("");
const table = document.createElement("table");
table.innerHTML = "<tr><th>title</th><th>content</th><th>category</th></tr>";
table.innerHTML += res;
document.body.appendChild(table);
table,tr,th,td {
border: 1px solid black;
border-collapse: collapse;
}
th {
color: white;
background-color: grey;
}
td,th {
padding: 10px 5px;
}
答案 2 :(得分:0)
const axios = { get: (s) => Promise.resolve({ 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 }] }) };
function getSelectColumns() {
const selected = document.querySelectorAll('#headers option:checked');
return Array.from(selected).reduce((all, el) => {
return { ...all, [el.value]: el.text }
}, {});
}
// ------------------------------------------
function createTable(data, columns = {
title: 'Title', content: 'Content', category: 'Category'
}) {
const headerCells = Object.values(columns).map(val => `<th>${val}</th>`);
const headerRow = `<tr>${headerCells.join('')}</tr>`;
const dataRows = data.map(obj => {
return Object.keys(columns).map(key => {
return `<td>${obj[key] || '-'}</td>`;
}).join('');
});
return `<table>${headerRow + dataRows.join('')}</table>`;
}
function loadDataAndTable() {
return axios.get('http://localhost:2672/categories').then(res => {
const div = document.getElementById("showCategories");
div.innerHTML = createTable(res.data, getSelectColumns());
});
}
window.addEventListener('load', () => loadDataAndTable());
table {
text-align: left;
}
th, td {
padding: 5px 10px;
}
th {
background: grey;
color: white;
}
<div id="showCategories"></div>
<hr>
<p>Hold shift and select the columns you want in the table.</p>
<select id="headers" multiple onchange="loadDataAndTable()">
<option value="title" checked>Title</option>
<option value="content" checked>Content</option>
<option value="category" checked>Category</option>
</select>