Я пытаюсь передать объект массива 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);