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

Question

Question

javascript sum for each department

asked on April 19, 2017

Hello,

 

I have this javascript that is totaling data based on a form submission. I would like to break the total down by cost center. Any suggestions?

 

$(document).ready(function () {
$('.cf-table-block').on('blur', 'input', sumtotal);
$('.tax').on('blur', 'input', sumtotal);
$('.shipping').on('blur', 'input', sumtotal);
function sumtotal() {
var sum = 0;
$('.cf-table-block tbody tr').each(function () {
var s = 0;
s = parseNumber($(this).find('.price input').val()) * parseNumber($(this).find('.quantity input').val())+ parseNumber($(this).find('.miscellaneous input').val());
$(this).find('.subtotal input').val(s);
sum += s;
sum += parseNumber($('.tax input').val()) + parseNumber($('.shipping input').val());
});
$('.total input').val(sum);
}
function parseNumber(n) {
var f = parseFloat(n); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});

0 0

Replies

replied on April 19, 2017

Hi Tiffany,

You don't need custom JavaScript for this; you can use formula calculations to achieve this. (You could probably use calculations for the computations within the table too, but the JavaScript gets the job done.)

The formula you need is the SUMIFS formula, which is used thus:

=SUMIFS( ValueSet , CheckSet1 , Condition1 , CheckSet2 , Condition2 , ... )

This will sum the values in ValueSet where the corresponding entries in CheckSet1 meet Condition1, the entries in CheckSet2 meet Condition2, etc. This really only makes sense in the Forms implementation for tables and collections.

For example, I have a table (variable name: Table) in a sample form with two columns: Number and Category. Number column consists of Number-type fields and Category consists of Dropdown-type fields with values {"A","B","C"}. Outside of the table I have three Number-type fields called A, B, and C. The field A has a calculation like so:

=SUMIFS(Table.Number,Table.Category,"=A")

The other standalone fields B and C have analogous calculations. The attached screenshot shows the form in action: A has the sum of values in Table where Category is "A", and so on.

So for your form, something like

=SUMIFS(Expense_Information.Subtotal,Expense_Information.Cost_Center,"=01-A")

should do the trick. (Up to replacement with correct variable names from your form.)

Hope this helps!

1 0
replied on April 20, 2017

Thank you! I have Forms 9.1. Do you know if the sumif works for this version? 

 

 

0 0
replied on April 21, 2017

I tried adding this if line and it's still not working.
 

$(document).ready(function () {
$('.cf-table-block').on('blur', 'input', sumtotal);
$('.quantity').on('blur', 'input', sumtotal);
$('.price').on('blur', 'input', sumtotal);
$('.miscellaneous').on('blur', 'input', sumtotal);
function sumtotal() {
var sum = 0;
$('.cf-table-block tbody tr').each(function () {
var s = 0;
s = parseNumber($(this).find('.price input').val()) * parseNumber($(this).find('.quantity input').val())+ parseNumber($(this).find('.miscellaneous input').val());
$(this).find('.subtotal input').val(s);
  if (($(this).find('.cost-center select').val() == "01-Admin")

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

 

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

Sign in to reply to this post.