Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
testinz
Posts: 183
Registered: ‎09-03-2012
My Device: Blackberry 10
Accepted Solution

DropDown - set selected option by value?

Is there an easy way to set the selected option in the DropDown by value?

 

Seems like I have to retrieve a list of Options and the loop through it in order to set the value.

 

Developer
testinz
Posts: 183
Registered: ‎09-03-2012
My Device: Blackberry 10

Re: DropDown - set selected option by value?

Solved. I used at(index) and loop.
Developer
Bluenoser
Posts: 313
Registered: ‎04-04-2012
My Device: BlackBerry Z30/Z10/Z10LE/Q10/PlayBook

Re: DropDown - set selected option by value?

Would you mind showing the code you did to accomplish this? Thanks in advance.

Developer
Bluenoser
Posts: 313
Registered: ‎04-04-2012
My Device: BlackBerry Z30/Z10/Z10LE/Q10/PlayBook

Re: DropDown - set selected option by value?

Here is what I was able to come up with. updateFrequency is the objectName of the DropDown.

 

    onCreationCompleted: {
        for (var i = 0; i < updateFrequency.count; i++)
        {
            var dropDownOption = updateFrequency.at(i);
            if (dropDownOption.value() == _wuWeatherApp.m_updateFrequency) {
                updateFrequency.setSelectedIndex(i);
                break;
            }
        }
    }

 

Developer
testinz
Posts: 183
Registered: ‎09-03-2012
My Device: Blackberry 10

Re: DropDown - set selected option by value?

That is right. You use the count and loop through it.

 

For-each in Java is way simpler :smileyhappy:

New Developer
tjaartb
Posts: 12
Registered: ‎11-01-2012
My Device: simulator

Re: DropDown - set selected option by value?

[ Edited ]

Here it is in a function for anyone who would like this in a library.

  // Set DropDown selected option by value. Returns false if the value is not in the DropDown
    function setDropDownOptionByValue(dropdown, value) {
        for (var i = 0; i < dropdown.options.length; i++) {
var o = dropdown.options[i]; if (o.value == value) { dropdown.setSelectedOption(o); return true; } } return false; }