Expand my Community achievements bar.

Calculating the total of dynamic subforms using javascript

Avatar

Level 2

I have created a dynamic form that allows me to add or subtract the amount of entries using the instance manager. I can calculate a total for each instance, but how do I get the total of each one of those instances using Javascript?

4 Replies

Avatar

Level 2

I think I know a way to do it with tables but that is with formcalc. I can try that but I wanted to keep it with subforms and use javascript if possible. Anyone have any Ideas?

Avatar

Level 10

Hi,

I have an example here:

http://www.assuredynamics.com/index.php/category/portfolio/building-dynamic-tables/

Its main purpose is to show how to build dynamic tables, but if you have a look at the total field you will see the FormCalc script. To be honest it is much easier to do this using FormCalc because with this language you do not need to resolve each instance.

// sum using FormCalc

$ = Sum(Row1[*].amount)

You can do this in Javascript, but it is a slightly longer script.

// sum using Javascript

var vTotal = 0;

var vRows = _Row1.count;


for (var i=0; i<vRows; i++)

{

     vTotal = vTotal + xfa.resolveNode("Row1[" + i + "]").amount.rawValue;

}

this.rawValue = vTotal;

Both will achieve the same thing and will be automatically updated as rows are added and deleted, but overall the FormCalc is a simpler script.

Hope that helps,

Niall

Assure Dynamics

Avatar

Level 2

I was trying to do it without a table, but I ended up converting it to a table so the code you gave me works with that. Thanks.

Avatar

Level 10

Aghh, that's a pity.

The building dynamic tables example (http://assure.ly/gBJYj9) had a repeating subform example (Table 6). The FormCalc is very similar, you just reference the repeating subform.

// sum using FormCalc

$ = Sum(rowSubform[*].amount)

Good luck,

Niall

Assure Dynamics