can the rows or columns of a table be added?

0

I want to add the columns of a table

<table id="ejemplo" border="1">
    <thead>
        <tr class="encabezado">
            <th>Campo 1</th>
            <th>Campo 2</th>
        </tr>
    </thead>
    <tbody>
          <tr>
            <td>1</td>
            <td>5</td>
          </tr>
          <tr>
            <td>7</td>
            <td>1</td>
          </tr>
          <tr>
            <td>16</td>
            <td>1</td>
          </tr>
    </tbody>
    <tfoot>
        <tr>
            <th>Total</th>
            <th>Total</th>
        </tr>
    </tfoot>
</table>

this data comes from an array, I must add in the array or I can add the table columns directly.

    
asked by hubman 06.09.2018 в 23:32
source

1 answer

3

Look something like this

var total=0;
$("#ejemplo tbody tr").each(function(){
	total+=parseFloat($(this).find("td").eq(0).text());
});
$("#total").text(total);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table id="ejemplo" border="1">
  <thead>
    <tr class="encabezado">
      <th>Campo 1</th>
      <th>Campo 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>5</td>
    </tr>
    <tr>
      <td>7</td>
      <td>1</td>
    </tr>
    <tr>
      <td>16</td>
      <td>1</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th id="total">Total</th>
      <th>Total</th>
    </tr>
  </tfoot>
</table>

Greetings:)

    
answered by 06.09.2018 / 23:38
source