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
New Developer
Posts: 6
Registered: ‎02-11-2009
My Device: Not Specified
Accepted Solution

Sorting DropDown

My app have a DropDown with several options disposed alphabetically by Option::text property. When switching to another language for example, the order must change but there are no way to re-sort the DropDown.

 

Any one knows the best approach to solve this?

 

Depicting what I need:

 

DropDown{

options[

Option{value: "en", text: qsTr("English")},

Option{value:"tl", qsTr("Filipino")} 

]

}

 

When translated into Portuguese the Option for English becomes "Ingles" and must be placed after "Filipino"

 

Any clue?

 

 

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Sorting DropDown

Hi,

 

I've written the following code, please try if it works for you:

 

import bb.cascades 1.0

Page
{
  function repopulateDropDown()
  {
    dropDown.removeAll();

    var languages = [
        qsTr("English"),
        qsTr("Filipino")
    ]

    languages.sort()
    
    for (var i = 0, arrLength = languages.length; i < arrLength; i++) {
      var option = optionDef.createObject();
      option.text = languages[i];
      dropDown.add(option); 
    }
  }
    
  onCreationCompleted:
  {
    repopulateDropDown();
  }

  content: ScrollView {
    DropDown {
      id: dropDown
      
      attachedObjects: [
        LocaleHandler  {
          id: localeHandler
          onLanguageChanged: {
            repopulateDropDown();
          }
        }
      ]
    }
  }
    
  attachedObjects: [
    ComponentDefinition {
      id: optionDef
      Option {}
    }
  ]
}

The strange thing I've noticed is that languageChange signal is emitted twice for every language change. Does anybody know why? This shouldn't affect this code though.

 


Andrey Fidrya, @zmeyc on twitter
Highlighted
Developer
Posts: 1,008
Registered: ‎12-12-2010
My Device: Passport (Red Limited Edition)
My Carrier: Mobile Vikings

Re: Sorting DropDown

I noticed the same behavior in another dropdown a while ago, but I haven't used dropdowns since. I think you can ignore it safely though.

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
New Developer
Posts: 6
Registered: ‎02-11-2009
My Device: Not Specified

Re: Sorting DropDown

[ Edited ]

Based on your solution I've made this:

 

onCreationCompleted: {
   speakLanguageOption.removeAll();
   var languages = mainTabedPane.getLanguages();//{qsTr("Label|Value|canHear:1|canSpeak:1")}
   languages.sort();
   for (var i = 0, arrLength = languages.length; i < arrLength; i++) {
      //label|value|canHear|canSpeak
      var splitedValue = languages[i].split("|");
      if(splitedValue[3] == "1"){
         var option = optionDef.createObject();
         option.text = splitedValue[0];
         option.value = splitedValue[1];         
         speakLanguageOption.add(option);
      }
   }
}