Total sum of a column

2

I have this column of a table in html which I need to do a total sum and the result insert it a input text type

td(class='sumTtotal') This is my td where each result of the dif $

remains

What I was trying to do is to take the value of each td and add it to an array and then add it all up

$(function() {
    var arrText = new Array();
    $('.sumaTotal').find("td").map(function() {
        arrText.push($(this).val());
    });
    console.log(arrText);
});

But it does not bring me any information

This is the table which is in Jade

 table(id='table-preAjuste' class='table table-striped table-hover table-condensed analisisD')
    thead
      tr
        th='Articulo'
        th='Descripción'
        th='Um'
        th='Ov'
        th='Ubic'
        th='Ubic F'
        th='Teorico'
        th(class='vi1')='Conteo1'
        th(class='vi2')='Conteo2'
        th(class='vi3')='Conteo3'
        th='Dif'
        th='Dif'
        th='Dif $'
        th(class='vi3')='Asignar conteo'
        th='Ajustar'
          input(type="checkbox" id='selectall')
        th='Reversar'

      tbody
      if PreAjuste != undefined
        each PArticulos in PreAjuste
          tr
            td(id='id_articulo' class='iarticulo')= PArticulos.SI_Articulo
            td= PArticulos.SI_Descripcion
            td= PArticulos.SI_UM
            td= PArticulos.SI_OV
            td= PArticulos.SI_Ubicacion
            td= PArticulos.SI_Ubicacion_Fisica
            td= PArticulos.SI_Existencia
            td(class='vi1')= PArticulos.SI_Cantidad
            td(class='vi2')
            td(class='vi3')
            td= PArticulos.SI_Dif
            td(contenteditable='true')
            td(class='sumTtotal')= PArticulos.SI_Dif_Dinero

and so the table looks

I hope you can help me, Thanks.

    
asked by Eduard 26.07.2017 в 16:11
source

3 answers

1

You are using val() which is for the form controls (textarea, checkbox, radio, textbox, select) on a td that is not a control.

Use .text() that gets the text from the html elements:

var data = [];

$("td.sumTotal").each(function(){
  data.push(parseFloat($(this).text()));
});


var suma = data.reduce(function(a,b){ return a+b; },0);

console.log(data);
console.log(suma);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
 <tr>
  <td class="sumTotal">5.6</td>
 </tr>
  <tr>
  <td class="sumTotal">5</td>
 </tr>
  <tr>
  <td class="sumTotal">5</td>
 </tr>
</table>
    
answered by 26.07.2017 / 16:18
source
3

You can do it in the following way:

$(function(){
    var sum = 0;
    $("td.sumaTotal").each(function(){
        sum += parseFloat( $( this ).text() );
    });

    $("#your-input-id").val(sum);
});
    
answered by 26.07.2017 в 16:44
2

Taking the previous answer as a reference, we can adapt it to your question in this way

$(document).ready(function(){
var data = [];
$("td.subtotal").each(function(){
  data.push(parseFloat($(this).text()));
});
var suma = data.reduce(function(a,b){ return a+b; },0);
    $("#detalle_total").html(suma);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
 <tr>
  <td class="subtotal">-106952.291</td>
 </tr>
  <tr>
  <td class="subtotal">-66225.678</td>
 </tr>
  <tr>
  <td class="subtotal">-993385.17</td>
 </tr>
  <tr>
  <td class="subtotal">-1176545.887</td>
 </tr>
  <tr>
  <td class="subtotal">-534793.585</td>
 </tr>
 <tr>
  <td class="subtotal">-156381.806</td>
 </tr>
</table>
<br>
<div>Suma total :
<lable id="detalle_total"></label>
</div>
    
answered by 26.07.2017 в 16:34