Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

process a list of options with java script?

Community Beginner ,
Feb 28, 2017 Feb 28, 2017

So I'm nealry finished setting up a self-calculating pdf-document, but even though I could use my java knowledge to solve most of my problems myself, there is one thing, that doesn't work quite as it was supposed to work.

I made a table with 2 different forms, one row is a drop down menu with several options the other one is supposed to be Digits.

It probably looks like the one below. In this case the drop down fields on the left should have 5 options and the fields on the right simply digits. (I couldn't write beneath the table any more, so i just left it down there)

On the bottom of this table i made 5 Fields. One for every option. Now I'd like to sum up the numbers of the different options. (So if i have 3 drop-down-fields on the left with option 3, I want to count the 3 numbers on the right).

My idea was to do that with a for loop.

So basically (for i = 0; i <= n; i++) while n is the number of colums

but now i don't know how to adress each field

I tried to solve it like this:

if (this.getField("Option_"+i).value == "O1"){

event.value = event.value+(this.getField("Digit_"+i)).value

}

This doesn't work however.

I could do it with if-functions for every single field, but that would lead to a lot of redundant code.

There are 2 more, small issues.

1. The auto-calculation always lags one field behind. So if i Have 3 fields: 4+5 = 9 and i change 4 to 7, the sum won't change to 12 until i changed another field (like 5 to 8). Any ideas why?

2. If I'm on the "calculations" tab while editing a field and I double click into an empty field without any calculations and double click back into one with calculations, they won't be displayed. The Tab tells we the value will not be calculated even though it will be like it should be normally, unless i write any kind of new calculation.

OptionsDigits
O1, O2, O3, O4, O52
56

1

TOPICS
PDF forms
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2017 Mar 01, 2017

Azzarrel  wrote

...

I could do it with if-functions for every single field, but that would lead to a lot of redundant code.

...

What functions do you use for single fields?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 01, 2017 Mar 01, 2017

basically this

if (this.getField("Option_"+i).value == "O1"){

event.value = event.value+(this.getField("Digit_"+i)).value

}

but without changing variables, so like this for example:

if (this.getField("calculate").value == "multiply"){

event.value = (this.getField("Digit_1")).value * (this.getField("Digit_2")).value

}

Now, if i want to sum up 3 different multiplications as long as their value is positive, I'd have to wirte:

if (this.getField("multiplication_1").value >= 0){

event.value = event.value + (this.getField("multipicalton_1")).value

}

if (this.getField("multiplication_2").value >= 0){

event.value = event.value + (this.getField("multipicalton_2")).value

}

if (this.getField("multiplication_3").value >= 0){

event.value = event.value + (this.getField("multipicalton_3")).value

}

I'd like to use a for loop (as shown in my first post) to shorten this, but i don't know how  to adressed different a field without using another if-function

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2017 Mar 01, 2017

You can access the fields like this:

this.getField("multiplication_" + i).value

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 08, 2017 Mar 08, 2017

okay.

Any Ideas on my other 2, small issues.

I'd like to have them answered as well before i can close this question

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2017 Mar 08, 2017

Check the calculation order of the form.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 10, 2017 Mar 10, 2017
LATEST

I have another problem.

I succeeded in adding a for loop for a sum

var v = 0;

for (var i=1; i <= 4; i++)

{

if (this.getField("Text"+i).value

>= 1)

{

v = v+this.getField("Text"+i).value

}

}

event.value = v;

In this case the event.value of my field "sum" is the sum of all fields named Text1 to Text4 as long as they have a positive value

Now I tried to set my Text fields into a dependency on a second field namend "bool".

For each Text field there is one field called "bool". The fields named "bool" can have any value, but i only care about if the value is either "1" or "not 1".

If the value is 1 i want to include the related text field in the sum, if it is anything else, I don't want it to be part of the sum.

so now i wrote:

var v = 0;

for (var i=1; i <= 4; i++)

{

if (this.getField("bool"+i).value

== 1)

{

v = v+this.getField("Text"+i).value

}

}

event.value = v;

But now the sum won't change no matter what i write into the "bool" fields.

If i change "bool" to "Text" however, the sum works again, but only for Text fields with the value of 1.

So yea, why doesn't it work if i set a dependency to another field?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines