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

Question

Question

How to sum table columns to a subtotal field and weight the columns?

asked on March 29, 2016

We have a form with a table that has 4 columns. We'll reference them as columns 1, 2, 3, and subtotal. There is a row for each day of the week (Sunday-Saturday). For each row I need to be able to do the following calculation: (col1 *30)+(col2 *20)+(col3 *40).

 

How would I use JavaScript to do this since currently it is not possible to calculate table columns in Forms 10?

1 0

Answer

SELECTED ANSWER
replied on March 29, 2016

Hi Blake,

The Forms JavaScript documentation is a great resource for setting up a table with calculations. For your particular case, you can try using the following JavaScript to get you started:

$(document).ready(function(){
  $('.cf-table-block').on('blur', 'input', sumtotal);
  function sumtotal(){
    $('.cf-table-block tbody tr').each(function(){
      var sub = 0;
      var col1 = parseNumber($(this).find('.col1 input').val());
      var col2 = parseNumber($(this).find('.col2 input').val());
      var col3 = parseNumber($(this).find('.col3 input').val());
      sub = col1*30 + col2*20 + col3*40;
      $(this).find('.sub input').val(sub);
    });
  }
  function parseNumber(n){
    var f = parseFloat(n); //Convert to float number.
    return isNaN(f) ? 0 : f; //treat invalid input as 0;
  }
});

The above code assumes that you have given your column fields the corresponding CSS classes: col1, col2, col3, sub

Let me know if this works for you!

1 0
replied on October 12, 2016

Hello Alex, 

I am currently going through the same situation. I was successful in implementing javascript to add up colomns in a table and assigning the value to a number filed 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.

 

Could you help? 

 

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;
    }
});    

Replies

replied on March 29, 2016

Blake,

   There's a great example of doing something very similar to this in the following answer:

https://answers.laserfiche.com/questions/85243/calculations-within-and-outside-of-a-table-in-forms

   Take a look at it and if you have any questions about altering it for your specific form, post back here and I'll be glad to help out with it.

0 0
replied on March 29, 2016

Hi Blake

It is possible to Sum Columns in a table to a Subtotal Field using the Forms 10 built in Calculator. The great thing about this is that if you allow the submitter to use the Add Rows feature in the form, it will include those in the calculations on the fly, so you are not limited to a static amount of rows.

The Syntax of the formula to place in the Calculation area of your Subtotal Field (which is separate from the table) is:

=Sum(Table variable Name.Column Name)

I find it easiest to use the variable > that appears on the right of the Calculation Field

Example here is the Table Variable with the arrow and the column names appear underneath.

 

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

Sign in to reply to this post.