Guest User

Untitled

a guest
Jan 19th, 2020
82
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let personer = [
  2. {navn: "Ole", alder: 23},
  3. {navn: "Kim", alder: 43},
  4. {navn: "Frank", alder: 2},
  5. {navn: "Kim", alder: 12},
  6. {navn: "Huggo", alder: 20}
  7. ];
  8.  
  9. async function GET(url) {
  10. const OK = 200;
  11. let response = await fetch(url);
  12. if (response.status !== OK)
  13. throw new Error("GET status code " + response.status);
  14. return await response.text();
  15. };
  16.  
  17. async function generateHTML(data) {
  18. let template = await GET('/index.hbs');
  19. let compiledTemplate = Handlebars.compile(template);
  20. return compiledTemplate({ data });
  21. };
  22.  
  23. function sum() {
  24. let tempSum = 0;
  25. let rows = document.querySelectorAll("tr");
  26. for(let row of rows){
  27. tempSum += Number.parseInt(row.cells[1].innerHTML);
  28. }
  29. let diven = document.querySelector("#diven");
  30. diven.innerHTML = "Sum: " + tempSum;
  31. };
  32.  
  33. /* Tunger at udfører men meget mere simpel
  34. function sum() {
  35. let tdElements = document.querySelectorAll('td');
  36. let tempSum = 0;
  37. for(let i = 0; i < tdElements.length; i++) {
  38. if(i % 2 == 1) {
  39. tempSum += Number.parseInt(tdElements[i].innerHTML.trim());
  40. }
  41. }
  42. let diven = document.querySelector("#diven");
  43. diven.innerHTML = "Sum: " + tempSum;
  44. };
  45. */
  46.  
  47. function removeElement(event) {
  48. let e = event.target;
  49. e.parentNode.parentNode.removeChild(e.parentNode);
  50. sum();
  51. };
  52.  
  53. function setOnclick() {
  54. let rows = document.querySelectorAll("tr");
  55. for(let i of rows){
  56. i.onclick = removeElement;
  57. }
  58. }
  59.  
  60. async function main() {
  61. try {
  62. let html = await generateHTML(personer);
  63. document.body.innerHTML = html;
  64. setOnclick();
  65. sum();
  66. } catch (e) {
  67. console.log(e.name + ": " + e.message);
  68. }
  69. };
  70.  
  71. main();
RAW Paste Data