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

how to populate drop down list based on multiple text box entries

Community Beginner ,
Feb 23, 2019 Feb 23, 2019

dropdown.png

hi, i'm having problem on how to auto-populate the drop down list (Wall Assignment) based on the Text box fields (Name). I am using Adobe Acrobat Pro Dc.

The table with Name column is from page 2, while the Wall Assignment drop down, is on page 3.

Whenever the user enters a Name, it will auto-populate the Wall Assignment list.

i saw some post that uses addItems and setItems, but still don't know what to use and where to insert the code. is it on the textbox using keystroke or action javascript, or on the drop down field.

thanks

TOPICS
PDF forms
16.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 ,
Feb 28, 2019 Feb 28, 2019

All items in the dropdown are selectable. That is not the problem.

The issue is that this script is a calculation. The script is run anytime any field on the form changes.

There are 3 solutions:

1)  Change the script to a validation script on the "LstGrp" text fields

2)  Qualify the calculation by testing the source of the event. In this case it should be one of the "LstGrp" fields

3)  (poor solution) Save the value of the list before changing the list items, then set it.

I think #2 is the best one since it blocks unnecessary updates and only changes code in one location.

Put the code in this if statement

if(/^LstGrp/.test(event.source.name))

{

    .. The script...

}

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 ,
Feb 23, 2019 Feb 23, 2019

Here's a tutorial that covers adding and deleting items from a list. And there is an example file.

https://acrobatusers.com/tutorials/list_and_combo_in_lc

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 Beginner ,
Feb 24, 2019 Feb 24, 2019

hi, i already saw this example and there's no drop down list in the sample pdf form.

do you have a simple code where in I can populate a drop down list after I enter some text in the textbox?

i only know how to populate a text box field using a drop down list. but I don't know how to populate a drop down.

thanks

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 ,
Feb 25, 2019 Feb 25, 2019

List and dropdowns are exactly the same programatically. The code in the example will work for your dropdown.

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 Beginner ,
Feb 26, 2019 Feb 26, 2019

is there a way where I don't need to use a button to populate the combo box?

like when I enter a name on a textbox, it will automatically added to the combo box?

i tried using the setItems method, but i can only add one entry/data to the combo box. i have 6 text box that needs to be added to the combo box.

i'm not good in using array, can an array do this? if yes, how?

thanks in advance

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 ,
Feb 26, 2019 Feb 26, 2019

Yes, the text from a text box can be automatically added to a list.

Here's an example of how it can be done. The code below assumes that each text box represents one entry in the dropdown, so if there are 6 text boxes, there will be a maximum of 6 entries in the dropdown

Do this:

1) All text boxes that are list items are prefixed with "LstGrp.", i.e., "LstGrp.Txt1",  "LstGrp.Txt2", "LstGrp.Txt3",etc.

2) Place this code into the "Custom Calculation" script for the dropdown:

var aValues = this.getField("LstGrp").getArray().map(function(a){return a.valueAsString;});

var aList = aValues.filter(function(a){return (a.length > 0);});

event.target.setItems(aList);

Now any values entered into the text boxes will appear as entries in the dropdown.

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 Beginner ,
Feb 26, 2019 Feb 26, 2019

if I do this

event.target.setItems(" ",aList);

will it insert a blank value on the first line? so that the dropdown will be viewed as blank/clear, until the user click on it.

thanks

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 ,
Feb 26, 2019 Feb 26, 2019

No, but you can insert a blank entry into aList

aList.unshift(" ");

event.target.setItems(aList);

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 ,
Feb 26, 2019 Feb 26, 2019

No, setItems only takes one parameter.

You can do it like this:

aList.unshift(" ");

event.target.setItems(aList);


Or even:

event.target.setItems([" "].concat(aList));

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 ,
Feb 28, 2019 Feb 28, 2019

the text were added to the dropdown list, however, whenever I select an item to the dropdown list, nothing is selected. only blank is selected.

when i used the first code:

//populate dropdown based on multiple textbox

var aValues = this.getField("LstGrp").getArray().map(function(a){return a.valueAsString;});

var aList = aValues.filter(function(a){return (a.length > 0);});

event.target.setItems(aList);

only the first item is selected.

is there something missing?

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 ,
Feb 28, 2019 Feb 28, 2019

Where does you use the code?

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 ,
Feb 28, 2019 Feb 28, 2019

code is used to populate the dropdown list, based on several textbox entries

see the image above

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 ,
Feb 28, 2019 Feb 28, 2019

Where is the image with the code?

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 ,
Feb 28, 2019 Feb 28, 2019

the code is inserted to the dropdown as suggested

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 ,
Feb 28, 2019 Feb 28, 2019

You've said multiple things in your post.

"nothing is selected"  , "only blank is selected", "only the first item is selected"

The "setItems()" function uses the first item in the list as the selection. The code is doing exactly what it is supposed too.

If you want something else, then the value of the list must be set with another 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
Community Beginner ,
Feb 28, 2019 Feb 28, 2019

sorry for the confusion.

after it populate the dropdown list, user should be able to choose one among the items in the dropdown list.

i didn't know that it won't allow to select other items aside from the first item in the list.

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 ,
Feb 28, 2019 Feb 28, 2019

what should i add to the line of code so that all the items in the dropdown list will be selectable? thanks

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 ,
Feb 28, 2019 Feb 28, 2019

All items in the dropdown are selectable. That is not the problem.

The issue is that this script is a calculation. The script is run anytime any field on the form changes.

There are 3 solutions:

1)  Change the script to a validation script on the "LstGrp" text fields

2)  Qualify the calculation by testing the source of the event. In this case it should be one of the "LstGrp" fields

3)  (poor solution) Save the value of the list before changing the list items, then set it.

I think #2 is the best one since it blocks unnecessary updates and only changes code in one location.

Put the code in this if statement

if(/^LstGrp/.test(event.source.name))

{

    .. The script...

}

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 Beginner ,
Mar 01, 2019 Mar 01, 2019

if(/^LstGrp/.test(event.source.name))

{

//populate dropdown based on multiple textbox

var aValues = this.getField("LstGrp").getArray().map(function(a){return a.valueAsString;});

var aList = aValues.filter(function(a){return (a.length > 0);});

aList.unshift(" ");

event.target.setItems(aList);

}

code works perfectly. thank you.

one last question:

i have 10 dropdowns and it name like this ddDistributionSystemLocation.1 up to ddDistributionSystemLocation.10.

how can I make them readonly = true using array or loop to make the code shorter instead of using this line of codes:

this.getField("ddDistributionSystemLocation.1").readonly = true;

up to

this.getField("ddDistributionSystemLocation.10").readonly = true;

thanks

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, 2019 Mar 01, 2019

Just use the group name. It will set the value for all fields in the group.

this.getField("ddDistributionSystemLocation").readonly = true;

this technique only works for some parameters.

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 Beginner ,
Mar 01, 2019 Mar 01, 2019

thanks. appreciate all the help

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 04, 2019 Mar 04, 2019

how will I reset the dropdown to blank, when I reset the whole form?

the last entered values still appear to the dropdown list.

i tried adding the code

this.getField("ddWindowsWallAssignment").setItems(" ");

to the reset button

but it won't still remove the list from the dropdown list

thanks

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 04, 2019 Mar 04, 2019

Use the clearItems method, like this:

this.getField("ddWindowsWallAssignment").clearItems();

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 04, 2019 Mar 04, 2019

i also tried that one. it doesn't clear the list

it will only clear the textbox, but the last entered value still appear on the dropdown list

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 04, 2019 Mar 04, 2019

Then there is an error in your code.

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