我正在尝试将json数组对象作为工作表从表传递到我的服务器,作为表格。我无法使循环正常工作。
我尝试了一个循环,该循环在控制台中创建了一堆空表。肯定有问题。 JSON数组是:
{
"degrees":[
{
"degree":{
"School": "Dutchess Community College",
"Program": "Humanities",
"Type": "Associate of Arts",
"Year": "2009"
}
},
{
"degree":{
"School": "Binghamton University",
"Program": "English Literature",
"Type": "Bachelor of Arts",
"Year": "2011"
ETC。
所需的输出是一个简单的表,因此:
School | Program | Type |Year
dutchesscommunity college Humanities associate of arts 2009
Binghamton English Lit Bachelor of arts 2011
const getDegrees = () => {
const xhr = new XMLHttpRequest();
xhr.open('get', 'data/degrees.json', true);
xhr.onreadystatechange = function() {
if ( (xhr.readyState === 4) && (xhr.status === 200) ) {
const response = JSON.parse(xhr.responseText);
const table = document.createElement("TABLE");
table.setAttribute("id", "myTable");
document.body.appendChild(table);
var degrees = response.degrees;
var degreeHeaders = Object.keys(degrees[0].degree);
var headerRow = document.createElement("TR");
headerRow.setAttribute("id", "myTr");
table.appendChild(headerRow);
for (let i = 0; i < degreeHeaders.length; i++) {
const headerCell = document.createElement("th");
headerCell.innerText = degreeHeaders[i];
headerRow.appendChild(headerCell);
}
for (let i = 0; i < degrees.length; i++) {
let degreeData = degrees[i].degree;
let tr = document.createElement("TR");
table.appendChild(tr);
for (let column = 0; column < degreeHeaders.length; column++) {
const columnProperty = degreeHeaders[column];
let td = document.createElement("td");
tr.appendChild(td);
td.innerHTML = degreeData[columnProperty];
}
}
}
}
};
const btn = document.querySelector('button');
btn.addEventListener('click', getDegrees);
完全
const btn = document.querySelector('button');
const getDegrees = () => {
const xhr = new XMLHttpRequest();
xhr.open('get', 'data/degrees.json', true);
xhr.onreadystatechange = function() {
if ( (xhr.readyState === 4) && (xhr.status === 200) ) {
const response = JSON.parse(xhr.responseText);
function createTr(line, i) {
var y = document.createElement("TR");
y.setAttribute("id", "myTr" + i);
var school = document.createElement("TD");
school.innerHTML = line['School'];
var program = document.createElement("TD");
program.innerHTML = line['Program'];
var type = document.createElement("TD");
type.innerHTML = line['Type'];
var year = document.createElement("TD");
year.innerHTML = line['Year'];
y.appendChild(school);
y.appendChild(program);
y.appendChild(type);
y.appendChild(year);
return y;
}
function getDegrees() {
var x = document.createElement("TABLE");
x.setAttribute("id", "myTable");
document.body.appendChild(x);
x.append(createTr({"School": "School",
"Program": "Program",
"Type": "Type",
"Year": "Year" }, 0));
for (var i = 0; i < response['degrees'].length; i++) {
var line = response['degrees'][i]['degree'];
var tr = createTr(line, i);
x.appendChild(tr);
}
};
}
}
}
btn.addEventListener('click', getDegrees);
答案 0 :(得分:0)
一个选择是基于第一个学位的属性来获取“列”名称的列表。 (假设所有度数都具有与该行相同的属性)
创建“标题行”并将这些属性添加为“标题单元格”。
然后,遍历每个度数,为度数添加一行,并为每个标题列添加一个单元格,并为该属性添加度数。
请参见下面的示例(它切掉了您的javascript请求部分,但应具有相同的行为。
<html>
<head>
</head>
<body>
<button>Load Data</button>
<script type="text/javascript">
function getDegrees() {
const response = {
"degrees": [
{
"degree": {
"School": "Dutchess Community College",
"Program": "Humanities",
"Type": "Associate of Arts",
"Year": "2009"
}
},
{
"degree": {
"School": "Binghamton University",
"Program": "English Literature",
"Type": "Bachelor of Arts",
"Year": "2011"
}
}
]
};
// Create the table
const table = document.createElement("TABLE");
table.setAttribute("id", "myTable");
document.body.appendChild(table);
// Get the firest degree element, and get the headers from it
var degrees = response.degrees;
var degreeHeaders = Object.keys(degrees[0].degree); // i.e. this will be an array of ["School", "Program", "Type" "Year"]
// Create a row for the headers
var headerRow = document.createElement("TR");
headerRow.setAttribute("id", "myTr");
table.appendChild(headerRow);
// Add a cell for each of the 'columns' based on the properties that we will display
for (let i = 0; i < degreeHeaders.length; i++) {
const headerCell = document.createElement("th");
headerCell.innerText = degreeHeaders[i];
headerRow.appendChild(headerCell);
}
// Loop over each of the degree elements
for (let i = 0; i < degrees.length; i++) {
// Build a row element and add it to the table
let degreeData = degrees[i].degree;
let tr = document.createElement("TR");
table.appendChild(tr);
// For each 'Header' property, build a cell and add it to the current degree's row
for (let column = 0; column < degreeHeaders.length; column++) {
const columnProperty = degreeHeaders[column]; // This is the propery for the column i.e. ["School", "Program", "Type" "Year"]
let td = document.createElement("td");
tr.appendChild(td);
td.innerHTML = degreeData[columnProperty];
}
}
}
const btn = document.querySelector('button');
btn.addEventListener('click', getDegrees);
</script>
</body>
</html>
答案 1 :(得分:0)
这是您想要的简单解决方案:
var json = {
"degrees":[
{
"degree":{
"School": "Dutchess Community College",
"Program": "Humanities",
"Type": "Associate of Arts",
"Year": "2009"
}
},
{
"degree":{
"School": "Binghamton University",
"Program": "English Literature",
"Type": "Bachelor of Arts",
"Year": "2011"
}
} ]
};
function createTr(line, i) {
var y = document.createElement("TR");
y.setAttribute("id", "myTr" + i);
var school = document.createElement("TD");
school.innerHTML = line['School'];
var program = document.createElement("TD");
program.innerHTML = line['Program'];
var type = document.createElement("TD");
type.innerHTML = line['Type'];
var year = document.createElement("TD");
year.innerHTML = line['Year'];
y.appendChild(school);
y.appendChild(program);
y.appendChild(type);
y.appendChild(year);
return y;
}
function getDegrees() {
var response = json;
var x = document.createElement("TABLE");
x.setAttribute("id", "myTable");
document.body.appendChild(x);
x.append(createTr({"School": "School",
"Program": "Program",
"Type": "Type",
"Year": "Year" }, 0));
for (var i = 0; i < response['degrees'].length; i++) {
var line = response['degrees'][i]['degree'];
var tr = createTr(line, i);
x.appendChild(tr);
}
};
btn.addEventListener('click', getDegrees);
<div id='res'>
</div>
<button id="btn" >Get Degrees</button>