Acrobat Pro DC - Dropdown list always keeps the first item on the list instead of the selected item

Community Beginner ,
Jun 21, 2020 Jun 21, 2020

Copy link to clipboard

Copied

Hello, community. 🙂

 

I am creating a fillable form with some calculation and dependent options.

But, I am encountering some problems. I already searched Google, here in the forum (in all topics related to dropdown), stack overflow ... And I still couldn't solve it.

The first problem, and the most annoying: I create a dropdown that is dependent on the values ​​of other 3 dropdown lists, and that changes its content according to the choices of the previous dropdowns.
I managed to generate the dropdown, with great difficulty, but when I click on an option, it does not keep the selected item, showing only the first item in the list.

The second problem is that the list items only appear after I click on the dropdown and not before. It is blank / empty and, after I click it, it displays the result.

I already checked the option to commit immediately after choosing, however, this did not solve the problem.

I read, in another topic, that the "setItem ()" method is not the most suitable, but I only know the setItem and addItem and the most indicated, it seems to me, is the setItem.

 

So, the first dropdown is "Classe", the second is "Nível" and the third is "Tendência", and the "problematic" dropdow is "especializacao" (NOT the "Especialização" « that one is independent and static).

PDF -  001.png

 

CLASSE

PDF -  005.pngPDF -  006.pngPDF -  007.png

NÍVEL

PDF -  008.pngPDF -  009.pngPDF -  010.png

 

TENDÊNCIA

PDF -  011.pngPDF -  012.pngPDF -  013.png

 

ESPECIALIZACAO

 

PDF -  002.pngPDF -  003.pngPDF -  004.png

 

The script

Format - Custom - Custom Format Script

 

 

 

 

 

 

 

 

 

 

//Format - Custom - Custom Format Script
var c = getField("Classe").value;
var n = getField("Nível").value;
var t = getField("Tendência").value;
var esp = this.getField("especializacao");

var e;

if(c == "Classe" || c == "  " || n == "Nível" || n == "  " || n < 5 || t == "Tendência" || t == "  "){
	e = '';
	this.getField("especializacao").clearItems();
}

else{
	
	if(c == "Ladrão") e = ["Especialização", "Ranger", "Bardo", "Assassino"];
	
	else if(c == "Clérigo"){ 
		if(t == "Caótica") e = ["Especialização", "Druída", "Caçador", "Ocultista"];
		else e = ["Especialização", "Druída", "Caçador"];
	}
	
	else if(c == "Homem de Armas"){
		if(t == "Ordeira") e = ["Especialização", "Paladino", "Guerreiro", "Bárbaro"];
		else e = ["Especialização", "Guerreiro", "Bárbaro"];
	}
		
	else if(c == "Mago"){
		if(t == "Caótica") e = ["Especialização", "Ilusionista", "Adivinhador", "Necromante"];
		else e = ["Especialização", "Ilusionista", "Adivinhador"];
	}
	
	else e = '';
		
}

esp.commitOnSelChange = true;
esp.setItems(e);

 

 

 

 

 

 

 

 

 

 

 

Format - Custom - Custom Keystroke Script

 

 

 

 

 

 

 

 

 

 

 

//Format - Custom - Custom Keystroke Script

if( event.willCommit ){

	if(event.value === '') this.getField("especializacao").clearItems();
	else SetFieldValues(event.value);

}

 

 

 

 

 

 

 

 

 

 

 

Here the dropdown "working"

 

Still ... It takes 3 minutes to load for nothing and, in the end, I only have the first option on the list instead of the entire list.

 

By the way: it is necessary to display the choice from the list, it must be possible to change the choice, and if the "Nível" drops from 5, the dropdown should be empty again.
And, to make my situation worse, the result of this dropdown will fill a field.

 

Update: Working code

 

var c = getField("Classe").value;
var n = getField("Nível").value;
var t = getField("Tendência").value;
var esp = this.getField("especializacao");

