Browse Source

Added sort routine for adding items to select

Items are placed in the selects in the order they are found in the data.

This fix sorts the options as they are added to the select element
Troy Morehouse 10 years ago
parent
commit
e0765ae0d5
1 changed files with 16 additions and 0 deletions
  1. 16 0
      src/extensions/filter-control/bootstrap-table-filter-control.js

+ 16 - 0
src/extensions/filter-control/bootstrap-table-filter-control.js

@@ -67,6 +67,22 @@
             selectControl.append($("<option></option>")
                 .attr("value", value)
                 .text($('<div />').html(text).text()));
+            // Sort it. Not overly efficient to do this here
+            var $opts = selectControl.find('option:gt(0)');
+            $opts.sort(function(a,b){
+                a = $(a).text().toLowerCase();
+                b = $(b).text().toLowerCase();
+                if (a === undefined || a === null) { a = ''; }
+                if (b === undefined || b === null) { b = ''; }
+                if ($.isNumeric(a) && $.isNumeric(b)) {
+                    // Convert numerical values from string to float.
+                    a = parseFloat(a);
+                    b = parseFloat(b);
+                }
+                return a > b ? 1 : a < b ? -1 : 0;
+            });
+			selectControl.find('option:gt(0)').remove();
+			selectControl.append($opts);
         }
     };