Skip to main content
Inspiring
September 22, 2023
Answered

Conditional hiding of dropdown field values based on previous dropdown field selection

  • September 22, 2023
  • 3 replies
  • 2349 views

Eventually, this script will have to cover nearly 100 dropdown fields (one for every ship selectable).  The end-state is to minimize the required scrolling, disallow duplicate selections, and hopefully allow for first letter(s) input to jump to through the alphabetical listing.

Is there even a way to conditionally hide dropdown values based on previous selections?  I have been tinkering with this for a week now and am no closer to making it work.  Below is the code I've been attempting to implement and attached is my working test file.

My sincerest thanks in advance for your time and any and all assistance.

 

DROPDOWN 01 - replicated & modified for each successive dropdown.

var dropdown1 = this.getField("Drop01");
var dropdown2 = this.getField("Drop02");
var dropdown3 = this.getField("Drop03");
var dropdown3 = this.getField("Drop04");
dropdown1.setAction("Keystroke", function() {
var selectedValue1 = dropdown1.value;
if (selectedValue1 === "Reagan") {
dropdown2.setItems(["Enterprise", "Roosevelt", "Stennis"]);
} else if (selectedValue1 === "Enterprise") {
dropdown2.setItems(["Reagan", "Roosevelt", "Stennis"]);
} else if (selectedValue1 === "Roosevelt") {
dropdown2.setItems(["Reagan", "Enterprise", "Stennis"]);
} else if (selectedValue1 === "Stennis") {
dropdown2.setItems(["Reagan", "Enterprise", "Roosevelt"]);
} else {
dropdown2.setItems(["Reagan", "Enterprise", "Roosevelt", "Stennis"]);
}
});

 

This topic has been closed for replies.
Correct answer Nesa Nurani

If you use this as 'Validate' script, it will reject duplicate selection:

if(event.value !== " " && event.value !== ""){
for( var j=1; j<=4; j++){
var f2 = j < 10 ? "Ship" + "0" + j : "Ship" + j;
if(this.getField(f2).value == event.value){
event.rc = false;
break;}}}

Change 4 to the number of "Ship" fields you have.

3 replies

Nesa Nurani
Community Expert
September 22, 2023

Use this as Document level script:

function editList(){
  var num = parseInt(event.target.name.split(/[A-Za-z]+/)[1], 10);
  num++;
  var f = num < 10 ? "Ship" + "0" + num : "Ship" + num;
  var fname = this.getField(f);
  var cList = [" ", "Reagan", "Enterprise", "Roosevelt", "Stennis"];
  
  if (event.value === " ")
    fname.setItems(cList);
  else{
    var filteredList = [];
    for(var i=0; i<cList.length; i++){
      if (cList[i] !== event.value)
        filteredList.push(cList[i]);}
  fname.setItems(filteredList);}}

Call function from every field keystroke script:

if(event.willCommit){
 editList();}
Inspiring
September 22, 2023

I am absolutely floored at how clean and elegant the solution you have provided. Please accept my admiration and sincerest appreciation.  Thank you! 

Joel Geraci
Community Expert
September 22, 2023

I actually set up something similar to what you are asking for in one of my demo files. See the dropdowns in the attached PDF.  Select a meal and a second dropdown will appear with all of the same options as the first except the one selected. If that's what you were looking for, I can give you a detailed reply on how to tune it to your needs.

try67
Community Expert
September 22, 2023

Why are you using the setAction method for this? This just complicates things, as you need to apply the code as a string. Just apply the code directly to the field. Also, replace this:

var selectedValue1 = dropdown1.value;

With:

var selectedValue1 = event.value;

Inspiring
September 22, 2023

Frankly, my forays into the FM (f*cking magic) of Arcobat scripting are the result of the bright idea fairy visiting the brass and my inability to quit or cry uncle -- which the brass clearly takes advantage of. Unfortunately, they refuse to execute on my advice to hire a subject matter expert to work the FM... despite being told and given evidence of my insane level of effort long before I ask for an assist in these forums. Last time, the response received was "you should've just gone to the forums to begin with" which resulted in an argument they should hire or contract someone who actually knows what they are doing... they've been told and shown the correspondence proving if it weren't for you, Nesa, Bebarth, Bernd, and ls_rbls having provided extreme assistance over the past 12+ months, they would not have been able to keep the promises they make when I'm not around to manage expectations.  As a CT/AT/FP knuckledragger throughout my military career you can guess my worldly experience is clearly not condusive to understanding the arcane intricacies of Acrobat scripting... and thus far, there has only been one item I've had to tell them was impossible based on a month of research and my refusal to post in the here.

All that being said, I truly appreciate the folks here who have made the magic happen and I have learned quite a bit in the process -- although clearly not enough as my example script above puts on public display. 😉