Expand my Community achievements bar.

SOLVED

Summation not working

Avatar

Level 2

I was trying to sum value through array in JavaScript calculation event. But the value is not added rather the value places side by side (output 111 but it should be 3)

var aList = new Array(xfa.resolveNode("A"),xfa.resolveNode("B"),xfa.resolveNode("C"));

j = NumericalField.rawValue;

total = 0;

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

{

total = total+aList[i].rawValue;

}

this.rawValue = total;

Above script cannot add the value just showing 111 instead of 3.

How can I solve it?

1 Accepted Solution

Avatar

Correct answer by
Level 8

Also when adding fields with 'rawValue' in JavaScript you run the risk of inadvertantly concatenating strings together, in your case '111'. You need to convert it to a number first by either using parseInt or parseFloat.

For example, total = total+parseInt(aList[i].rawValue); from your original post.

Kyle

View solution in original post

8 Replies

Avatar

Level 5

Hi,

may I als if you use an array. Normally you don't need an array for an addition.

This script makes an addition from all fields called "summe". Change this if you field name is amount to amount.

var page = xfa.layout.page(this)-1;

var fields = xfa.layout.pageContent(page , "field", 0);

this.rawValue = 0;

var total = 0;

for (var i=0; i <= fields.length-1; i++)

{

    if (fields.item(i).name == "summe")

    {

        total = total + fields.item(i).rawValue;

    }

}

this.rawValue = total;

Here you will find an example. https://workspaces.acrobat.com/?d=PtkT0VkRuIsKJOgnmagKWw

Hopefully helpful, (if not maybe you could insert a screenshot from your scenario?)

Mandy

Avatar

Level 2

Thanks for your response.

Actually the field name must be unique and always all fields(500 fields) value will not add. And that is why I used array and j value will control the no of field from array.

I do not understand why my script do not add rather it gets value side by side.

In result field I get 111 insted of 3 but it should be 3.

Thanks

Avatar

Level 5

You have the possibility to upload your formular anywhere?

Avatar

Level 2

You have posted a nice and convenient script. If I want to add fields value in different pages or in different subform, what will be script?

Thanks

Avatar

Level 5

You have to add nothing. The script looks for all pages and alle fields called summe and make an addition of them.

I see you have no number fields for the addition. you want to make an addition from a lot of dropdown fields, right?

If I right then you have to use the following simple thing.

rename all your dropdowns - for example  DDL -

in the total field you select as language "Formcalc"

and use this script in the calculate event

$ = Sum(DDL[*])

https://workspaces.acrobat.com/?d=tSNfzRZXkOG5azk4I1*mFw

Helpful?

Mandy

Avatar

Correct answer by
Level 8

Also when adding fields with 'rawValue' in JavaScript you run the risk of inadvertantly concatenating strings together, in your case '111'. You need to convert it to a number first by either using parseInt or parseFloat.

For example, total = total+parseInt(aList[i].rawValue); from your original post.

Kyle

Avatar

Level 2

Nice solution.

Getting problem until enter all three fields, I can not get result.

If I select the 1st dropDown, the result will be 1 but not get result after selecting 1st one.

How can I solve it?