Skip to content

Commit 9a3666e

Browse files
committed
Merge pull request select2#1365 from corinnaerin/master
Feature: Allow placeholders with nonempty values
2 parents fcd7da7 + 50cbd39 commit 9a3666e

1 file changed

Lines changed: 38 additions & 12 deletions

File tree

select2.js

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ the specific language governing permissions and limitations under the Apache Lic
875875
opts.query = this.bind(function (query) {
876876
var data = { results: [], more: false },
877877
term = query.term,
878-
children, firstChild, process;
878+
children, placeholderOption, process;
879879

880880
process=function(element, collection) {
881881
var group;
@@ -896,9 +896,9 @@ the specific language governing permissions and limitations under the Apache Lic
896896

897897
// ignore the placeholder option if there is one
898898
if (this.getPlaceholder() !== undefined && children.length > 0) {
899-
firstChild = children[0];
900-
if ($(firstChild).text() === "") {
901-
children=children.not(firstChild);
899+
placeholderOption = this.getPlaceholderOption();
900+
if (placeholderOption) {
901+
children=children.not(placeholderOption);
902902
}
903903
}
904904

@@ -1629,10 +1629,27 @@ the specific language governing permissions and limitations under the Apache Lic
16291629

16301630
// abstract
16311631
getPlaceholder: function () {
1632+
var placeholderOption;
16321633
return this.opts.element.attr("placeholder") ||
16331634
this.opts.element.attr("data-placeholder") || // jquery 1.4 compat
16341635
this.opts.element.data("placeholder") ||
1635-
this.opts.placeholder;
1636+
this.opts.placeholder ||
1637+
((placeholderOption = this.getPlaceholderOption()) !== undefined && placeholderOption.text());
1638+
},
1639+
1640+
// abstract
1641+
getPlaceholderOption: function() {
1642+
if (this.select) {
1643+
var firstOption = this.select.children().first();
1644+
if (this.opts.placeholderOption !== undefined ) {
1645+
//Determine the placeholder option based on the specified placeholderOption setting
1646+
return (this.opts.placeholderOption === "first" && firstOption) ||
1647+
(typeof this.opts.placeholderOption === "function" && this.opts.placeholderOption(this.select));
1648+
} else if (firstOption.text() === "" && firstOption.val() === "") {
1649+
//No explicit placeholder option specified, use the first if it's blank
1650+
return firstOption;
1651+
}
1652+
}
16361653
},
16371654

16381655
/**
@@ -1941,7 +1958,8 @@ the specific language governing permissions and limitations under the Apache Lic
19411958
clear: function(triggerChange) {
19421959
var data=this.selection.data("select2-data");
19431960
if (data) { // guard against queued quick consecutive clicks
1944-
this.opts.element.val("");
1961+
var placeholderOption = this.getPlaceholderOption();
1962+
this.opts.element.val(placeholderOption ? placeholderOption.val() : "");
19451963
this.selection.find("span").empty();
19461964
this.selection.removeData("select2-data");
19471965
this.setPlaceholder();
@@ -1959,7 +1977,7 @@ the specific language governing permissions and limitations under the Apache Lic
19591977
// single
19601978
initSelection: function () {
19611979
var selected;
1962-
if (this.opts.element.val() === "" && this.opts.element.text() === "") {
1980+
if (this.isPlaceholderOptionSelected()) {
19631981
this.updateSelection([]);
19641982
this.close();
19651983
this.setPlaceholder();
@@ -1974,6 +1992,14 @@ the specific language governing permissions and limitations under the Apache Lic
19741992
});
19751993
}
19761994
},
1995+
1996+
isPlaceholderOptionSelected: function() {
1997+
var placeholderOption;
1998+
return ((placeholderOption = this.getPlaceholderOption()) !== undefined && placeholderOption.is(':selected')) ||
1999+
(this.opts.element.val() === "") ||
2000+
(this.opts.element.val() === undefined) ||
2001+
(this.opts.element.val() === null);
2002+
},
19772003

19782004
// single
19792005
prepareOpts: function () {
@@ -2013,9 +2039,9 @@ the specific language governing permissions and limitations under the Apache Lic
20132039

20142040
// single
20152041
getPlaceholder: function() {
2016-
// if a placeholder is specified on a single select without the first empty option ignore it
2042+
// if a placeholder is specified on a single select without a valid placeholder option ignore it
20172043
if (this.select) {
2018-
if (this.select.find("option").first().text() !== "") {
2044+
if (this.getPlaceholderOption() === undefined) {
20192045
return undefined;
20202046
}
20212047
}
@@ -2027,10 +2053,10 @@ the specific language governing permissions and limitations under the Apache Lic
20272053
setPlaceholder: function () {
20282054
var placeholder = this.getPlaceholder();
20292055

2030-
if (this.opts.element.val() === "" && placeholder !== undefined) {
2056+
if (this.isPlaceholderOptionSelected() && placeholder !== undefined) {
20312057

2032-
// check for a first blank option if attached to a select
2033-
if (this.select && this.select.find("option:first").text() !== "") return;
2058+
// check for a placeholder option if attached to a select
2059+
if (this.select && this.getPlaceholderOption() === undefined) return;
20342060

20352061
this.selection.find("span").html(this.opts.escapeMarkup(placeholder));
20362062

0 commit comments

Comments
 (0)