Subhash Chaganti
Subhash Chaganti

Reputation: 117

sap.m.Select: How to Get Item Text

I have this code given below in view page:

<Select id="deptId" required="true">
    <core:Item key="1" text="Agri" />
    <core:Item key="2" text="Trade" />
    <core:Item key="3" text="Marketing" />
</Select>

I need to get value in Controller page. I added this code below:

department = this.getView().byId("deptId").getSelectedKey();

But all I am getting is key value like 1, 2, and 3 and not "Agri", "trade" and "Marketing".

I even tried adding .getSelectedKey().getValue() which is throwing an error.
Please help me to fix this.

Upvotes: 0

Views: 1564

Answers (2)

Rufi
Rufi

Reputation: 2645

Based on the documentation of sap.m.Select you should go with getSelectedItem() instead of getSelectedKey().

Upvotes: 0

Stephen S
Stephen S

Reputation: 3994

You simply have to get the selected Item control and get the text from that control.

var oItem = this.getView().byId("deptId").getSelectedItem();
var department = oItem.getText();

Upvotes: 1

Related Questions