Advertisement
vladovip

generateReport_ DOM_ JS Advanced

Feb 11th, 2023 (edited)
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function generateReport() {
  2.     let bodyRowsRef = document.querySelectorAll("tbody tr");
  3.     let headRowsRef = document.querySelectorAll("thead tr th input");
  4.     let columnPosition = [];
  5.     for (let index = 0; index < headRowsRef.length; index++) {
  6.        
  7.         if ( headRowsRef[index].checked == true){
  8.             columnPosition.push(index);
  9.         }
  10.     }
  11.     let finalResult = [];
  12.     // let data = document.querySelectorAll("tbody tr");
  13.  
  14.     for (let j = 0; j < bodyRowsRef.length; j ++) {
  15.         let currRow = bodyRowsRef[j].children;
  16.         let currItems = {};
  17.         for (let k = 0; k < currRow.length; k ++) {
  18.             if (columnPosition.includes(k)) {
  19.                 let label = headRowsRef[k].name;
  20.                 currItems[label] = currRow[k].textContent;
  21.             }
  22.         }
  23.         finalResult.push(currItems);
  24.     }
  25.  
  26.     let jsonFinal = JSON.stringify(finalResult);
  27.     let target = document.getElementById('output');
  28.     target.value = jsonFinal;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement