cancel
Showing results for 
Search instead for 
Did you mean: 

How to get selected key value in sap.m.Select?

Former Member
0 Kudos

Hi Gurus,

I bound the items with a entity set in my OData, but now here comes the question, i.e. how could I get the use selected key or value?

Here is the select/dropdown box snapshot.

And here is the code snippet snapshot.

The content in BPRoleModel>/d/results, as what is seen in the last snap pic, has been populated in the select/dropdown box successfully via this in controller.

The value BPGroup is a field name in an entity.

How could I get the key value and put it into the field BPGroup?

Accepted Solutions (0)

Answers (1)

Answers (1)

saurabh_vakil
Active Contributor
0 Kudos

Try the below:


this.getView().byId('BPGroupSelect').getSelectedKey(); // get selected item's key

this.getView().byId('BPGroupSelect').getLiveValue(); // get selected item's value

Former Member
0 Kudos

Hi Saurabh,

I think you missed the bottom part content, which is most important part to this question.

Regards,

Xuebin

Former Member
0 Kudos

Hi,

Add a change (or select) event to your dropdown list and perform oEvent.getSource().getSelectedKey().

I don't have access to the api at the moment so the events may be different. But its enough for pointing the direction.

Greetz

Former Member
0 Kudos

Hi Silvio,

Thank you for your reply.

I guess the way you introduced is similar to Saurabh's, but how to set the value to a OData entity?

Regards,

Xuebin

saurabh_vakil
Active Contributor
0 Kudos

You will have to call the create/update function on the entity which holds the BPGroup field in your service.

You can refer to the below links which show the method signatures of these 2 functions:

SAPUI5 SDK - Demo Kit - create

SAPUI5 SDK - Demo Kit - update

Regards,

Saurabh

Former Member
0 Kudos

Hi Saurabh,

Thank you for your reply.

As what you've said, if I want to implement some logic with the value of BPGroup, I do need to 'call the create/update function on the entity which holds the BPGroup field in your service.'.

But the thing is, how could I get the key value selected by a user put into the field BPGroup so that the back end can do something with it?

I kindly suggest you to go thru the whole question again.

Regards,

Xuebin

saurabh_vakil
Active Contributor
0 Kudos

I'm still not clear what your confusion is. There are 2 parts here: first, getting the selected value from the sap.m.Select and maybe storing it in a JavaScript object; then passing on the object along with the selected value to the OData service using the create/update method. Second - getting this value in the OData service's create/update method and updating this to the relevant back end tables. Are you not clear on the front end part (in your SAPUI5 application) or the back end part(OData service)?

Former Member
0 Kudos

Hi Saurabh,

Let me make myself much clearer.

If I wanner get the value/key selected by a user in a sap.m.Select( or a dropdown box ), and the values listed inside it is from back-end, i.e. OData service, I think 3 steps are mandatory:

1. Prepare values/keys and get them shown in sap.m.Select(or a dropdown box);

E.g.

Key     Value

1         Male

2         Female

( I've done successfully. )

2. Pass the value/key a user selected to a entity field, i.e. BPGroup here in the snapshot;

E.g.

The user select Male, and it is passed to BPGroup.

(That's what I am wondering.)

3. As what you've mentioned, do something with the value in BPGroup in back-end, i.e. getting this value in the OData service's create/update method and updating this to the relevant back end tables.

E.g.

Find the Male a pretty girlfriend if he isn't coming out of the closet.

( I can say for sure that I can do this because I've done this with other input fields. )

Hope this is clear.


Regards,

Xuebin

saurabh_vakil
Active Contributor
0 Kudos

In that case you will have to fetch the value selected by the user in the dropdown using the below code and save it in a JavaScript object:


var selectedVal = this.getView().byId('BPGroupSelect').getLiveValue(); // get selected item's value 

var aData = {"BPGrp": selectedVal, "name1":....., "name2":..... etc};

Then assuming you are updating the details in the back end from the OData service, create ODataModel and call its update method as shown below:


var oModel = new sap.ui.model.odata.ODataModel("<serviceURL>");

oModel.update("/Account('12345')", aData, null, function(oData) {alert("data updated");}, function(oError) {alert("error occurred while updating data");});

Former Member
0 Kudos

Hi Saurabh,

Your reply looks like a nice way to get this done.

Could you make it more detailed on 'save it in a JavaScript object'?

  1. var selectedVal = this.getView().byId('BPGroupSelect').getLiveValue(); // get selected item's value   
  2. var aData = {"BPGrp": selectedVal, "name1":....., "name2":..... etc}; 


var selectedVal = this.getView().byId('BPGroupSelect').getLiveValue(); // get selected item's value  
var aData = {"BPGrp": selectedVal, "name1":....., "name2":..... etc};

How could I put the selected key/value into a JSON model?

Regards,

Xuebin