Search code examples
javascripthtmlloopsfor-loophtml-table

Summing the numbers of the columns of the HTML table


I want to add the columns of the following tables together and display the same table in the last row. I was able to collect the columns of the first table and display them in the last row, but I want to repeat this code for all the tables, but I don't know how to do it.

enter image description here

I have used the following code to add the columns of the first table and it works fine, but I don't know how to repeat this code for all the tables.

function computeTableColumnTotal(colNumber) {
  var result = 0;
  try {
    var tableBody = document.querySelector("tbody");
    var howManyRows = tableBody.rows.length;
    for (var i = 1; i < (howManyRows - 1); i++) {
      var thisNumber = parseInt(tableBody.rows[i].cells[colNumber].childNodes.item(0).data);
      if (!isNaN(thisNumber)) {
        result += thisNumber;
      }
    }
  } finally {
    return result;
  }
}

var final = 0;
var tbody = document.querySelector("tbody");
var howManyCols = tbody.rows[0].cells.length;
var totalRow = tbody.rows[tbody.rows.length - 1];

for (var j = 1; j < howManyCols; j++) {
  final = computeTableColumnTotal(j);
  totalRow.cells[j].innerText = final;
}
html {
     scroll-behavior:smooth;
}
 body {
     margin-bottom:200px;
}
 * {
     scroll-margin:30px;
}
 h1 {
     display:inline-block;
     margin-top:0px;
     font-size:22px;
     border-bottom:3px solid black;
}
 span{
     margin-bottom:20px;
}
 table {
     border-collapse: collapse;
     width: 90%;
     direction:ltr;
     margin:10px 10px;
     overflow: scroll;
     margin:0px;
}
 th {
     border:2px solid black;
     padding:6px;
     font-size:17px;
     font-weight:bold;
     text-align:center;
     border-collapse: collapse;
}
 td {
     border:2px solid black;
     padding:6px;
     font-size:16px;
     font-weight:bold;
     text-align:center;
     border-collapse: collapse;
}
 caption {
     padding:6px;
     border:2px solid black;
     border-bottom:none;
     font-size:17px;
     font-weight:bold;
     text-align:right;
     border-collapse: collapse;
     background:dodgerblue;
     color:white;
}
 tr:nth-of-type(odd){
     background:#B9B9B9;
}
 button {
     margin:20px 0px;
}
 table{
     margin-top: 20px;
}
<!DOCTYPE html>
<html dir="ltr">
  <head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <title></title>
  </head>
  <body>
    <table>
      <tbody>
        <tr>
          <th>column 1</th>
          <th>column 2</th>
          <th>column 3</th>
        </tr>
        <tr>
          <td>A</td>
          <td>10</td>
          <td>10</td>
        </tr>
        <tr>
         <td>B</td>
         <td>1</td>
         <td>1</td>
        </tr>
        <tr>
          <td>C</td>
          <td>20</td>
          <td>20</td>
        </tr>
        <tr>
          <td>D</td>
          <td>1</td>
          <td>1</td>
        </tr>
        <tr>
          <td>Total</td>
          <td></td>
          <td></td>
        </tr>
      </tbody>
    </table>
    <table>
      <tbody>
        <tr>
          <th>column 1</th>
          <th>column 2</th>
          <th>column 3</th>
        </tr>
        <tr>
          <td>A</td>
           <td>10</td>
           <td>10</td>
        </tr>
        <tr>
           <td>B</td>
           <td>1</td>
           <td>1</td>
        </tr>
        <tr>
           <td>C</td>
           <td>20</td>
           <td>20</td>
        </tr>
        <tr>
           <td>D</td>
           <td>1</td>
           <td>1</td>
        </tr>
       <tr>
           <td>Total</td>
           <td></td>
           <td></td>
        </tr>
        </tbody>
    </table>
    <table>
      <tbody>
      <tr>
        <th>column 1</th>
        <th>column 2</th>
        <th>column 3</th>
      </tr>
        <tr>
          <td>A</td>
           <td>10</td>
           <td>10</td>
        </tr>
        <tr>
           <td>B</td>
           <td>1</td>
           <td>1</td>
        </tr>
        <tr>
           <td>C</td>
           <td>20</td>
           <td>20</td>
        </tr>
        <tr>
           <td>D</td>
           <td>1</td>
           <td>1</td>
        </tr>
        <tr>
          <td>Total</td>
          <td></td>
          <td></td>
        </tr>
      </tbody>
    </table>
  </body>
</html>


