Advertisement
EntropyStarRover

DOM Introduction. 8 Generate report

Jan 31st, 2021
688
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function generateReport() {
  2.  
  3.     let rowsArray = Array.from(document.getElementsByTagName("tbody")[0].querySelectorAll('tr'));
  4.     console.log(rowsArray);
  5.  
  6.     let chkboxes = Array.from(document.getElementsByTagName("input"));
  7.     // console.log(chkboxes);
  8.  
  9.     let outputArea = document.getElementById("output");
  10.  
  11.     let data = [];
  12.  
  13.     for (let i = 0; i < rowsArray.length; i++) {
  14.         let row = rowsArray[i];
  15.  
  16.         let cells = Array.from(row.querySelectorAll('td'));
  17.      
  18.         let employee = cells[0].textContent;
  19.         let deparment = cells[1].textContent;
  20.         let status = cells[2].textContent;
  21.         let dateHired = cells[3].textContent;
  22.         let benefits = cells[4].textContent;
  23.         let salary = cells[5].textContent;
  24.         let rating = cells[6].textContent;
  25.  
  26.         let employeeObj = {
  27.             employee,
  28.             deparment,
  29.             status,
  30.             dateHired,
  31.             benefits,
  32.             salary,
  33.             rating
  34.         }
  35.         data.push(employeeObj)
  36.     }
  37.  
  38.     //  console.log(data);
  39.     let checkedFields = []
  40.  
  41.     for (let i = 0; i < chkboxes.length; i++) {
  42.         let isChecked = chkboxes[i].checked;
  43.         // console.log(isChecked);
  44.         if (isChecked) {
  45.             checkedFields.push(chkboxes[i].name);
  46.         }
  47.     }
  48.  
  49.     let output = [];
  50.  
  51.     for (let i = 0; i < data.length; i++) {
  52.         let emp = data[i];
  53.         let newObj = {};
  54.         for (let k = 0; k < checkedFields.length; k++) {
  55.             let field = checkedFields[k];
  56.             newObj[field] = emp[field];
  57.         }
  58.  
  59.         output.push(newObj);
  60.  
  61.     }
  62.     let outputString=JSON.stringify(output)
  63.     outputArea.textContent = outputString;
  64.     // console.log(output)
  65.  
  66. }
Advertisement
RAW Paste Data Copied
Advertisement