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

Make field dependent on total number of radio buttons input variable.

New Here ,
May 10, 2018 May 10, 2018

Copy link to clipboard

Copied

On the PDF form I have created I would like to create the following functionality.

  • If all standards (1.1.1, 1.1.2, 1.1.3) are "meeting" then standard 1.1 rating is "Meeting Standard "
  • If any standards (1.1.1, 1.1.2, 1.1.3) are "working towards" then standard 1.1 rating is "Working Towards Standard"
  • If no criteria selected then "Not Yet Rated" is displayed.

I have followed these steps to achieve this but the calculation is not performing correctly.

  1. I have set radio buttons fields "1.1.1, 1.1.2, 1.1.3" to = 1 for Meeting and = 0 for Working towards.
  2. I have created a tally "1.1 Tally" that sums these (working correctly - displays 1,2,3 as the 3 Meeting boxes are selected - counts back down if changed to working towards)
  3. I have added a calculation to field "Rating 1.1"

var Tally = this.getField("1.1 Tally").value;

if (Tally == 3) {

event.value = "Meeting Standard";

}

else if ((Tally <= 3) && (Tally >= 0)) {

event.value = "Working Towards";

}

else {

event.value = "Not Yet Rated";

}

However - I get the following results (the Tally field e.g. 3/3 is always correct)

1 x Working and 2 x Meeting = Meeting Standard (incorrect)

3 x Meeting = Working Towards (incorrect)

2 x Working 1 x Meeting = Working Towards (correct!)

Please, can someone help me understand my error? I have never used Javascript before

Thank you

TOPICS
Acrobat SDK and JavaScript

Views

1.4K

Translate

Translate

Report

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
Engaged ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

this is why this part:

//If at least one is checked, sum only those that are checked

else{

    var sum = 0

    for (var x=1;x<=3;x++){

       var f = this.getField("1.1."+x)

       if (f.isBoxChecked(0) == true){

             sum += f.value

       }

    }

is important.  It will sum up only boxes that are checked (that return a number) and will skip the ones returning a string ("Off")

Votes

Translate

Translate

Report

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
New Here ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

Thanks I appreciate the explanation.

I have tried your original formula MatLac and it is not summing the Meeting boxes. It returns blank.

Votes

Translate

Translate

Report

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
Engaged ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

Of course you are.  In JS, like in every language, data has a type.  It can be a number, a string, an array, a boolean, etc.  JS is a dynamic language, as opposed to a static language.  In a static language, like JAVA for instance, you need to declare variables, but you also need to declare the type or it will not work.  JS, being dynamic, will "presume" the type, and will also dynamicaly change type when needed, without the need for the coder to do it specifically.  This speeds up the coding process but will sometimes induces errors when the coder is not aware of what is happenig.  You can return the type of the data with the typeof command like so:

var myData = 9

app.alert(typeof myData) //will output "number" as an alert

var myData = []

app.alert(typeof myData) //will output "array" as an alert

In JS, the "+" operator is an addition operator when working with numbers, but it becomes a concatenation operator when working with strings (meaning it will append strings one after the other).  But what happens when you try add a number (0) to a string ("Off")?  As soon as it its a string, it converts any number it encountered to a string and concatenate it.

Votes

Translate

Translate

Report

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
Engaged ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

I tested it myself.  There is a problem with it so try this instead:

//Counts how many Un-checked boxes there are

var isOff = 0

for (var x=1;x<=3;x++){

   var f = this.getField("1.1."+x)

   if (f.value == "Off") isOff++

}

//If all three are un-checked, shows blank

if (isOff == 3) event.value = ""

//If at least one is checked, sum only those that have a value of 1

else{

    var sum = 0

    for (var x=1;x<=3;x++){

       var f = this.getField("1.1."+x)

       if (f.value == 1)  sum++

    }

   //and display the sum

   event.value = sum

}

For some reason, isBoxChecked(0) only returned true when the first check box was checked, not the second one.......So I tested its value instead.  For the sum part, I tested if the value is equal to 1 (because it is the only case where the value will be incremented) and just used the increment operator "++"

Votes

Translate

Translate

Report

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
New Here ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

Thanks MatLac!

This worked.

Would you please mind checking this script for me. Now that I have Rating 1.1 1.2 and 1.2 correctly populating I would like to total the number of ratings that are "meeting"

I have tried to first assign each variable a value of 1 only if the field value is Meeting. Then I would like to sum these together so I get a number /3

if (this.getField("Rating 1.1").value =="Meeting") {

    var v1 = 1

} else {

    var v1 = 0

}

if (this.getField("Rating 1.2").value =="Meeting") {

    var v2 = 1

} else {

    var v2 = 0

}

if (this.getField("Rating 1.3").value =="Meeting") {

    var v3 = 1

} else {

    var v3 = 0

}

var sum = v1 + v2 + v3

event.value = sum

If all are "Meeting" I correctly get a value of 3 however - if two of the rating fields are Working Towards and the other Meeting, I get a value of 1.

Votes

Translate

Translate

Report

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
Engaged ,
May 14, 2018 May 14, 2018

Copy link to clipboard

Copied

LATEST

This seems ok altought it is a bit long.

You could really shorten it by assigning a 0 value from the sart to all three variables and increment it to 1 only if needed like so:

var v1 = 0

var v2 = 0

var v3 = 0

if (this.getField("Rating 1.1").value =="Meeting") v1++

if (this.getField("Rating 1.2").value =="Meeting") v2++

if (this.getField("Rating 1.3").value =="Meeting") v3++

var sum = v1 + v2 + v3

event.value = sum

The middle block looks repetitive, don't it? How about using a loop and an array instead:

var aValues = [0,0,0]

for (var i in aValues){

    if (this.getField("Rating 1."+(Number(i)+1)).value =="Meeting") aValues++

}

var sum = 0

for (i in aValues) sum += aValues

event.value = sum

Well, look at that, we iterated twice in the same array.  Why not do both operations in one loop?

var aValues = [0,0,0]

var sum = 0

for (var i in aValues){

    if (this.getField("Rating 1."+(Number(i)+1)).value =="Meeting") aValues++

    sum += aValues

}

event.value = sum

We can even get rid of sum and operate directly with on the field value

var aValues = [0,0,0]

event.value = 0

for (var i in aValues){

    if (this.getField("Rating 1."+(Number(i)+1)).value =="Meeting") aValues++

    event.value += aValues

}

Do we need an array at all?  Definitly not. Lets iterate 3 times instead

event.value = 0

for (var i=1;i<=3;i++){

if (this.getField("Rating 1."+i).value =="Meeting") event.value++

}

Do we even need brackets? nope

event.value = 0

for (var i=1;i<=3;i++) if (this.getField("Rating 1."+i).value =="Meeting") event.value++

So that 17 lines code you wrote can be achieved as a 2 lines code.  Whenever you feel something is repetitive and long, it's because you are doing it wrong.  It takes experience but it will come.

Votes

Translate

Translate

Report

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