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

Question

Question

Subtotal in Row and Row Subtotals in Grand Total

asked on August 16, 2016

I have a form that I cannot get the calculations to work correctly using JavaScript in Forms 9.1.0. Each row in the table has 4 columns (Cash, In-Kind, Grant, and Total). Each has a CSS class assigned to it respectively (revenueCash, revenueInKind, revenueGrant, revenueLineTotal). There is also a Grand Total field at the bottom of the form with the CSS value of revenueGrandTotal.

I need to add the Cash, InKind, and Grant totals together and display the line total in the Total field per row. I then need to add the Total fields from each row and display it in the Grand Total field.

I have used the following JavaScript, but it is not calculating at all when a value is entered. I'm sure I am missing something simple, but any help would be appreciated.

//Calculate row totals
  $('cf-table-block').on('blur', 'input', sumtotal);
  
  function sumtotal() {
    var sum = 0;
    $('.cf-table-block tbody tr').each (function () {
      var s = 0;
      s = parseNumber($(this).find('.revenueCash input').val()) + parseNumber($(this).find('.revenueInKind input').val()) + parseNumber($(this).find('.revenueGrant input').val());
      $(this).find('.revenueLineTotal input').val(s);
      sum += s;
    });
    
    $(".revenueGrandTotal input").val(sum);
  }
    
  function parseNumber(n) {
    var f = parseFloat(n); //Convert to float number
    return isNaN(f) ? 0 : f; //Treat invalid input as 0;
  }

 

2 0

Answer

SELECTED ANSWER
replied on August 16, 2016 Show version history

Hi Blake, two things:

  1. The statement with the on method needs to be wrapped in the $(document).ready trigger like so:
    $(document).ready(function() {
        $('cf-table-block').on('blur', 'input', sumtotal);
    });
  2. In line 2 of your code block, the string "cf-table-block" is missing the period in front. It should be ".cf-table-block".

Hope this helps!

Edit: Was over-zealous in the first iteration of this reply; the whole thing doesn't need to be in the trigger.

1 0
replied on August 16, 2016

Thank you James! I did have it wraped in the $(document).ready trigger, I just didn't include that. After adding the period in front of cf-table-block it is working correctly. Thank you!

1 0

Replies

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

Sign in to reply to this post.