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

Multi-Condition Dropdowns

New Here ,
Jul 24, 2023 Jul 24, 2023


I would like assistance for populating a different drop down based on two conditions. For example, I want to display rides in different sections of a theme park based on height level. I'd like to be able to filter on which section of the park as well as the height level to display the available options within that range? Is there a way to do this given that item 1 could potentially point to "south\>48in " as well as "south\<50in"?

 

Also where would I place the code for each conditional?

TOPICS
How to , JavaScript , PDF , PDF forms
397
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 ,
Jul 24, 2023 Jul 24, 2023

The answer is Yes!  You can read about list/dropdown basics here:

https://www.pdfscripting.com/public/List-Field-Usage-and-Handling.cfm

 

Basically, use the "setItems" function to set the items in a dropdown.  Use this function with care. It will change the value of the dropdown, and trigger calculations.

 

And the best way to handle changing a value based on multiple inputs is to use a calculation script. 

https://www.pdfscripting.com/public/How-to-Write-a-Basic-PDF-Calculation-Script.cfm

https://www.pdfscripting.com/public/How-to-write-an-If-statement.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
New Here ,
Jul 25, 2023 Jul 25, 2023

Hello, 

So I would need to create variables and calculations for each possibility? Would I have to assign each field and the corresponding values a variable as well?

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 ,
Jul 25, 2023 Jul 25, 2023

You can create a single script to set the new items based on the values of the two input fields you've listed. 

This can be done many different ways and the specifics depend on the specific requirements. 

But Here's a very simple sample script that sets a different set of items based on the two inputs. 

 

 

var cInputA = this.getField("FieldA").value;
var cInputB = this.getField("FieldB").value;
var aItemList = null;
if(cInputA == "Option1")
{
    if(cInputB == "Choice1")
       aItemList = ["A","B","C"];
    else if(cInputB == "Choice2")
       aItemList = ["AA","BB","CC"];
}
else if(cInputA == "Option2")
{
    if(cInputB == "Choice1")
       aItemList = ["a","b","c"];
    else if(cInputB == "Choice2")
       aItemList = ["aa","bb","cc"];
}

if(aItemList)
    this.getField("Dropdown1").setItems(aItemList);
else
    this.getField("Dropdown1").clearItems();
    

 

 

 

This code demonstrates a brute force decision pattern.  You can use this in just about any situation. 

 

 

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
Community Expert ,
Jul 25, 2023 Jul 25, 2023
LATEST

If used as a calculation script, you won't be able to select items in the dropdown, so add this to the top of the script:

if (event.source && (event.source.name=="FieldA" || event.source.name=="FieldB")) {

 

And at the end of the script close curly bracket. Or use script at validate in both "FieldA" and "FieldB".

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