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

Check a checkbox based on number of checkboxes checked within same pdf

New Here ,
Nov 02, 2022 Nov 02, 2022

I am really banging my head against a wall and believe I have exhausted every possible search that I can. To paint a picture:

 

  • I have a checklist with two checkbox options per line, "Checked" and "Repaired."
  • I have assigned an export value of 1 to all "Checked" checkboxes and an export value of 2 to all "Repaired" checkboxes.
  • Each row has the same checkbox name so they are linked together and only the "Checked" or "Repaired" checkbox can be checked for that particular line.
  • On a separate page, I would like a "Checked" checkbox to be automatically checked if ALL previously mentioned "Checked" checkboxes are checked.
  • On that same separate page, I would like a "Repaired" checkbox to be automatically checked if even a single "Repaired" checkbox is checked in the previously mentioned "Repaired" checkboxes.

 

I am able to calculate the total of the checkboxes in a separate hidden field. If the value in this field is EQUAL to the total number of rows of checkboxes (in this case 8), I would like the "Checked" checkbox on the second mentioned page to be automatically checked. If the total value in this field is GREATER than 8, I would like the "Repaired" checkbox checked on the second mentioned page.

 

Is this possible to complete in 1 stage/field or do I need to first calculate the value of all the checkboxes on the first page and export to a field and then dynamically set another field equal to my export field and then dictate which checkbox should be checked on the second page? I know very confusing and can provide illustration of what I am trying to accomplish if necessary. Thank you to all who digest this and can offer support!!

TOPICS
Create PDFs , JavaScript , PDF forms
2.0K
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
1 ACCEPTED SOLUTION
Community Expert ,
Nov 02, 2022 Nov 02, 2022

That is a clever solution, but it does have a major fault. if half the repaired boxes were checked the sum equals the same if all checked boxes are checked. 

 

It is more direct and simple to just loop over all checkbox groups and detect the required conditions.

 

Lets say all the check list groups are named like this. 

"Checklist.row1", "Checklist.row2", etc. 

Then this code will set the result checkboxes based on the conditions you've stated above,

the Checked checkbox is named "Checked" and the Repaired checkbox is named "Repaired", and the export values of the result checkboxes are "Yes"

 

 

 

var aFldList = this.getField("Checklist").getArray();


this.getField("Checked").value = aFldList.every(a=>a.value == 1)?"Yes":"Off";
this.getField("Repaired").value = aFldList.some(a=>a.value == 2)?"Yes":"Off";

 

 

 

The Array.every() function returns true if all elements of the array meet the test and the Array.some() function returns true if any element of the array meets the test. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

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 ,
Nov 02, 2022 Nov 02, 2022

That is a clever solution, but it does have a major fault. if half the repaired boxes were checked the sum equals the same if all checked boxes are checked. 

 

It is more direct and simple to just loop over all checkbox groups and detect the required conditions.

 

Lets say all the check list groups are named like this. 

"Checklist.row1", "Checklist.row2", etc. 

Then this code will set the result checkboxes based on the conditions you've stated above,

the Checked checkbox is named "Checked" and the Repaired checkbox is named "Repaired", and the export values of the result checkboxes are "Yes"

 

 

 

var aFldList = this.getField("Checklist").getArray();


this.getField("Checked").value = aFldList.every(a=>a.value == 1)?"Yes":"Off";
this.getField("Repaired").value = aFldList.some(a=>a.value == 2)?"Yes":"Off";

 

 

 

The Array.every() function returns true if all elements of the array meet the test and the Array.some() function returns true if any element of the array meets the test. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
New Here ,
Nov 02, 2022 Nov 02, 2022
Thank you Thom, I had thought about the possibility of half the REPAIRED checkboxes simulating the same condition as if all CHECKED checkboxes were checked.

Are you saying that by naming the checkbox groups with the period between the prefix and suffix, this would solve my problem? I was thinking alternatively that I could change the export value of the REPAIRED checkboxes to 9 or any number larger than the sum of all CHECKED values.

Any ideas on how I can get from finding the sum of all export values and applying a Javascript or other formula to make the second page’s checkboxes react the way I described in my original post? Thank you again, I will gladly take any insight and suggestions passed along!

Regards,

Nick Furlan
nafurlan26@gmail.com
224-229-1909
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
New Here ,
Nov 02, 2022 Nov 02, 2022

My apologies Thom, I didn't see the rest of your explanation and sample code. I will try and report back, have a great evening!

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 ,
Nov 02, 2022 Nov 02, 2022

Well, I suppose summing could work if you used prime numbers so the summs are unique. But the script I provided in the previous post is much better. Don't over complicate things. Simple direct solutions are best.  

 

The "." in the name is a way to group a set of form fields, so that all of the fields in a group can be acquired in a single line of code. 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
New Here ,
Nov 04, 2022 Nov 04, 2022

Hello Thom, that suggestion regarding the checkbox arrays that you recommended did the trick. I have one more question. Is there an action trigger besides "Mouse Enter" or "Mouse Exit" that I can use that will automatically change the checkboxes on my page second page? The checkboxes will not update until I hover over both. I was hoping to fill out the checklist in its entirety and not really have to review the second form at all. Thank you again for any suggestions you can offer!

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 ,
Nov 04, 2022 Nov 04, 2022

The calculation scripts are triggered any time any field changes, so the calculation on the hidden field you're already using is perfect. This is where the script I provided should be. The script only needs to be put in one place to handle all checkbox changes. 

 

If there is some other action you want to trigger on a checkbox change then yes, it is possible to add for example, a validation script to a checkbox. But this can only be done with another script.

See this article: 

https://www.pdfscripting.com/public/Editing-Fields-Properties.cfm#JScript

And this one for more general form scripting info

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Explorer ,
Apr 13, 2023 Apr 13, 2023

@Thom Parkerwhat exactly does (a=>a.value == 1) do, I don't see 'a' declared?

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 ,
Apr 15, 2023 Apr 15, 2023
LATEST

That's an arrow function. It's a core JS feature. (the a is declared implicitly by it's use)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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