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

Question

Question

Separate Sums from Separate tables to

asked on June 17, 2015

Hi,

I'm trying to figure out how to distinguish sums from separate tables. I have two tables on the same form and I'm using the following code from this link: https://www.laserfiche.com/support/webhelp/laserficheforms/9.0/en-us/forms/Content/CustomizingYourFormExamples.htm

The code looks like this

$(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.sum').each(function () {
var s = 0;
$(this).find('input').each(function () {
s += parseNumber($(this).val());
})
$(this).find('.subtotal input').val(s);
sum += s;
});
$('.total 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;
}
});

 

How would I distinguish the sums of both tables?

Here is a screenshot of the form so you can see what I'm doing.

 

0 0

Answer

SELECTED ANSWER
replied on June 17, 2015

Here's a generic form

And the Javascript used to calculate the sums from two tables and input it into separate "total" fields

$(document).ready(function () {
  
  $('.t1total input').attr('readonly','True');
  $('.t2total input').attr('readonly','True');
  $('.cf-table-block').on('blur', 'input', sumtotal);
  $(document).on('click', sumtotal);

  function sumtotal() {
    var sum1 = 0;
    var sum2 = 0;
    $('td.t1sum').each(function () {
      var s1 = 0;
      $(this).find('input').each(function () {
        s1 += parseNumber($(this).val());
      });
      sum1 += s1;
    });
    $('td.t2sum').each(function () {
      var s2 = 0;
      $(this).find('input').each(function () {
        s2 += parseNumber($(this).val());
      });
      sum2 += s2;
    });
    $('.t1total input').val(sum1);
    $('.t2total input').val(sum2);
  }

  function parseNumber(n) {
    var f = parseFloat(n); //Convert to float number.
    return isNaN(f) ? 0 : f; //treat invalid input as 0;
  }
  
});

The "amount" fields in Table1 and Table2 use the CSS class "t1sum" and "t2sum" respectively. The "total" fields use the CSS class "t1total" and "t2total" respectively.

1 0

Replies

replied on June 18, 2015

Fantastic, thank you!

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

Sign in to reply to this post.