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

Question

Question

Calculating a Total from a Field in a Collection

asked on December 20, 2013 Show version history

I am looking for some help on the correct javascript that would accomplish adding the sum of a field in a Collection and placing the calculated total into a field outside of the collection.

 

I have reviewed the Forms 9.1 administration guide that discusses javascript calculations (found here: http://www.laserfiche.com/support/webhelp/laserficheforms/9.1/en-us/forms/#CustomizingYourFormExamples.htm), and although one of the last examples on that page describes how to calculate fields from within a Collection, it only shows how to add the values to a subtotal field also within the collection and not a "Total" field outside of the collection.

 

I've attempted many permutations to the sample code to see if I can make it work, to no avail.  Any help will be much appreciated.

 

EDIT: Even going with the example used in the administration guide where there's a subtotal adding the sum of the fields in a "collection", how would you then calculate the sum of all of the subtotals and place the value in a "Total" field outside of the collection?

0 0

Answer

APPROVED ANSWER SELECTED ANSWER
replied on December 23, 2013

For the following code, I added a single line field with the total css class to the form outside of any collections. This code will calculate the subtotal of fields within each collection and then populate the overall total in the total field.

 

$(document).ready(function () {
    $('.cf-collection-block').on('blur', 'input', sumtotal);
    function sumtotal() {
        var sum = 0;
        $('.cf-collection-block ul').each(function () {
            var s = 0;
            $(this).find('.sum input').each(function () {
                s += parseNumber($(this).val());
            })
            $(this).find('.subtotal input').val(s);
            sum += s;
          
        });
      $('.total input').val(sum);  //populates the total field.
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

 

1 0
replied on December 23, 2013

This worked wonderfully, thank you very much Eric!!

0 0
replied on December 23, 2013

You're welcome, Travis!

0 0
replied on November 17, 2015

Hey Guys,

So for the CSS Class on the repeatable fields, would that be sum?

And the CSS class for the total would be total?

Thanks!

0 0
replied on October 12, 2018

This works but why does the each loop run so many times when you only have 2 instances in your collection. I would expect each to mean run for each instance in the collection. So if I had a collection with 1 instance, then it would run once, if I had a collection with 2 instances it would run twice.

Something like this would be nice, if possible, just to make sure we are not over processing. 

for(var row in collection){console.log(row.field)}

0 0
replied on May 14, 2019

The float is artificially rounding up the total, so my result is inaccurate. However, I've been unable to use without the parseNumber function. Any suggestions?

0 0
replied on May 14, 2019 Show version history

Would it be easier to use the built-in Calculations that Forms has?
I have a sample Collection with the Number fields "Pages", "Copies", and "Price".
The Price field is a multiplication of Pages and Copies:
=MULT(INDEX(Collection.Pages, ROW()),INDEX(Collection.Copies, ROW()))
Although in the below screenshot, I take it a step further and multiply that by 100:
=MULT(100,MULT(INDEX(Collection.Pages, ROW()),INDEX(Collection.Copies, ROW())))
And to get a total of all Price values, I have a Number field just outside the collection with the calculation:
=SUM(Collection.Price)

You should be able to control the number of decimal places using the Number field properties. You can apply ROUND, ROUNDUP, or ROUNDDOWN functions as needed, too. For more information, see the Formulas in Laserfiche Forms help file.

2 0
replied on May 15, 2019

Thank you, that's perfect.

0 0

Replies

replied on February 6, 2014

Eric, I have a form that contains a dozen or so different repeatable collections. If someone fills out this form and repeats one of those collections, will this add the totals of all dozen collection types, including every iteration of those collections, and then place that total value in to a field outside all of the collections?

0 0
replied on February 6, 2014

Never mind, this worked! But since I only had one value to calculate in each collection, I used the same variable name for the subtotal and individual field amounts in each collection.

0 0
replied on February 6, 2014

That works, or you could omit this line:

$(this).find('.subtotal input').val(s);

and use the sum class with the desired field.

0 0
replied on May 20, 2016

Is there a way with Forms 10.1 to have a total field in the collection, and sum all total values into an outside Grand total field?

In the example above would i just specify the class for the total field in the collection as "sum" ?

0 0
replied on April 26, 2018

Hi all,

First, thank you Eric !

 

This is hopefull!

Your script is working ; however I have some trouble when i'm trying to erase 1 value in collection.

As you can see below, my sum still have old value (500) until I add a new value.

 

 

 

It looks like the function doesn't called when I erase some value.

 

I tried this but unsucces :

$(document).on('blur change click', 'input', sumtotal);

 

How can I do ?

Regards

0 0
replied on April 26, 2018

Ok i found how to do.

Just change

$(document).on('blur change click', 'input', sumtotal);

to

$(document).on('blur change click', sumtotal);

 

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

Sign in to reply to this post.