cancel
Showing results for 
Search instead for 
Did you mean: 

How to retrive the Selected Value from DropDownByKey UI Element

Former Member
0 Kudos

Hi

I have DropDownByKey UI element.to which i am adding values dynamically Following way:

IWDAttributeInfo attinfo = wdContext.getNodeInfo().getAttribute("name");

ISimpleTypeModifiable modinfo = attinfo.getModifiableSimpleType();

IModifiableSimpleValueSet valset = modinfo.getSVServices().getModifiableSimpleValueSet();

valset.put(new String("name"),"Germany");

valset.put(new String("name1"),"India");

-


when i select perticular value from DropDownlist,and when i click on submit button,i wanted to retrive selected value in view controller.fo which i did following way

-


wdContext.currentContextElement().getAttributeValue("name").toString());

-


but i am getting "name1" value.but i am expecting "India" .How to get required value?

Please hel[p

Thanks

Prasad

Accepted Solutions (1)

Accepted Solutions (1)

thomas_chadzelek
Participant
0 Kudos

Hello,

as the name "DropDownByKey" suggests, the selection is handled via locale-independent keys ("name1" in your example). This is a good thing if you like to base conditions on the selection. If, for some other reason, you like to retrieve and output the human-readable locale-dependent description of that key ("India" in your example), just retrieve it from the value set:

Object key = wdContext.currentContextElement().getAttributeValue("name");

String text = valset.getText(key);

You probably like to do this in an action's event handler, not in wdDoModifyView()!

Best regards,

Thomas

Former Member
0 Kudos

Hi Thomas,

But doesn't he need the value corresponding to the selected key in the dropdown ? And which action / event handler did you mean one can use in this case ? Is there an action that is triggered when the user selects a particular value from the dropdown ?.. if that is the case, I totally agree that the wdDoModifyView() method doesnt need to be used.

Also is there a simliar action/event that is triggered when a particular value from a value help is used ?

Regards,

Navneet.

thomas_chadzelek
Participant
0 Kudos

Hello,

I understand the original code as follows: the IWDDropDownByKey's selectedKey property is already bound against the top-level attribute "name". You can retrieve the currently selected key by wdContext.currentContextElement().getAttributeValue("name") or wdContext.currentContextElement().getName() (in case you have generated typed accessors).

There is an event, namely IWDDropDownByKey.onSelect, fired whenever the selection has been changed. In reaction to that, you can retrieve the new selection from the context and then, e.g. for messages or display in UI, retrieve the key's description from the data type. That will be thread-safe and respect the current session locale. By the way: you can also do this without a IModifiableSimpleValueSet, there are also unmodifiable counterparts to those interfaces for read access only.

Best regards,

Thomas

Former Member
0 Kudos

Hi Thomas,

Will surely try what you said abt the dropdown element and the onSelect() action. But in my case I wanted to retrieve the desrciption based on the selected key in a value help. I didnt find any event or action that corresponds to a selection in the value help and finally had to retrieve values from that static hashmap in the wdDoModifyView() since I found tht to be the only method that is called when the selection is made.

Also could you please elaborate on the connection between the static hashmap & the current user session ?

Regards,

Navneet.

thomas_chadzelek
Participant
0 Kudos

Hello,

if you are talking about value help (EVS), then I guess you are also talking about IWDInputField, not IWDDropDownByKey, right? There is no event in that case. But I guess you have other appropriate actions like "Save" where you can write that code. Not sure, though, what you like to achieve in the end.

A static HashMap will only contain texts for one locale, obviously. In a productive system, you will have many concurrent sessions even for different locales. Access the value set via the attribute's data type and everything is fine!

Best regards,

Thomas

Former Member
0 Kudos

Hi Thomas,

Yes I am talking about EVS in an input field.

My requirement was as follows -

1) There is an input field in the view, for which an EVS is created.

2) This EVS is populated using the standard IModifiableSimpleType technique with values coming from an RFC.

3) The <i>"key"</i> in the EVS is a partciular account number like "0019102220" and the description of this account; eg. "Account for travel expense re-imbursements" is its <i>"value"</i>.

4) Now my requirement is that when the user opens the value help, and selects a particular key from the value help, the input field should contain the Account number, but the user should also be able to see the description of that account in a text view that is placed on the right of the input field.

5) In the code snippet that I mentioned in my very first reply, I use the "strValue" and set it to a context attribute that is bound to the above textview.

Also, as you said, I didnt find any event/method/action that caters to a value selection from an EVS, but just the wdDoModifyView() hook. And inside the wdDoModifyView() method, I can access only static attributes - hence the static HashMap. Do you think it will be a better idea if - instead of creating a static HashMap in the "others" section, I declared a ContextAttribute of type "HashMap" in the view context and used it ? Will this method comply with the WDP session that you talked about ?

Also, as a note.. I would like to clear that fact that my application will be used only in one locale... English. (Did you mean anything else when you said that static variables will be locale specific?)

Also I didnt understand what you meant by the "Save" action ?!

Regards,

Navneet.

thomas_chadzelek
Participant
0 Kudos

Hello,

"my application will be used only in one locale... English": do you really want to bet on that? I wouldn't.

For your use case, I recommend to bind the IWDTextView.text property against a calculated attribute of type string. There you map key to description, by accessing the value set from the attribute's data type. Note that this value set <b>is</b> the HashMap we are talking about here, no need to recreate it! Save the effort for other features.

Best regards,

Thomas

P.S.: Even inside wdDoModifyView(), one can still access the context. And from there, you can navigate to the attribute info and on to its data type and then to the value set. But in your case, the calculated attribute is the way to go!

Former Member
0 Kudos

Hi,

Always thought there would be a better way of doing it.. and even abt the duplicate values in the hashmap and the valueset. I'm not very familiar with calculated attributes and the works... but will definitely try out your method and get back to you.

Thanks for the great help !

Regards,

Navneet.

Answers (4)

Answers (4)

Former Member
0 Kudos

if you dont need name1, name2 etc,

why do u use them

use like the below

valset.put("Germany","Germany");

valset.put("India","India");

Former Member
0 Kudos

Hi Prasad,

1) Create a GLOBAL STATIC HashMap "dropDownMap" in your controller's "others" section (at the very bottom of your controllerm, you will see a whole lot of comments which say that this section isnt controlled by the webdynpro runtime... etc etc).

2) In the loop in which you create the Simple Value set for your drop down, along with populating the SimpleValueSet, also include an EXTRA STATEMENT where you PUT VALUES into the above hashmap as follows -

valset.put(new String("name"),"Germany");
dropDownMap.put(new String("name"),"Germany");
valset.put(new String("name1"),"India");
dropDownMap.put(new String("name1"),"India");

3) The wdDoModifyView method will ideally be called even when a particular value from the dropdown is selected. But wdDoModifyView can refer only to Static objects, thats why in step1, the hashmap has to be declared as "static". In the wdDoModifyView, get the key from the context attribute in which your value help/DropDownByKey appears <u>and retrieve the VALUE from the hashmap on the basis of this key</u>. In your case, the code should look something like -

wdDoModifyView() {
static String strValue = dropDownMap.get(wdContext.currentContextElement().getName()).toString();
}

4) "strValue" should contain "India" or "Germany" as your exanple goes

This method worked for me when I wanted to retrieve values from a ValueHelp. Hope it works even in case of your dropdown !

regards,

Navneet.

Former Member
0 Kudos

Bad advice.

Don't use static variables (by the way: you cannot declare a static variable inside a method as your code suggests).

Don't use method wdDoModifyView() to access UI element properties.

Of course, you can maintain the key/value-pairs in a map or a separate class to be able to get the display text from the selected key.

Armin

Former Member
0 Kudos

hi Armin,

I agree that the static variable cannot be defined in the wdDoModifyView(). Just cross checked it with my code, and I have defined that variable as static in the "others" section like the hashmap.

But I still havent understood what UI Element property I am accessing in that code snippet ? I am only setting a context attribute's value with values taken from a hashmap.. please advice.

Regards,

Navneet.

thomas_chadzelek
Participant
0 Kudos

Hello,

I strongly recommend against this static HashMap. The value set is already contained in the data type and could well be session-specific. Especially the session locale definitely is! Thus you cannot successfully retrieve the text in this way in a productive environment.

Cf. my answer and retrieve the text from the data type.

Best regards,

Thomas

Former Member
0 Kudos

Hi Prasad,

Check this posting

Regards,Anilkumar

Former Member
0 Kudos

First, you should access the context attribute using the generated constant IContextElement.NAME (or the like) instead of using the string literal "name".

Second, the new String(...) is not needed, use the string literal alone.

If you use a DropDownByKey, you have to bind the "selectedKey" property to some context attribute, in this case the attribute "name".

To read this attribute from the context, use

String selectedKey = wdContext.currentContextElement().getName();

This will give you the key (e.g. "name"). To get the value (e.g. "Germany"), you have to use the value set again and ask for the selected key's text.

Do you really need the text instead of the key for further processing?

Armin

Former Member
0 Kudos

Hi Armin

Thanks for immidiate reply.but i want text only instead of key.How to do this?

Thanks

Prasad