cancel
Showing results for 
Search instead for 
Did you mean: 

Change in dropdown values based on input value

shailesh1
Explorer
0 Kudos

Hi,

I have created one application in web dynpro Java, where i have one input field and one dropdown.

My question is, how to change the dropdown values depending on the input field.

Ex: if the input field is country name and dropdown is states name, then when i give the country name, the respective state names should be reflected in the dropdown. as i change the country name, state names also should change.

Please provide me the solution for it.

Thank you

Accepted Solutions (1)

Accepted Solutions (1)

psnn24
Explorer
0 Kudos

Hi Shailesh,

You can use drop-down instead of text field for your scenario.

please follow the steps:

Create the context node and attribute as in the image

note that State node comes under Country node.

Change collection cardinality to 0..n and selection cardinality to 0..1 for both the nodes

Change the "Singleton" property of State node to false.

The Country and State attributes are type of String.

Now add two DropDownByIndex UI elements and bind the Country attribute to first one and State attribute to the second.

Since you need to to change the values in the 'State' based on the 'Country' selected, create an event for Country drop-down onSelect. no need of putting any code in the event handler.

Add this code in the wdDoInit() method of the controller

String []country = {"India","USA","UK"};

//Country names; i have taken 3 countries so i'll be having multidimensional States array with 3 objects inside it

      String[][]states = {{"Karnataka","Andhra Pradesh","TamilNadu","Kerala","Goa"},

                                                                  {"Washington","NewYork","Chicago","Arizona","Houston"},

                                                                  {"lancashire","Middlesex","Nottingham","Birmingham","Sheffield"}}; 

      IPrivateTestDropComp1View.ICountryNode countryNode = null;

      IPrivateTestDropComp1View.ICountryElement countryEle = null;

      IPrivateTestDropComp1View.IStateNode stateNode = null;

      IPrivateTestDropComp1View.IStateElement stateEle = null;

      for (int i = 0; i < country.length; i++) {     

            countryNode = wdContext.nodeCountry();

            countryEle = countryNode.createAndAddCountryElement();

            countryEle.setCountry(country[i]); //Set the country in ith position and set the states in i,jth position

            for(int j= 0; j < states[i].length; j++){

                      stateEle = countryNode.getCountryElementAt(i).nodeState().createAndAddStateElement();

                stateEle.setState(states[i][j]);

            }

      }

      wdContext.nodeCountry().setLeadSelection(0);

      wdContext.nodeState().setLeadSelection(0);

deploy and run. Hope it helps you. Contact if you have any problem

Regards,

Prasanna

Former Member
0 Kudos

It works fine. Thanks a lot. Will try out more things..

shailesh1
Explorer
0 Kudos

Hi Prasanna,

there is some problem. i hope you can help me to resolve this problem. i have created the app as per you told but now the problem is, states name is not changing with respect to the country name. for all the selected country name, i am getting same states name (corresponding to the first country name).

i.e, even if i select USA and UK, i am getting

"Karnataka","Andhra Pradesh","TamilNadu","Kerala","Goa" names in dropddown.

Thank You

Sharathmg
Active Contributor
0 Kudos

1. Invoke-bind an action( to load country) on the select dropdown - country

2. In the action, get the value of the currentelement - country i.e. country name

    

     Clear the values of the current states dropdown list

     Then, load the new states for the selected country.

Regards,

Sharath

psnn24
Explorer
0 Kudos

Hi Shailesh,

     I think you haven't created action for the 'Country' drop down. Since you need to change values in 'State' drop down based on the selected 'Country', you should have an event handler for 'onSelect' of the 'Country' drop down.

Select the Drop down >> Properties >> Events >> onSelect >> Create

No need of putting any code inside the event handler.

Regards,

Prasanna

shailesh1
Explorer
0 Kudos

Hi Prasanna,

Yes. i didnt give any event for the dropdown. now i got. Thank you.

Regards,

Shailesh

Answers (2)

Answers (2)

former_member197475
Active Contributor
0 Kudos

Hi Shailesh,

Please follow the below thread to populate the values.

http://scn.sap.com/thread/556251

BR,

RAM

Sharathmg
Active Contributor
0 Kudos

Preferred option : Two drop downs. In country dropdown ui element, on select event, call the method to load values for the city.

If you want to use input field, based on the version you will have :

OnEnter event or OnChange event,

In these events, call the method to load the other dropdowns.

Now, to load dropdowns, refer sap help - dropdown by index / Key element.

Regards,

Sharath