You are viewing limited content. For full access, please sign in.

Question

Question

Calculation from 3 tables to one single text field as Total Script

asked on October 26, 2015

Hi, 

I have 3 Table fields with an Estimated Cost value. css class for table 1 is estc1, css class for table 2 is estc2 and css class for table 3 is estc3. The total is suppose to be the calculated value of estc1 + estc2 + estc3. The css class for the Total Estimated Cost is total. I know i am doing something silly. What am  i missing?

$(function() {
  $(document).ready(function () {
    
    function parseNumber(n) {
      var f = parseFloat(n); //Convert to float number.
      return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
    function sumtotal() {
      var s = 0;
      var s1 = 0;
      var s2 = 0;
      
      $('.cf-table-block tbody').each(function () {
        
        s = parseNumber($(this).find('.estc1 input').val());
        s1 = parseNumber($(this).find('.estc2 input').val());
        s2 = parseNumber($(this).find('.estc3 input').val());
        

      });
      
      
       $('.total input').val(s + s1 + s2);
      
      
     }
     $('.cf-table-block').change(sumtotal);
    });
});
    

0 0

Answer

SELECTED ANSWER
replied on October 26, 2015

Hi Christobal, 

 

I came right with the code. I saw i forgot to add the following 

      $('.cf-table-block tbody tr').each(function () {

instead of 

      $('.cf-table-block tbody').each(function () {

1 0

Replies

replied on October 26, 2015

Try this in your code:

 s += parseNumber($(this).find('.estc1 input').val());

When you just have:

 s = parseNumber($(this).find('.estc1 input').val());

it is resetting the value to the last value in the table. Try to add the '+' in front of the '=' for each of your s1 and s2.

1 0
replied on October 26, 2015

Thanks Christobal,

It is working, but it is now not calculating the 1st table estc1 which consist of 4 column/rows. It is only taking the 1st row and adding it to the rest of the other tables. 

 

estc1 has 4 columns only 1st one is calculating

estc2 has 1 and is adding to total

estc3 has 1 and is adding to total

estc4 has 1 and is adding to total

 


$(function() {
  $(document).ready(function () {
    
    
    function parseNumber(n) {
      var f = parseFloat(n); //Convert to float number.
      return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
    function sumtotal() {
      var s = 0;
      var s1 = 0;
      var s2 = 0;
      var s3 = 0;
      
      
      $('.cf-table-block tbody').each(function () {
        
        s += parseNumber($(this).find('.estc1 input').val());
        s1 += parseNumber($(this).find('.estc2 input').val());
        s2 += parseNumber($(this).find('.estc3 input').val());
        s3 += parseNumber($(this).find('.estc4 input').val());
        
        

      });
      
      
       $('.total input').val(s + s1 + s2 +s3);
      
      
     }
     $('.cf-table-block').change(sumtotal);
    });
});
    

 

0 0
SELECTED ANSWER
replied on October 26, 2015

Hi Christobal, 

 

I came right with the code. I saw i forgot to add the following 

      $('.cf-table-block tbody tr').each(function () {

instead of 

      $('.cf-table-block tbody').each(function () {

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.