Skip to content

Commit 8644ccb

Browse files
committed
Items with spaces can be added infinite times on multiselects
Add optional parameter transformVal that allows the user to define transformation for splitVal Example demos: (Infinite spaces, default transformVal): http://jsfiddle.net/DV23t/9/ (No infinite spaces, transformVal = function (x) { return x; }): http://jsfiddle.net/DV23t/12/
1 parent d0d4bfb commit 8644ccb

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

select2.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,16 @@ the specific language governing permissions and limitations under the Apache Lic
160160
}
161161

162162
/**
163-
* Splits the string into an array of values, trimming each value. An empty array is returned for nulls or empty
163+
* Splits the string into an array of values, transforming each value. An empty array is returned for nulls or empty
164164
* strings
165165
* @param string
166166
* @param separator
167167
*/
168-
function splitVal(string, separator) {
168+
function splitVal(string, separator, transform) {
169169
var val, i, l;
170170
if (string === null || string.length < 1) return [];
171171
val = string.split(separator);
172-
for (i = 0, l = val.length; i < l; i = i + 1) val[i] = $.trim(val[i]);
172+
for (i = 0, l = val.length; i < l; i = i + 1) val[i] = transform(val[i]);
173173
return val;
174174
}
175175

@@ -1061,7 +1061,7 @@ the specific language governing permissions and limitations under the Apache Lic
10611061
if (opts.initSelection === undefined) {
10621062
opts.initSelection = function (element, callback) {
10631063
var data = [];
1064-
$(splitVal(element.val(), opts.separator)).each(function () {
1064+
$(splitVal(element.val(), opts.separator, opts.transformVal)).each(function () {
10651065
var obj = { id: this, text: this },
10661066
tags = opts.tags;
10671067
if ($.isFunction(tags)) tags=tags();
@@ -2589,7 +2589,6 @@ the specific language governing permissions and limitations under the Apache Lic
25892589
self=this;
25902590

25912591
// TODO validate placeholder is a string if specified
2592-
25932592
if (opts.element.get(0).tagName.toLowerCase() === "select") {
25942593
// install the selection initializer
25952594
opts.initSelection = function (element, callback) {
@@ -2604,7 +2603,7 @@ the specific language governing permissions and limitations under the Apache Lic
26042603
} else if ("data" in opts) {
26052604
// install default initSelection when applied to hidden input and data is local
26062605
opts.initSelection = opts.initSelection || function (element, callback) {
2607-
var ids = splitVal(element.val(), opts.separator);
2606+
var ids = splitVal(element.val(), opts.separator, opts.transformVal);
26082607
//search in data by array of ids, storing matching items in a list
26092608
var matches = [];
26102609
opts.query({
@@ -3207,7 +3206,7 @@ the specific language governing permissions and limitations under the Apache Lic
32073206
return val === null ? [] : val;
32083207
} else {
32093208
val = this.opts.element.val();
3210-
return splitVal(val, this.opts.separator);
3209+
return splitVal(val, this.opts.separator, this.opts.transformVal);
32113210
}
32123211
},
32133212

@@ -3423,6 +3422,9 @@ the specific language governing permissions and limitations under the Apache Lic
34233422
markMatch(this.text(result), query.term, markup, escapeMarkup);
34243423
return markup.join("");
34253424
},
3425+
transformVal: function(val) {
3426+
return $.trim(val);
3427+
},
34263428
formatSelection: function (data, container, escapeMarkup) {
34273429
return data ? escapeMarkup(this.text(data)) : undefined;
34283430
},

0 commit comments

Comments
 (0)