From: Alessandro Boch Date: Wed, 4 Sep 2013 22:58:26 +0000 (-0700) Subject: Add sorting to js select.create() X-Git-Tag: releasepom-0.1.0~127^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=6ff76389794bdf58986c97d8ea5c544712eeb43b Add sorting to js select.create() - Add optional parameter to the select constructor to get the options array sorted Change-Id: Icb7d529ccbd4d343310f3c8397f59bc083f2cb52 Signed-off-by: Alessandro Boch --- diff --git a/opendaylight/web/root/src/main/resources/js/lib.js b/opendaylight/web/root/src/main/resources/js/lib.js index 90fd49772a..811d35e12b 100644 --- a/opendaylight/web/root/src/main/resources/js/lib.js +++ b/opendaylight/web/root/src/main/resources/js/lib.js @@ -251,7 +251,7 @@ one.lib.modal = { 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 = {}; @@ -261,6 +261,24 @@ one.lib.form = { $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); });