Situation:
You have a record set. You want to show it to the user.
Actions:
Create HTML that uses the <table>
tag. Use .forEach()
to loop across the record set. Create HTML for each record. Accumulate the HTML.
Explanation:
Create HTML that uses the <table>
tag. Use .forEach()
to loop across the record set. Create HTML for each record. Accumulate the HTML.
Here's a record set:
var catalog = [
{
id: 42,
title: "Web apps for happy dogs",
maxEnrollment: 40,
location: "Monty Hall 301"
},
{
id: 155,
title: "Databases for happy dogs",
maxEnrollment: 32,
location: "Monty Hall 301"
},
...
];
Here's a loop to show it:
var catalogHtml =
"<table class='table'>" +
" <thead>" +
" <th>Title</th>" +
" <th>Max enroll</th>" +
" <th>Location</th>" +
" </thead>" +
" <tbody>";
catalog.forEach(function(course){
var courseRow =
"<tr>" +
" <td>" + course.title + "</td>" +
" <td>" + course.maxEnrollment + "</td>" +
" <td>" + course.location + "</td>" +
"</tr>";
catalogHtml += courseRow;
});
catalogHtml += "</tbody></table>";
$("#catalogContainer").html(catalogHtml);