if (event.source && (event.source.name=="Classe" || event.source.name=="Nível" || event.source.name=="Tendência")) {

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

	if(c == "Ladrão" && n > 4) esp.setItems([["Especialização", "esp"],["  ","  "],["Ranger", "ran"],["Bardo", "bar"],["Assassino", "ass"]]);
	
	else if(c == "Clérigo" && n > 4){ 
		if(t == "Caótica") esp.setItems([["Especialização", "esp"],["  ","  "],["Druída", "dru"],["Caçador", "cac"],["Ocultista", "ocu"]]);
		
		else if(t == "Ordeira" || t == "Neutra") esp.setItems([["Especialização", "esp"],["  ","  "],["Druída", "dru"],["Caçador", "cac"]]);
		
		else this.getField("especializacao").clearItems();
	}
	
	else if(c == "Homem de Armas" && n > 4){
		if(t == "Ordeira") esp.setItems([["Especialização", "esp"],["  ","  "],["Paladino", "pal"],["Guerreiro", "gue"],["Bárbaro", "brb"]]);
		
		else if(t == "Neutra" || t == "Caótica") esp.setItems([["Especialização", "esp"],["  ","  "],["Guerreiro", "gue"],["Bárbaro", "brb"]]);
		
		else this.getField("especializacao").clearItems();
	}
		
	else if(c == "Mago" && n > 4){
		if(t == "Caótica") esp.setItems([["Especialização", "esp"],["  ","  "],["Ilusionista", "ilu"],["Adivinhador", "adi"],["Necromante", "nec"]]);
		
		else if(t == "Ordeira" || t == "Neutra") esp.setItems([["Especialização", "esp"],["  ","  "],["Ilusionista", "ilu"],["Adivinhador", "adi"]]);
		
		else this.getField("especializacao").clearItems();
	}
	
	else this.getField("especializacao").clearItems();
	
}

 



For mor information look the comments below and the attached PDF. 🙂

TOPICS
Acrobat SDK and JavaScript, Create PDFs, PDF forms

Views

262

Likes

translate

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

correct answers 1 Correct Answer

