Expand my Community achievements bar.

SOLVED

How to use a switch case function to reference options that is used numerous times in the previous drop-down?

Avatar

Level 2

I am using JavaScript Coding in LiveCycle and developing dependent drop-down boxes but I am having difficulty writing the correct code. See example below. I am trying to use a multiple case function that references an option that is listed numerous times in the previous drop-down. The problem that I am running into is that when I reference Chevy or Nissan and any color, it is returning the results of the color under the "Ford" column only. I will still see the color options (Blue, Black, Green and Purple) in my second drop down and it looks fine, but then the third drop-down only references "Ford" colors and options. (I did not list the code for the third drop-down).

I want the customer to choose Chevy or Nissan and then have its dependent drop downs (based on the color they chose), but I cannot seem to make this work.

I hope this is clear enough (probably as mud…). I would greatly appreciate any help!!

form1.#subform[0].#subform[1].cboModels::exit - (JavaScript, client) 

cboColor.clearItems(); 

switch(this.rawValue){ 

case "Ford": 

  cboColor.addItem("Blue"); 

  cboColor.addItem("Black"); 

  cboColor.addItem("Green"); 

  cboColor.addItem("Purple"); 

  break; 

case "Chevy": 

  cboColor.addItem("Blue"); 

  cboColor.addItem("Black"); 

  cboColor.addItem("Green"); 

  cboColor.addItem("Purple"); 

  break; 

case "Nissan": 

  cboColor.addItem("Blue"); 

  cboColor.addItem("Black"); 

  cboColor.addItem("Green"); 

  cboColor.addItem("Purple"); 

  break; 

form1.#subform[0].#subform[1].cboColor::exit - (JavaScript, client)

cboPrice.clearItems(); 

switch(this.rawValue){ 

  case "Ford": 

  case "Green": 

  cboPrice.addItem("blah"); 

  cboPrice.addItem("blah"); 

  cboPrice.addItem("blah"); 

  cboPrice.addItem("blah"); 

  break; 

  case "Chevy": 

  case "Green": 

  cboPrice.addItem("blah"); 

  cboPrice.addItem("blah"); 

  cboPrice.addItem("blah"); 

  cboPrice.addItem("blah"); 

  break; 

  case "Nissan": 

  case "Green": 

  cboPrice.addItem("blah"); 

  cboPrice.addItem("blah"); 

  cboPrice.addItem("blah"); 

  cboPrice.addItem("blah"); 

  break; 

}

1 Accepted Solution

Avatar

Correct answer by
Level 10

You can use "\n" for a new line in a string, so

"Option 1,\nOption 2,\nOption 3";

Would put each one on its own line.

Bruce

View solution in original post

8 Replies

Avatar

Level 10

Hi,

The switch case expression can't reference the two fields (cboModels and cboColor) like that and you probably don't want to populate the cboPrice in the exit event of cboColor in case they go back and change cboModel the cboColor exit event wont fire.

So try the following in the preOpen event of cboPrice.

switch(true){ 

  case cboColor.rawValue == "Green" && cboModels.rawValue == "Ford": 

    cboPrice.addItem("blah"); 

    cboPrice.addItem("blah"); 

    cboPrice.addItem("blah"); 

    cboPrice.addItem("blah"); 

    break

  case cboColor.rawValue == "Green" && cboModels.rawValue == "Chevy":  

    cboPrice.addItem("blah"); 

    cboPrice.addItem("blah"); 

    cboPrice.addItem("blah"); 

    cboPrice.addItem("blah"); 

    break

  case cboColor.rawValue == "Green" && cboModels.rawValue == "Nissan":  

    cboPrice.addItem("blah"); 

    cboPrice.addItem("blah"); 

    cboPrice.addItem("blah"); 

    cboPrice.addItem("blah"); 

  break

etc...

Bruce

Avatar

Level 2

BR001_ACP,

This worked like a charm! Thank you for all of your help!

I do have one more question.

I would like for the final drop down selection to automatically populate a list or text field, but I am unsure how to write the code. I have this (see below), but it only shows the last option in the text field (Option 3). Is there anyway to have the field show all three?

Text Field is cboPartNumber

switch(this.rawValue){

  case cboModel.rawValue == "Nissan" && "Green":

   cboPartNumber.rawValue = ("Option 1");

   cboPartNumber.rawValue = ("Option 2");

   cboPartNumber.rawValue = ("Option 3");

   break;

}

Again, I appreciate all of your help!

Avatar

Level 10

Hi,

Maybe the simplest way would be the following JavaScript in the calculate event of the text field cboPartNumber;

switch(true){

  case cboColor.rawValue == "Green" && cboModels.rawValue == "Nissan":

    "Option 1, Option 2, Option 3";

  break;

...

}

There probably are better ways but would depend on the data, ... like are you part numbers always unique for a make/color combination?

Bruce

Avatar

Level 2

Bruce,

Good morning. Yes, the part numbers are unique. Is there any way to have them listed on top of each other? Such as:

Option 1

Option 2

Option 3

etc.

Avatar

Correct answer by
Level 10

You can use "\n" for a new line in a string, so

"Option 1,\nOption 2,\nOption 3";

Would put each one on its own line.

Bruce

Avatar

Level 2

Bruce,

Thank you! So simple, should have known..

I want to thank you for all of your help thus far. I have one last question. I would like a message box to pop up if a certain option is selected in the drop down. Lets say they select "Option 3" under "Green and Nissan". Currently, I have a list box that automatically populates with the information I want after this selection is made, but I would also like a message box to popup on certain options. Is this possible?

Avatar

Level 10

You should be able to add some code into the calculate event of a subform (so it doesn't actually return a value, but does get executed every time one of the referenced fields changes).  Choice any subform, but one that is a parent of all three fields is probably best.

So something like;

if (cboModel.rawValue == "Nissen" && cboColor.rawValue == "Green" && cboPartNumber.rawValue == "Option 3")

{

    xfa.host.messageBox("message ...")

}

Bruce

Avatar

Level 2

Bruce,

This worked great! Again, thank you. You've been a tremendous help!