javascript - Display Content Underscore js -
i trying display data within table using underscore.js.
i have div container class here:
<div id="container"></div>
now on windows load added event listener:
window.addeventlistener("load", function(event){ let content = document.getelementbyid('container'); _.each(students, function(elem, index, list){ _.extend(elem, {overall_score : (elem.midterm_score + elem.final_score) / 2}); let items = '<table border="1"><thead><tr><th>last name</th><th>first name</th><th>school</th><th>grade</th><th>overall score</th></tr></thead><tbody><tr class="studentrec"><td class="lastname">' + elem.lastname + '</td><td class="firstname">' + elem.firstname + '</td><td class="school">' + elem.school + '</td><td class="grade">' + elem.grade + '</td><td class="overall-score">' + elem.overall_score + '</td></tr></tbody>'; content.innerhtml += items; }); });
so added of records in items variable , put them within table layout. extended specific column w/c overall score. instead of plain table, got this:
as can see table header gets repeated each time. think missing here create perfect table?
because, output many table each record...
try this:
window.addeventlistener("load", function(event){ let content = document.getelementbyid('container'); let table = document.createelement('table'); let thead = document.createelement('thead'); let tbody = document.createelement('tbody'); let tr; thead.innerhtml = '<tr><th>last name</th><th>first name</th><th>school</th><th>grade</th><th>overall score</th></tr>'; _.each(students, function(elem, index, list){ _.extend(elem, {overall_score : (elem.midterm_score + elem.final_score) / 2}); tr = document.createelement('tr'); tr.classname = 'studentrec'; tr.innerhtml = '<td class="lastname">' + elem.lastname + '</td><td class="firstname">' + elem.firstname + '</td><td class="school">' + elem.school + '</td><td class="grade">' + elem.grade + '</td><td class="overall-score">' + elem.overall_score + '</td>'; tbody.appendchild(tr); }); table.border = 1; table.appendchild(thead); table.appendchild(tbody); content.appendchild(table); });
Comments
Post a Comment