Most Valuable Participant , Jun 22, 2020 Jun 22, 2020
Surround the code with this:if (event.source && (event.source.name=="Classe" || event.source.name=="Nível"|| event.source.name=="Tendência")) {// rest of code goes here}

Likes

translate

Translate

Translate
Most Valuable Participant ,
Jun 21, 2020 Jun 21, 2020

Copy link to clipboard

Copied

A couple of notes:

- There is no setItem method. I think you mean setItems.

- The parameter you supply to this method must be an array, not a string.

- I would use the Validation event, instead of the Format event.

- Make sure the option to commit the selected value immediately is ticked, under the Options tab of the Properties dialogs of your drop-down fields.

 

If you're interested, I've developed a (paid-for) tool that allows you to set up such "cascading" drop-downs very easily and quickly, without having to write any code. All you need is to have a spreadsheet with all the possible combinations and then you run the script on it and it sets it all up for you. You can find it here: http://try67.blogspot.com/2014/11/acrobat-create-cascading-dropdowns.html

I can also help you set up the last step you described, populating a text field based on the last value selected.

Likes

translate

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
Community Beginner ,
Jun 21, 2020 Jun 21, 2020

Copy link to clipboard

Copied

Hello, try67.

 

Thank you for your notes.

 

Answering your points:

- Yes. The correct is "setItems". My mistake.

- From what I researched, I thought I used an Array. OK. I will search better.

- In this case, don't I need to use the keystroke?

- It is.

PDF -  002.png

 

Thank you for the offer, but what I'm doing is nonprofit. So I appreciate it, but I don't want to pay for the help.

Thank you very much for your notes. They were enlightening. I will put them into practice immediately. 🙂

 

Likes

translate

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
Most Valuable Participant ,
Jun 21, 2020 Jun 21, 2020

Copy link to clipboard

Copied

- You've declared it at some places as an array and at others as a string. JS is very lenient with variable types and allows you to do that, but some methods require a value of a specific kind.

- I prefer to use the Validation event as you don't need to check if the value is going to be committed or not.

- PS. Did you check the JS Console for error messages?

Likes

translate

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
Community Beginner ,
Jun 21, 2020 Jun 21, 2020

Copy link to clipboard

Copied

Hello, again.

 

- I am using an Array.

var e = ["Especialização", "Ranger", "Bardo", "Assassino"];

 

- I changed from Format to Validate, but now it doesn't create the Dropdown list.

Likes

translate

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
Most Valuable Participant ,
Jun 21, 2020 Jun 21, 2020

Copy link to clipboard

Copied

- In this line you're not:

else e = '';

- Again, check the JS Console for errors.

 

Likes

translate

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
Community Beginner ,
Jun 21, 2020 Jun 21, 2020

Copy link to clipboard

Copied

Hello.

 

Sorry, try67. I had not seen your previous message talking about the linearity of the variables.

 

I searched on Google, I found the "Acrobat JavaScript Object
Specification "and I understood what I was doing wrong. I modified it according to the document.
However, the dropdown still doesn't keep the item I choose by moving to the top of the list.
Can I use the same code within Validate? Because when I copy and paste it, nothing happens. The dropdown does not create the item list.
I apologize, but I am completely noob in programming. I am learning while doing and researching.

 

The new version of my code:

 

var c = getField("Classe").value;
var n = getField("Nível").value;
var t = getField("Tendência").value;
var esp = this.getField("especializacao");

if(c == "Classe" || c == "  " || n == "Nível" || n == "  " || n < 5 || t == "Tendência" || t == "  "){

	this.getField("especializacao").clearItems();
}

else{
	
	if(c == "Ladrão") esp.setItems([["Especialização", "esp"],["Ranger", "ran"],["Bardo", "bar"],["Assassino", "ass"]]);
	
	else if(c == "Clérigo"){ 
		if(t == "Caótica") esp.setItems([["Especialização", "esp"],["Druída", "dru"],["Caçador", "cac"],["Ocultista", "ocu"]]);
		
		else esp.setItems([["Especialização", "esp"],["Druída", "dru"],["Caçador", "cac"]]);
	}
	
	else if(c == "Homem de Armas"){
		if(t == "Ordeira") esp.setItems([["Especialização", "esp"],["Paladino", "pal"],["Guerreiro", "gue"],["Bárbaro", "brb"]]);
		
		else esp.setItems([["Especialização", "esp"],["Guerreiro", "gue"],["Bárbaro", "brb"]]);
	}
		
	else if(c == "Mago"){
		if(t == "Caótica") esp.setItems([["Especialização", "esp"],["Ilusionista", "ilu"],["Adivinhador", "adi"],["Necromante", "nec"]]);
		
		else esp.setItems([["Especialização", "esp"],["Ilusionista", "ilu"],["Adivinhador", "adi"]]);
	}
	
	else this.getField("especializacao").clearItems();
		
}

esp.commitOnSelChange = true;

 

 

PDF -  024.pngPDF -  025.png

Likes

translate

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
Most Valuable Participant ,
Jun 22, 2020 Jun 22, 2020

Copy link to clipboard

Copied

This error message means that on line 23 of your calculation script you've referenced an incorrect field name.

It might not be related to this, but you should still fix it.

I had a quick look at your code above and I see some possible issues. You're still not being very consistent with the variable types. See this part, for example:

n == "Nível" || n == "  " || n < 5

So is the "n" variable a string or a number?

 

Also, this is not needed:

esp.commitOnSelChange = true;

You only need to set it once, it doesn't have to be set each time.

 

To be able to help you further I would really need to see the actual file, though.

Likes

translate

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
Community Beginner ,
Jun 22, 2020 Jun 22, 2020

Copy link to clipboard

Copied

Hello, try67.

 

This error message means that on line 23 of your calculation script you've referenced an incorrect field name.

It might not be related to this, but you should still fix it.

 

I modified the code and now the Console no longer displays any error messages. However, it still doesn't load the list automatically and it doesn't keep the chosen option either, skipping to the first item in the list.

 

See this part, for example:

n == "Nível" || n == "  " || n < 5

So is the "n" variable a string or a number?

 

That one is a string. Because I have "Nível", "  ", and numbes [1-20].

 

Also, this is not needed:

esp.commitOnSelChange = true;

You only need to set it once, it doesn't have to be set each time.

 

OK. Removed.

 

To be able to help you further I would really need to see the actual file, though.

 

Smoothly. In order to keep my focus I created a blank PDF and copied / pasted the four dropdowns into it and created a clear forms button.

I added this PDF to the original message.

Likes

translate

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
Most Valuable Participant ,
Jun 22, 2020 Jun 22, 2020

Copy link to clipboard

Copied

If it's a string then you can't use numerical operators on it, like "<".

Likes

translate

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
Most Valuable Participant ,
Jun 22, 2020 Jun 22, 2020

Copy link to clipboard

Copied

The problem is you put the code under the Format event of "especializacao", which is the field that you want to populate... That's not going to work. You need to place it as the custom validation script of the Tendência field, for example, or as the calculation script of a (hidden) text field, but add code to keep the current value of "especializacao" before applying the new items. This is not a simple task...

Likes

translate

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
Community Beginner ,
Jun 22, 2020 Jun 22, 2020

Copy link to clipboard

Copied

The problem is you put the code under the Format event of "especializacao", which is the field that you want to populate... That's not going to work. You need to place it as the custom validation script of the Tendência field, for example, or as the calculation script of a (hidden) text field, but add code to keep the current value of "especializacao" before applying the new items. This is not a simple task...

 

Hum... OK. I think I understand
Let me try. 

Likes

translate

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
Community Beginner ,
Jun 22, 2020 Jun 22, 2020

Copy link to clipboard

Copied

I created a text field to do the validation.
Now the list is created / loaded at the exact moment that I select the last parameter required.
However, the dropdown still does not maintain the chosen value.

Likes

translate

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
Most Valuable Participant ,
Jun 22, 2020 Jun 22, 2020

Copy link to clipboard

Copied

Yes, because you have to add a condition to only update the values if the source field is one of the other drop-downs, or you need to save the current value at the start of the script and re-apply it at the end, if it's still a part of the list.

Likes

translate

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
Community Beginner ,
Jun 22, 2020 Jun 22, 2020

Copy link to clipboard

Copied

I'm sorry, but now I don't understand what I need to do.
Can you share an example with me, please?

 

In time: I updated the attached PDF file.

Likes

translate

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
Most Valuable Participant ,
Jun 22, 2020 Jun 22, 2020

Copy link to clipboard

Copied

Surround the code with this:

if (event.source && (event.source.name=="Classe" || event.source.name=="Nível"
|| event.source.name=="Tendência")) {

// rest of code goes here

}

Likes

translate

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
Most Valuable Participant ,
Jun 23, 2020 Jun 23, 2020

Copy link to clipboard

Copied

.

Likes

translate

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
Community Beginner ,
Jun 22, 2020 Jun 22, 2020

Copy link to clipboard

Copied

I'm sorry. But I do not understand.
I changed the initial validation (the first if) to this:

 

if(c == "Classe" || c == "  " || n == "Nível" || n == "  " || t == "Tendência" || t == "  ") this.getField("especializacao").clearItems();

 

And it continues to behave the same way.
It continues to load the list only after I click on the dropdown and it still doesn't keep the chosen value.

Do you have the opportunity to look de PDF file? What am I doing wrog?

Likes

translate

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
Community Beginner ,
Jun 26, 2020 Jun 26, 2020

Copy link to clipboard

Copied

Thank you very much @try67 for all the help given to help me solve my problem.

I updated the PDF with the proposed solution applied and working. 🙂

Likes

translate

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