Shiladittya Chakraborty
Shiladittya Chakraborty

Reputation: 4418

Select default value in dropdown using SAPUI5

I am facing issue while setting the default value in dropdown box in SAPUI5.

Currently I am using below code :

fields : [ new sap.ui.commons.DropdownBox("Cities", {
    selectedKey: "City3",
    items : [ new sap.ui.core.ListItem("City1", {
      text : "DE (Germany)"
    }), new sap.ui.core.ListItem("City2", {
      text : "GB (United Kingdom)"
    }), new sap.ui.core.ListItem("City3", {
      text : "IN (India)"
    }), new sap.ui.core.ListItem("City4", {
      text : "US (United States)"
    }) ]
  })
  }) ]

But In List box always it shows DE (Germany) value but I want to default set other value.

How to do that?

Upvotes: 2

Views: 16360

Answers (2)

Captain JK
Captain JK

Reputation: 160

It will be helpful if you use key and name

 sap.ui.getCore().byId('Cities').setSelectedItemId('YOUR ITEM ID')

//if you are using key value pair then its as easy as

   sap.ui.getCore().byId('Cities').setSelected('YOUR ITEM ID').setSelectedKey('YOUR KEY VALUE(1/2/3..)')

Upvotes: 1

herrlock
herrlock

Reputation: 1454

All sap.ui.core.Items have a property with the name key1, that is the one you can reference in the DropdownBox.

new sap.ui.core.ListItem("City1", {
    key : "City1",
    text : "DE (Germany)"
})

Otherwise you can try using selectedItemId instead of selectedKey2

Upvotes: 3

Related Questions