Advertisement
simeonshopov

Generate report

May 18th, 2021
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function generateReport() {
  2.     let results = [];
  3.     let checkedBoxes = {};
  4.     const totalBoxes = Array.from(document.querySelectorAll('table thead tr th')).map(x => x.children[0]);
  5.  
  6.     for (let i = 0; i < totalBoxes.length; i++) {
  7.         const box = totalBoxes[i];
  8.         if (box.checked) {checkedBoxes[i] = box.name}
  9.     }
  10.  
  11.     const rows = Array.from(document.querySelectorAll('table tbody tr')).map(x => Array.from(x.children));
  12.  
  13.     for (let row of rows) {
  14.         let obj = {};
  15.         for (let i = 0; i < row.length; i++) {if (Object.keys(checkedBoxes).map(Number).includes(i)) {obj[checkedBoxes[i]] = row[i].textContent;}}
  16.         results.push(obj);
  17.     }
  18.    
  19.     document.getElementById('output').textContent = JSON.stringify(results);
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement