cancel
Showing results for 
Search instead for 
Did you mean: 

Get the selected item from DropdownBox

Former Member
0 Kudos

Hi guys,

I have a DropdownBox with just one item. SAPUI5 selects this item by default, but doesn't fire event and hence the model behind is not updated. This is probably a bug that needs to be fixed.

However, on form submit, I want to simulate such event, but it looks hard to me to get the selected item, because there's no such method getSlectedItem(). There is getSelectedKey(), getSelectedItemId(), getItems(), but not getSelectedItem(). The only way that comes to my mind is to get the selected key, iterate through all the items and find the one with the given key.

My question is is there any simpler way to get the selected item? And since I need this to fire a change event, which in turn will probably update the model, is there already a function which I can call to force an UI control to update the model property, which it is bound to.

Best Regards

Ivan

Accepted Solutions (0)

Answers (3)

Answers (3)

0 Kudos

Hi Ivan,

use oDropdownBox.getLiveValue();

it works 100%

Regards,

Bheki

I311295
Associate
Associate
0 Kudos

Thanks Bheki. Your solution worked like a charm.

former_member191810
Participant
0 Kudos

Hi!

Have you try this on jQuery:

$("DropdownBox1").val();

Former Member
0 Kudos

Hi Raul,

This just returns the value in the combobox as a string, and not as sap.ui.core.ListItem.

Ivan

Former Member
0 Kudos

i `ve set the position of the element in dropdownBox as invisible key attribute.

why the dopdownbox can only carry 100 elements ??

greetings

Former Member
0 Kudos

maybe you can get the listitem with the stored key and the getItems() method of the dropdownBox.

getItems()[key]

greetings

former_member191810
Participant
0 Kudos

Ok.

Is a dropdownbox right?

I think you might have to combine some methods:

mydropdownbox.getSelectedItemId(): for catch the Id of the selectec item.

mydropdownbox.getItems(): it returns a sap.ui.core.ListItem[] (an array) and with the Id you get the selected element from the array.

I hope this can help you.

Former Member
0 Kudos

Hi Raul,

What you suggested will probably work. My question was if there is already a method I can call. Here it is:

The only way that comes to my mind is to get the selected key, iterate through all the items and find the one with the given key.

My question is is there any simpler way to get the selected item?

However, I already found another solution which I find pretty acceptable, so I don't need to get the selected item anymore.

Ivan

Former Member
0 Kudos

Hy Ivan nevertheless you could try out the oModel.getProperty("<FieldName>", context); Method...

I use it working with a table where i get the selectedRowContext as parameter from the eventhandler ( event.getParameter("<>")) but its hard to find out which parameter are given, for dropdownBox  for example the "newValue" parameter....

greetz

Former Member
0 Kudos

Hello Ivan,

have you found out how to fire events from DropdownBox meanwhile ?

Thanks for reply.

Greetings Chris

Former Member
0 Kudos

ok i`ve found out how to get an event from dropdownBox.

you can use the change event from sap.ui.commons.TextField

var cockpitDropdown = new sap.ui.commons.DropdownBox(this.createId("cockpitDropDown"),{

                          searchHelpEnabled: true,

                          change: oController.doSomething

              });

next question to solve is how get the Index from selected Item.

Former Member
0 Kudos

Hi Christoph,

I solved my problem using different approach. I added one empty item in the beginning, so the user should select the option manually. On manual selection the appropriate event gets fired.

oDictDetails.oCbTargetLang = new sap.ui.commons.DropdownBox('cb-dict-targetlang', {

                          value: "{/targetlang}",

                          editable:false,

                          selectedKey:undefined,

                          selectedItemId:undefined,

                          items: [

                                  new sap.ui.core.ListItem({text: "", key: undefined}),

                                  new sap.ui.core.ListItem("German",{text: "German", key: "de"})

                                  ]

                 });

Greetings

Ivan