diff --git a/select2.js b/select2.js index f220a9a8..949a4384 100644 --- a/select2.js +++ b/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 * @param string * @param separator */ - function splitVal(string, separator) { + function splitVal(string, separator, transform) { var val, i, l; if (string === null || string.length < 1) return []; 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; } @@ -1063,7 +1063,7 @@ the specific language governing permissions and limitations under the Apache Lic if (opts.initSelection === undefined) { opts.initSelection = function (element, callback) { var data = []; - $(splitVal(element.val(), opts.separator)).each(function () { + $(splitVal(element.val(), opts.separator, opts.transformVal)).each(function () { var obj = { id: this, text: this }, tags = opts.tags; if ($.isFunction(tags)) tags=tags(); @@ -2595,7 +2595,6 @@ the specific language governing permissions and limitations under the Apache Lic self=this; // TODO validate placeholder is a string if specified - if (opts.element.get(0).tagName.toLowerCase() === "select") { // install the selection initializer 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) { // install default initSelection when applied to hidden input and data is local 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 var matches = []; opts.query({ @@ -3213,7 +3212,7 @@ the specific language governing permissions and limitations under the Apache Lic return val === null ? [] : val; } else { 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); return markup.join(""); }, + transformVal: function(val) { + return $.trim(val); + }, formatSelection: function (data, container, escapeMarkup) { return data ? escapeMarkup(this.text(data)) : undefined; },