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

Question

Question

Javascript calculating Forms 10 totals plus keep two decimal places

asked on May 26, 2016 Show version history

Hello,

I am attempting to use javascript to calculate subtotals, tax, totals and then make fields read only in Forms 10.1.  

Classes:

This is working fine with the following java:

//Calculate Totals and Tax - START
$(document).ready(function () {
$('.cf-table-block').on('blur', 'input', sumtotal);
$('.tax').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());
$(this).find('.cost input').val(s);
  //$('.cost input').attr('readonly', 'True'); //makes the field read only after we give it a value.
	sum += s;
$('.subtotal input').val(sum);
  $('.subtotal input').attr('readonly', 'True'); //makes the field read only after we give it a value.
$('.tax input').val(parseNumber($('.taxpercent input').val())*sum);
  $('.tax input').attr('readonly', 'True'); //makes the field read only after we give it a value.
$('.total input').val(parseNumber($('.tax input').val())+sum);
  $('.total input').attr('readonly', 'True'); //makes the field read only after we give it a value.
  $('#q83 input').attr('readonly',true);
  $('#q83 select').attr('disabled',true);
});
}
function parseNumber(n) {
var f = parseFloat(n); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});
//Calculate Totals and Tax - END

I then added this code under the code above:

//Keep all currency fields at two decimal places - START
$(document).ready(function() {

  $('.currency').change(function() {
    $(this).val(formatCurrency($(this).val()));
  });
  
  function formatCurrency(value) {
    var float = parseFloat(value);
    return isNaN(float) ? 0 : (Math.round(float*100)/100).toFixed(2);
  }
});
//Keep all currency fields at two decimal places - END

My result ends up being this:  (only unit price gets the two decimal places)

How do I combine/change the code to get it to show two decimal places on all entries for "Unit Price", "Price/Cost", "Subtotal", "Tax" and "Total"?

0 0

Answer

SELECTED ANSWER
replied on May 30, 2016

Hi David, to do calculation within same row, you can use following formula:

=PRODUCT(INDEX(Completed_Tasks.Qty_Hrs,ROW()),INDEX(Completed_Tasks.Unit_Price,ROW()))

1 0

Replies

replied on May 27, 2016

Hi David,

     For the calculation and set read only part, you can simply use Calculation instead of JavaScript and check "Read-only" for these fields:

  1. for Subtotal, set the calculation to something like "=SUM(Table.Price_Cost)"
  2. for Tax, set the calculation to something like "=MULT(Subtotal,Tax_Percent)"
  3. for Total, set the calculation to something like "=SUM(Subtotal,Tax)"

     For the currency formatting, you can change based on Scott's suggestion, following script works for me:

$(document).ready(function() {
$('.cf-table-block').on('change','.currency',function() {
    $(this).val(formatCurrency($(this).val()));
  });
  
  $('.currency').change(function() {
    $(this).val(formatCurrency($(this).val()));
  });
  
  
  function formatCurrency(value) {
    var float = parseFloat(value);
    return isNaN(float) ? 0 : (Math.round(float*100)/100).toFixed(2);
  }
});

1 0
replied on May 30, 2016

Thank for both of your replies.  Xiuhong, I am attempting to convert it over to use the Forms 10 calculations but running into an issue.  When trying to use the built in calculations I get the following:

Result:

And if I try this:

Result is this:

What am I missing?

 

0 0
SELECTED ANSWER
replied on May 30, 2016

Hi David, to do calculation within same row, you can use following formula:

=PRODUCT(INDEX(Completed_Tasks.Qty_Hrs,ROW()),INDEX(Completed_Tasks.Unit_Price,ROW()))

1 0
replied on May 31, 2016

Perfect!

Thanks so much.

0 0
replied on May 26, 2016

I see 2 issues on a quick glance at your code.

1) You are using a change event to trigger the currency formatting. When your sumtotal function runs to update your line and table totals, you do not trigger change events for those fields when you set their values. There are 2 fixes you could do here. First, trigger a change manually as your code modifies those fields. Second, wrap the values you are passing into the fields with your formatCurrency() function as you set them.

2) You need a delegated event handler to properly catch events on all of your table fields. This would involve replacing "$('.currency').change(function() {" with something like "$('form.cf-form').on('change','.currency', function() {" instead. Notice how row 2 of your table didn't format the Unit Price? This will fix that issue.

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

Sign in to reply to this post.