Solution

  • document.querySelector return the first element matching your query.

    Just use document.querySelectorAll to get all your table bodys.

    Then loop over the returned tbodys for instance with a for(tbody in tbodys) loop.

    Pass the current tbody to your compute function instead of making a query again. And voilà:

    function computeTableColumnTotal(tableBody, colNumber) {
      var result = 0;
      try {
        var howManyRows = tableBody.rows.length;
        for (var i = 1; i < (howManyRows - 1); i++) {
          var thisNumber = parseInt(tableBody.rows[i].cells[colNumber].childNodes.item(0).data);
          if (!isNaN(thisNumber)) {
            result += thisNumber;
          }
        }
      } finally {
        return result;
      }
    }
    
    var final = 0;
    var tbodys = document.querySelectorAll("tbody");
    for (tbody of tbodys) {
      var howManyCols = tbody.rows[0].cells.length;
      var totalRow = tbody.rows[tbody.rows.length - 1];
    
      for (var j = 1; j < howManyCols; j++) {
        final = computeTableColumnTotal(tbody, j);
        totalRow.cells[j].innerText = final;
      }
    }
     html {
         scroll-behavior:smooth;
    }
     body {
         margin-bottom:200px;
    }
     * {
         scroll-margin:30px;
    }
     h1 {
         display:inline-block;
         margin-top:0px;
         font-size:22px;
         border-bottom:3px solid black;
    }
     span{
         margin-bottom:20px;
    }
     table {
         border-collapse: collapse;
         width: 90%;
         direction:ltr;
         margin:10px 10px;
         overflow: scroll;
         margin:0px;
    }
     th {
         border:2px solid black;
         padding:6px;
         font-size:17px;
         font-weight:bold;
         text-align:center;
         border-collapse: collapse;
    }
     td {
         border:2px solid black;
         padding:6px;
         font-size:16px;
         font-weight:bold;
         text-align:center;
         border-collapse: collapse;
    }
     caption {
         padding:6px;
         border:2px solid black;
         border-bottom:none;
         font-size:17px;
         font-weight:bold;
         text-align:right;
         border-collapse: collapse;
         background:dodgerblue;
         color:white;
    }
     tr:nth-of-type(odd){
         background:#B9B9B9;
    }
     button {
         margin:20px 0px;
    }
     table{
         margin-top: 20px;
    }
    <!DOCTYPE html>
    <html dir="ltr">
      <head>
       <meta charset="UTF-8">
       <meta name="viewport" content="width=device-width, initial-scale=1">
       <title></title>
      </head>
      <body>
        <table>
          <tbody>
            <tr>
              <th>column 1</th>
              <th>column 2</th>
              <th>column 3</th>
            </tr>
            <tr>
              <td>A</td>
              <td>10</td>
              <td>10</td>
            </tr>
            <tr>
             <td>B</td>
             <td>1</td>
             <td>1</td>
            </tr>
            <tr>
              <td>C</td>
              <td>20</td>
              <td>20</td>
            </tr>
            <tr>
              <td>D</td>
              <td>1</td>
              <td>1</td>
            </tr>
            <tr>
              <td>Total</td>
              <td></td>
              <td></td>
            </tr>
          </tbody>
        </table>
        <table>
          <tbody>
            <tr>
              <th>column 1</th>
              <th>column 2</th>
              <th>column 3</th>
            </tr>
            <tr>
              <td>A</td>
               <td>10</td>
               <td>10</td>
            </tr>
            <tr>
               <td>B</td>
               <td>1</td>
               <td>1</td>
            </tr>
            <tr>
               <td>C</td>
               <td>20</td>
               <td>20</td>
            </tr>
            <tr>
               <td>D</td>
               <td>1</td>
               <td>1</td>
            </tr>
           <tr>
               <td>Total</td>
               <td></td>
               <td></td>
            </tr>
            </tbody>
        </table>
        <table>
          <tbody>
          <tr>
            <th>column 1</th>
            <th>column 2</th>
            <th>column 3</th>
          </tr>
            <tr>
              <td>A</td>
               <td>10</td>
               <td>10</td>
            </tr>
            <tr>
               <td>B</td>
               <td>1</td>
               <td>1</td>
            </tr>
            <tr>
               <td>C</td>
               <td>20</td>
               <td>20</td>
            </tr>
            <tr>
               <td>D</td>
               <td>1</td>
               <td>1</td>
            </tr>
            <tr>
              <td>Total</td>
              <td></td>
              <td></td>
            </tr>
          </tbody>
        </table>
      </body>
    </html>