- Add optional parameter to the select constructor to get the options array sorted
Change-Id: Icb7d529ccbd4d343310f3c8397f59bc083f2cb52
Signed-off-by: Alessandro Boch <aboch@cisco.com>
one.lib.form = {
// create select-option form element
select : {
one.lib.form = {
// create select-option form element
select : {
- create : function(options, multiple) {
+ create : function(options, multiple, sort) {
// assert - auto assign
if (options == undefined)
options = {};
// assert - auto assign
if (options == undefined)
options = {};
$select.attr("multiple", "multiple");
}
var optionArray = one.lib.form.select.options(options);
$select.attr("multiple", "multiple");
}
var optionArray = one.lib.form.select.options(options);
+
+ // If specified, sort the option elements based on their text field
+ if (sort == true && optionArray.length > 1) {
+ var shifted = true;
+ var limit = optionArray.length;
+ while (shifted) {
+ shifted = false;
+ for ( var i = 1; i < limit; i++) {
+ if (optionArray[i - 1].text() > optionArray[i].text()) {
+ var swap = optionArray[i - 1];
+ optionArray[i - 1] = optionArray[i];
+ optionArray[i] = swap;
+ shifted = true;
+ }
+ }
+ }
+ }
+
$(optionArray).each(function(index, value) {
$select.append(value);
});
$(optionArray).each(function(index, value) {
$select.append(value);
});