In: Computer Science
In this assignment, you will use regular JavaScript and the Fetch API to read an external JSON data file from the server and then add the data from each student object into new rows in an existing HTML table.
This assignment is very similar to the Adding Rows to a Table assignment. The main difference is that you will:
Set Up This Assignment
Add this HTML to your "week11/index.html" file. Put your name in the meta author tag (highlighted below).
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Using Fetch to read a JSON file</title> <meta name="description" content="This is a simple example using Fetch to read a JSON string then add student data as rows in an HTML table."> <meta name="author" content="Your name here"> <!-- link to external CSS file --> <link rel="stylesheet" href="css/styles.css?v=1.0"> </head> <body> <!-- Content of the page goes here. --> <main> <h1>Use Fetch to read JSON data</h1> <section class="intro"> <h2>Introduction</h2> <p>In this assignment you will use Fetch to read in an external JSON data file. You will then display the contents in a web page as new rows in an existing table. </p> </section> <section class="student-info"> <h2>Student Information from JSON file</h2> <p class="loading">Loading student data...</p> <table id="student-table"> <thead> <tr><th scope="col">Name</th><th scope="col">Favorite Hobby</th><th scope="col">Favorite Color</th></tr> </thead> <tbody> </tbody> </table> </section> </main> <!-- link to external JS file --> <script src="js/scripts.js"></script> </body> </html>
JSON Data
You will also use the same students.json file that you used in the
week9 assignment. Put the JSON file in a "data" subdirectory:
"week11/data/students.json"
CSS Stylesheet
You will also use the same CSS file that you used in the week9
assignment. Put the CSS file in a "css" subdirectory:
"week11/css/styles.css"
JavaScript
Your "week11/js/scripts.js" file
will use the fetch API to read an external JSON data file, then add
student data as rows to an existing table. This page contains
sample code that uses the fetch API: Fetch API: An Example
This displayData function needs to do these things:
Note:
for json file, I have used the sample.json file (replace with your own json file)
code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Using Fetch to read a JSON file</title>
<meta name="description" content="This is a simple example using
Fetch to read a
JSON string then add student data as rows in an HTML
table.">
<meta name="author" content="Your name here">
<!-- link to external CSS file -->
<link rel="stylesheet" href="css/styles.css?v=1.0">
<style>
.loadingHidden{
display:none;
}
</style>
</head>
<body>
<!-- Content of the page goes here. -->
<main>
<h1>Use Fetch to read JSON data</h1>
<section class="intro">
<h2>Introduction</h2>
<p>In this assignment you will use Fetch to read in an
external JSON data file.
You will then display the contents in a web page as new rows in an
existing table.
</p>
</section>
<section class="student-info">
<h2>Student Information from JSON file</h2>
<p class="loading">Loading student data...</p>
<table id="student-table">
<thead>
<tr><th scope="col">Name</th><th
scope="col">Favorite Hobby</th><th
scope="col">Favorite Color</th></tr>
</thead>
<tbody>
</tbody>
</table>
</section>
</main>
<!-- link to external JS file -->
<script src="js/scripts.js"></script>
<script>
var students=[];
var
table=document.getElementById('student-table').getElementsByTagName('tbody')[0];
fetch('sample.json').then(function(response) {
return response.json();
})
.then(function(res) {
students=res;
for(var i=0;i<students.length;i++)
{
var fragment =
document.createDocumentFragment();
var tr =
document.createElement("TR");
for(var j=0;j<3;j++)
{
var td =
document.createElement("TD");
if(j==0)
{
var name=students[i].last_name +"
"+students[i].first_name;
var data = document.createTextNode(name);
}
else
if(j==1)
{
var data =
document.createTextNode(students[i].favorite_hobby);
}
else
{
var data =
document.createTextNode(students[i].favorite_color);
}
td.appendChild(data)
tr.appendChild(td);
}
fragment.appendChild(tr);
table.appendChild(fragment);
}
var
loading_cls=document.querySelector(".loading");
document.querySelector(".loading").setAttribute("class","loadingHidden");
});
</script>
</body>
</html>