Browse Source

new placeholderOption option

added ability to specify an option through a function or the shortcut
"first" which will be used as the placeholder option. If no placeholder
text is specified, then the text of this option will be used as the
placeholder text.
Corinna Schmidt 12 years ago
parent
commit
50cbd3927c
1 changed files with 38 additions and 12 deletions
  1. 38 12
      select2.js

+ 38 - 12
select2.js

@@ -875,7 +875,7 @@ the specific language governing permissions and limitations under the Apache Lic
                 opts.query = this.bind(function (query) {
                 opts.query = this.bind(function (query) {
                     var data = { results: [], more: false },
                     var data = { results: [], more: false },
                         term = query.term,
                         term = query.term,
-                        children, firstChild, process;
+                        children, placeholderOption, process;
 
 
                     process=function(element, collection) {
                     process=function(element, collection) {
                         var group;
                         var group;
@@ -896,9 +896,9 @@ the specific language governing permissions and limitations under the Apache Lic
 
 
                     // ignore the placeholder option if there is one
                     // ignore the placeholder option if there is one
                     if (this.getPlaceholder() !== undefined && children.length > 0) {
                     if (this.getPlaceholder() !== undefined && children.length > 0) {
-                        firstChild = children[0];
-                        if ($(firstChild).text() === "") {
-                            children=children.not(firstChild);
+                        placeholderOption = this.getPlaceholderOption();
+                        if (placeholderOption) {
+                            children=children.not(placeholderOption);
                         }
                         }
                     }
                     }
 
 
@@ -1629,10 +1629,27 @@ the specific language governing permissions and limitations under the Apache Lic
 
 
         // abstract
         // abstract
         getPlaceholder: function () {
         getPlaceholder: function () {
+            var placeholderOption;
             return this.opts.element.attr("placeholder") ||
             return this.opts.element.attr("placeholder") ||
                 this.opts.element.attr("data-placeholder") || // jquery 1.4 compat
                 this.opts.element.attr("data-placeholder") || // jquery 1.4 compat
                 this.opts.element.data("placeholder") ||
                 this.opts.element.data("placeholder") ||
-                this.opts.placeholder;
+                this.opts.placeholder ||
+                ((placeholderOption = this.getPlaceholderOption()) !== undefined && placeholderOption.text());
+        },
+        
+        // abstract
+        getPlaceholderOption: function() {
+            if (this.select) {
+                var firstOption = this.select.children().first();
+                if (this.opts.placeholderOption !== undefined ) {
+                    //Determine the placeholder option based on the specified placeholderOption setting
+                    return (this.opts.placeholderOption === "first" && firstOption) ||
+                           (typeof this.opts.placeholderOption === "function" && this.opts.placeholderOption(this.select));
+                } else if (firstOption.text() === "" && firstOption.val() === "") {
+                    //No explicit placeholder option specified, use the first if it's blank
+                    return firstOption;
+                }
+            }
         },
         },
 
 
         /**
         /**
@@ -1941,7 +1958,8 @@ the specific language governing permissions and limitations under the Apache Lic
         clear: function(triggerChange) {
         clear: function(triggerChange) {
             var data=this.selection.data("select2-data");
             var data=this.selection.data("select2-data");
             if (data) { // guard against queued quick consecutive clicks
             if (data) { // guard against queued quick consecutive clicks
-                this.opts.element.val("");
+                var placeholderOption = this.getPlaceholderOption();
+                this.opts.element.val(placeholderOption ? placeholderOption.val() : "");
                 this.selection.find("span").empty();
                 this.selection.find("span").empty();
                 this.selection.removeData("select2-data");
                 this.selection.removeData("select2-data");
                 this.setPlaceholder();
                 this.setPlaceholder();
@@ -1959,7 +1977,7 @@ the specific language governing permissions and limitations under the Apache Lic
         // single
         // single
         initSelection: function () {
         initSelection: function () {
             var selected;
             var selected;
-            if (this.opts.element.val() === "" && this.opts.element.text() === "") {
+            if (this.isPlaceholderOptionSelected()) {
                 this.updateSelection([]);
                 this.updateSelection([]);
                 this.close();
                 this.close();
                 this.setPlaceholder();
                 this.setPlaceholder();
@@ -1974,6 +1992,14 @@ the specific language governing permissions and limitations under the Apache Lic
                 });
                 });
             }
             }
         },
         },
+        
+        isPlaceholderOptionSelected: function() {
+            var placeholderOption;
+            return ((placeholderOption = this.getPlaceholderOption()) !== undefined && placeholderOption.is(':selected')) ||
+                   (this.opts.element.val() === "") ||
+                   (this.opts.element.val() === undefined) ||
+                   (this.opts.element.val() === null);
+        },
 
 
         // single
         // single
         prepareOpts: function () {
         prepareOpts: function () {
@@ -2013,9 +2039,9 @@ the specific language governing permissions and limitations under the Apache Lic
 
 
         // single
         // single
         getPlaceholder: function() {
         getPlaceholder: function() {
-            // if a placeholder is specified on a single select without the first empty option ignore it
+            // if a placeholder is specified on a single select without a valid placeholder option ignore it
             if (this.select) {
             if (this.select) {
-                if (this.select.find("option").first().text() !== "") {
+                if (this.getPlaceholderOption() === undefined) {
                     return undefined;
                     return undefined;
                 }
                 }
             }
             }
@@ -2027,10 +2053,10 @@ the specific language governing permissions and limitations under the Apache Lic
         setPlaceholder: function () {
         setPlaceholder: function () {
             var placeholder = this.getPlaceholder();
             var placeholder = this.getPlaceholder();
 
 
-            if (this.opts.element.val() === "" && placeholder !== undefined) {
+            if (this.isPlaceholderOptionSelected() && placeholder !== undefined) {
 
 
-                // check for a first blank option if attached to a select
-                if (this.select && this.select.find("option:first").text() !== "") return;
+                // check for a placeholder option if attached to a select
+                if (this.select && this.getPlaceholderOption() === undefined) return;
 
 
                 this.selection.find("span").html(this.opts.escapeMarkup(placeholder));
                 this.selection.find("span").html(this.opts.escapeMarkup(placeholder));