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

Question

Question

Addition of columns in table using javascript throwing error

asked on October 12, 2016 Show version history

 

I was successful in implementing javascript to add up colomns in a table and assigning the result to a number field outside the Table. 

 

It was all working fine, but whenever I enter a 4 digit value into colomns, the calculation all goes berserk. 

Lets say I enter 4 values into column1 like 1000, 1 , 1, 1. The result I am getting is 4 now. 

I think the code is taking the 4 digit number as single digit.

 

 

 

Javascript is given below for the same. 

 

 

$(document).ready(function () {
    $('.cf-table-block').on('blur', 'input', sumtotal);
    if ($('.subtotal').length > 0) {
        $('.cf-table-block').on('blur', 'input', rowtotal);
    }
    function sumtotal() {
        var sum = 0;
        $('td.S1').each(function () {
            var s = 0;
            $(this).find('input').each(function () {
                s += parseNumber($(this).val());
            });
            $(this).find('.subtotal input').val(s);
            sum += s;
        });
        $('.A input').val(sum);
    }
    function rowtotal() {
        var sum = 0;
        $('.cf-table-block tbody tr').each(function () {
            var s = 0;
            $(this).find('.sum input').each(function () {
                s += parseNumber($(this).val());
            })
            $(this).find('.subtotal input').val(s);
            sum += s;
        });
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});    

0 0

Answer

APPROVED ANSWER
replied on October 13, 2016

Are you using the thousands delimiter in your number column? parseFloat will ignore all numbers after the comma. Try the following instead where it takes out the commas from your numbers

    function parseNumber(n) {
        var f = parseFloat(n.replace(/,/g,'')); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
1 0
replied on October 15, 2016

Thank you Alex, 

That change in code worked out for me. I was using the thousands delimiter in my columns and hence the parseFloat function was ignoring the numbers after comma. 

 

One more help, what if I want to calculate decimal numbers?? like for example If i want to add up 3.5 , 6.7 , 7.9 etc etc.

Currently Am not able to enter any decimal numbers into my columns. Is there any work around ? Or am I missing some thing? 

Please bear with me as I am new to forms .

 

Regards.

0 0
replied on October 17, 2016

While you are editing your table in the Forms layout view, click on the "Field options" link next to the number column and confirm that you've configured it to accept decimals.

0 0
replied on February 1, 2022

Hi,

Sorry I'm late to this but I have a problem when I attempt this function:

function sumTotal(){

    var sum = 0;
    var q = 0;
    var price = 0;
    
    $('.cf-table-block tbody tr').each(function(){
      	var s = 0; 
     	s = parseNumber($(this).find('.quantity input').val()) * parseNumber($(this).find(' .price input').val());
     	q= parseNumber($(this).find('.quantity input').val());
     	price= parseNumber($(this).find('.price input').val())
       
     	$(this).find('.subtotal input').val(s.toFixed(2));    

        sum+=s;
    });
      
   $('#q806 input').val(sum.toFixed(2));
   }
   
  	function parseNumber(n) {
    	var f = parseFloat(n); //Convert to float number.
    	return isNaN(f) ? 0 : f; //treat invalid input as 0;    
   }
   });

I get this:

the first class=quantity

the second class=price

 

I've tried to replace this:

var f = parseFloat(n); //Convert to float number.

with what you showed above but get this:

Basically, all I'm attempting to do is take the quantity x the price to equal the subtotal.

Price and subtotal are both in USD & the "use thousands delimiter" is checked for price and subtotal

Any help would be greatly appreciated.

0 0

Replies

replied on October 13, 2016

I'd like to see a screenshot of your table, and to know the classes on some of the elements to better understand your code. For instance, if you have a field with class "subtotal" somewhere on your form, then you are running 2 event handlers in response to the same event. Should both of these be running or is it more of an either/or situation? I'm also not sure what you're trying to do in the sumtotal function. You're iterating over td elements in such a way that it looks like you're expecting multiple inputs within a single td (this is possible, but it would be unusual with the standard stuff in forms).

1 0
replied on October 15, 2016

Hey Scott, 

I am trying to add up the columns in a table and assign it to a number field outside the table. 

The code which I referred from Laserfiche Help file was ignoring all numbers after the comma. 

Small change in the code as per Alex did the work for me. 

 

Appreciate you response Scott. 

Thankyou

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

Sign in to reply to this post.