Advertisement
viligen

generateReport

May 30th, 2022
905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function generateReport() {
  2.     let outputArr = [];
  3.  
  4.     let headers = document.querySelectorAll("th input");
  5.     let cells = document.getElementsByTagName("td");
  6.     let rowLen = headers.length;
  7.     console.log(headers);
  8.  
  9.     let checkedHeaders = {}; //index: text
  10.  
  11.     for (let i = 0; i < headers.length; i++) {
  12.         let currentInput = headers[i];
  13.  
  14.         let currentText = currentInput.parentElement.textContent;
  15.  
  16.         if (currentInput.checked === true) {
  17.             checkedHeaders[i] = currentText;
  18.         }
  19.     }
  20.     console.log(checkedHeaders);
  21.  
  22.     console.log(cells);
  23.     console.log(cells.length);
  24.     let newObj = {};
  25.     for (let i = 0; i < cells.length; i++) {
  26.         if (i % rowLen == 0 && i != 0) {
  27.             outputArr.push(newObj);
  28.  
  29.             newObj = {};
  30.         }
  31.  
  32.         let idx = i % rowLen;
  33.         console.log();
  34.  
  35.         if (checkedHeaders.hasOwnProperty(idx)) {
  36.             let cellText = cells[i].textContent;
  37.  
  38.             console.log(cellText);
  39.  
  40.             let key = checkedHeaders[idx].toLowerCase().trim();
  41.             console.log(key);
  42.  
  43.             newObj[key] = cellText;
  44.  
  45.             console.log(newObj);
  46.         }
  47.     }
  48.     outputArr.push(newObj);
  49.  
  50.     console.log(outputArr);
  51.     let filtered = outputArr.filter((o) => Object.keys(o).length > 0);
  52.     let outputJSON = JSON.stringify(filtered);
  53.     let textArea = document.getElementById("output");
  54.     textArea.value = outputJSON;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement