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

Question

Question

Forms 10.1 - adding fields together in a table

asked on July 18, 2016 Show version history

I want to add two fields together for each row in a table using the SUM function.  I have a subtotal field for each row in the table.  (see attached. For example, the subtotal field on the top row should add up to 24, not 51).  However, what is happening is that the subtotal field is summing up the values in all the rows instead of each individual row.

0 0

Replies

replied on July 19, 2016

Hi Mark,

Your attachment didn't come through so I may be missing something in my response, but you have two options if you're using Forms 10.1 - the first option only works in 10.1 (or later).

For the following examples:

For the table, the variable is "Table", css is "table"

Amount 1: the variable is "Amount1", css is "amount1"

Amount 2: the variable is "Amount2", css is "amount2"

Subtotal: the variable is not used below, css is "subtotal"

First option is that you can use the Advanced-Calculation setting in your "Subtotal" field options per the changes for 10.1 with the following format:

=SUM(INDEX(Table.Amount1,ROW()),INDEX(Table.Amount2,ROW()))

Second option is to do it via JavaScript - credit to John Hanlon in his 7 April post which helped me work it out:

$(document).ready(function () {
  $('.table').on('blur','input',function () {
    $('.table tr').each(function () {
      var amount1 = parseNumber($(this).find('.amount1 input').val());
      var amount2 = parseNumber($(this).find('.amount2 input').val());
      var subtotal = amount1 + amount2;
      $(this).find('.subtotal input').val(subtotal).trigger('change');
    });
  });
});

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

I'm still learning this stuff too, but setting up a sample form in 10.1 worked with both of the above options.

Cheers,

Mike

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

Sign in to reply to this post.