简体   繁体   English

如何使用 JavaScript 数据使用 JSON 数据创建 HTML 表

[英]How to create a HTML table using JSON data with JavaScript

I am currently working on a website that imports JSON data and populates a table within HTML.我目前正在开发一个导入 JSON 数据并在 HTML 中填充表的网站。
I am having trouble iterating through the arrays to show all of the data in the table.我无法遍历 arrays 以显示表中的所有数据。 Currently, only some of the JSON data appears in the table while the other data is just shown as text outside the table.目前,表中仅显示 JSON 的部分数据,而其他数据仅显示为表外的文本。

I have tried using the keys from the JSON data as table headers but I only require a select few headers.我尝试使用 JSON 数据中的键作为表头,但我只需要 select 几个表头。

 var table = { "Employees": [ { "Started": "2016", "Department": "Engineering", "Employee": [ { "id": "a101", "firstname": "Alan", "surname": "Arkin" }, { "id": "a102", "firstname": "Geoff", "surname": "keegan" } ] }, { "Started": "2016", "Department": "R&D", "Employee": [ { "id": "a103", "firstname": "Michele", "surname": "Jones" }, { "id": "a104", "firstname": "Peter", "surname": "Smith" } ] } ] } var Employees = table.Employees // This code iterates through the colorArray and writes html code to put the color information in a table. var colorInfo = "<table>"; colorInfo += "<tr><th>Started</th><th>Department</th><th>ID</th><th>Name</th></tr>"; for (var i = 0; i < Employees.length; i++) { var started = Employees[i].Started; var Department = Employees[i].Department; var EmployeeArray = Employees[i].Employee; for (var j = 0; j < EmployeeArray.length; j++) { var Emp = EmployeeArray[j] var id = Emp.id var names = Emp.firstname + " " + Emp.surname colorInfo += "<tr><td>" + started + "</td><td>" + Department + "</td><td>" + id + "</td><td>" + names + "</td></tr>"; } // Close the table element. colorInfo += "</table>"; // Add the new html code to the div element with id = 'id01'. document.getElementById("id01").innerHTML = colorInfo; }
 body { font-size: 16px; font-family: Arial, Helvetica, sans-serif; } Table { border-collapse: collapse; } td { border: 1px solid grey; padding: 2px 10px; } th { background-color: turquoise;}
 <div id="id01">...</div>

I expect that all of the data should appear in the table here.我希望所有数据都应出现在此处的表格中。

Do not use the .innerHTML method, it is the best way to have problems.不要使用.innerHTML方法,这是最好的解决问题的方法。 There are many ways to create DOM in javascript, which in addition simplifies the interpreter, close automatically each tags on the right place and gives faster code execution.在 javascript 中有多种创建 DOM 的方法,此外还简化了解释器,在正确的位置自动关闭每个标签,并提供更快的代码执行速度。

.innerHTML is a devil ! .innerHTML是个魔鬼! (and prefer .textContent for simple text contents) (并且更喜欢.textContent用于简单的文本内容)

example:例子:

 const table = { "Employees": [ { "Started": "2016", "Department": "Engineering", "Employee": [ { "id": "a101", "firstname": "Alan", "surname": "Arkin" }, { "id": "a102", "firstname": "Geoff", "surname": "keegan" } ] }, { "Started": "2016", "Department": "R&D", "Employee": [ { "id": "a103", "firstname": "Michele", "surname": "Jones" }, { "id": "a104", "firstname": "Peter", "surname": "Smith" } ] } ] } let MyTable = document.querySelector('#id01').appendChild(document.createElement('table')) for (let StartDep of table.Employees) { for (let Employee of StartDep.Employee ) { let nRow = MyTable.insertRow(-1), rCell = 0 nRow.insertCell(rCell++).textContent = StartDep.Started nRow.insertCell(rCell++).textContent = StartDep.Department nRow.insertCell(rCell++).textContent = Employee.id nRow.insertCell(rCell++).textContent = `${Employee.firstname} ${Employee.surname}` } } let Rowhead = MyTable.createTHead().insertRow(-1) 'Started,Department,ID,Name'.split(',').forEach((T,i)=>Rowhead.insertCell(i).textContent=T)
 body { font-size: 16px; font-family: Arial, Helvetica, sans-serif; } Table { border-collapse: collapse; } td { border: 1px solid grey; padding: 2px 10px; } thead { background-color: turquoise;}
 <div id="id01"></div>

Your table isn't displaying properly because you have the 'close table element' and 'add to div element' statements inside the first Employees for-loop.您的表格无法正确显示,因为您在第一个员工 for 循环中有“关闭表格元素”和“添加到 div 元素”语句。 Move those outside the loop and it will work.将它们移到循环之外,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM