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

Question

Question

Sum items in collection only if...

asked on July 31, 2017 Show version history

Good afternoon,

For simplicity purposes let's say that we have a collection with two radio buttons (a and b). and also a number field.

Outside of the collection we have 2 boxes (totals_a and totals_b). 

Right now, we are using the javascript example found under https://www.laserfiche.com/support/webhelp/Laserfiche/10/en-US/administration/#../Subsystems/Forms/Content/Customizing-Your-Form-Examples.htm to sum all quantities entered into the number field and then populating the totals in totals_a and totals_b.

We would like to extend the functionality so that only the totals_a box has the sum for those items where the radio button is "a" and only the totals_b box has the sum for those items where the radio button is "b".

 

For example, 

I add three items in the collection

ITEM 1 = Radio "a" and number 4

ITEM 2 = Radio "b" and number 2

ITEM 3 = Radio "a" and number 7

totals_a box = 11

totals_b box = 2

Before embarking into research for this I wanted to ask you if you've worked on something similar that I can take a look at or if there is a more straight out-of-the-box function to tackle this.

 

Thank you,

 

Raul Gonzalez

0 0

Replies

replied on July 31, 2017

UPDATE: I was able to accomplish the split sums by using the code below. This works in 10.2. Hope someone finds it useful.

=SUMIF(Collection.Radio,"a",Collection.Number)

 

Thank you,

 

Raul Gonzalez

 

2 0
replied on July 31, 2017

One additional question. How can I use the formula so that if there are two fields in the collection, the two are SUMed and then place in the total. I tired this but didn't work.

 

=SUMIF(Collection.Radio,"a",SUM(Collection.Number,Collection.NumberTwo))

0 0
replied on August 2, 2017

Well, after some research I was able to finally make it work. Following some tutorials I was able to assemble it together. Hope someone finds it useful

The below function will sum three number fields in the same collection if the radio button is "a". You place this in your "Totals_a" textbox function.

=SUMIF(Collection.Radio,"a",Collection.Number) + SUMIF(Collection.Radio,"a",Collection.NumberTwo) +
SUMIF(Collection.Radio,"a",Collection.NumberThree)

2 0
replied on July 31, 2017

UPDATE: I'm experimenting with the built in function SUMIF by adding this into the totals_a, but can't make it work.

=SUMIF(Collection.Radio,"a")

replied on July 31, 2017 Show version history

So far I've got this, but haven't been able to make it work. I've seen other examples across the Laserfiche forums and some seem to have made that example work, but I just can't seem to make mine work. 

  function sumtotal() {    
        var s = 0;
    
    
    
        $('.number input').each(function () { 
          
          
          
          if (($(this).find('.radio input:checked').val() == "a")) {

            s += parseNumber($(this).val());
          }
          
        });
          
          
          
        var sum = s.toFixed(2);   
    	/*Total a*/
        $(".totals_a input").val(sum.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ","));

    
    }
 
  function parseNumber(n) {
        var stringNumber = n.toString().replace(/,/g, "").replace("^-?\d+$", "");
        var f = parseFloat(stringNumber); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }

 

replied on August 1, 2017 Show version history

I've also tried using JavaScripts from other Laserfiche examples across the forum, but haven't been able to make it work like that either.

  function sumtotal() {    
        var s = 0;
        $('.number input').each(function () {    
          
          if (($(this).find('.radio input:checked').val() == "a") {

            s += parseNumber($(this).val());
          
        }
                              
                              
        });
        var sum = s.toFixed(2);   
    	/*Total a*/
        $(".totals_a input").val(sum.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ","));
    

    }

 

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

Sign in to reply to this post.