Merge pull request #2665 from bor0/master
Items with spaces can be added infinite times on multiselects
This commit is contained in:
commit
ea266346be
16
select2.js
16
select2.js
@ -160,16 +160,16 @@ the specific language governing permissions and limitations under the Apache Lic
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Splits the string into an array of values, trimming each value. An empty array is returned for nulls or empty
|
* Splits the string into an array of values, transforming each value. An empty array is returned for nulls or empty
|
||||||
* strings
|
* strings
|
||||||
* @param string
|
* @param string
|
||||||
* @param separator
|
* @param separator
|
||||||
*/
|
*/
|
||||||
function splitVal(string, separator) {
|
function splitVal(string, separator, transform) {
|
||||||
var val, i, l;
|
var val, i, l;
|
||||||
if (string === null || string.length < 1) return [];
|
if (string === null || string.length < 1) return [];
|
||||||
val = string.split(separator);
|
val = string.split(separator);
|
||||||
for (i = 0, l = val.length; i < l; i = i + 1) val[i] = $.trim(val[i]);
|
for (i = 0, l = val.length; i < l; i = i + 1) val[i] = transform(val[i]);
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1063,7 +1063,7 @@ the specific language governing permissions and limitations under the Apache Lic
|
|||||||
if (opts.initSelection === undefined) {
|
if (opts.initSelection === undefined) {
|
||||||
opts.initSelection = function (element, callback) {
|
opts.initSelection = function (element, callback) {
|
||||||
var data = [];
|
var data = [];
|
||||||
$(splitVal(element.val(), opts.separator)).each(function () {
|
$(splitVal(element.val(), opts.separator, opts.transformVal)).each(function () {
|
||||||
var obj = { id: this, text: this },
|
var obj = { id: this, text: this },
|
||||||
tags = opts.tags;
|
tags = opts.tags;
|
||||||
if ($.isFunction(tags)) tags=tags();
|
if ($.isFunction(tags)) tags=tags();
|
||||||
@ -2595,7 +2595,6 @@ the specific language governing permissions and limitations under the Apache Lic
|
|||||||
self=this;
|
self=this;
|
||||||
|
|
||||||
// TODO validate placeholder is a string if specified
|
// TODO validate placeholder is a string if specified
|
||||||
|
|
||||||
if (opts.element.get(0).tagName.toLowerCase() === "select") {
|
if (opts.element.get(0).tagName.toLowerCase() === "select") {
|
||||||
// install the selection initializer
|
// install the selection initializer
|
||||||
opts.initSelection = function (element, callback) {
|
opts.initSelection = function (element, callback) {
|
||||||
@ -2610,7 +2609,7 @@ the specific language governing permissions and limitations under the Apache Lic
|
|||||||
} else if ("data" in opts) {
|
} else if ("data" in opts) {
|
||||||
// install default initSelection when applied to hidden input and data is local
|
// install default initSelection when applied to hidden input and data is local
|
||||||
opts.initSelection = opts.initSelection || function (element, callback) {
|
opts.initSelection = opts.initSelection || function (element, callback) {
|
||||||
var ids = splitVal(element.val(), opts.separator);
|
var ids = splitVal(element.val(), opts.separator, opts.transformVal);
|
||||||
//search in data by array of ids, storing matching items in a list
|
//search in data by array of ids, storing matching items in a list
|
||||||
var matches = [];
|
var matches = [];
|
||||||
opts.query({
|
opts.query({
|
||||||
@ -3213,7 +3212,7 @@ the specific language governing permissions and limitations under the Apache Lic
|
|||||||
return val === null ? [] : val;
|
return val === null ? [] : val;
|
||||||
} else {
|
} else {
|
||||||
val = this.opts.element.val();
|
val = this.opts.element.val();
|
||||||
return splitVal(val, this.opts.separator);
|
return splitVal(val, this.opts.separator, this.opts.transformVal);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -3429,6 +3428,9 @@ the specific language governing permissions and limitations under the Apache Lic
|
|||||||
markMatch(this.text(result), query.term, markup, escapeMarkup);
|
markMatch(this.text(result), query.term, markup, escapeMarkup);
|
||||||
return markup.join("");
|
return markup.join("");
|
||||||
},
|
},
|
||||||
|
transformVal: function(val) {
|
||||||
|
return $.trim(val);
|
||||||
|
},
|
||||||
formatSelection: function (data, container, escapeMarkup) {
|
formatSelection: function (data, container, escapeMarkup) {
|
||||||
return data ? escapeMarkup(this.text(data)) : undefined;
|
return data ? escapeMarkup(this.text(data)) : undefined;
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user