1
0
mirror of synced 2024-11-25 14:26:03 +03:00

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/
This commit is contained in:
Boro Sitnikovski 2014-09-08 22:45:14 +02:00
parent d0d4bfb289
commit 8644ccb68b

View File

@ -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;
}
@ -1061,7 +1061,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();
@ -2589,7 +2589,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) {
@ -2604,7 +2603,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({
@ -3207,7 +3206,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);
}
},
@ -3423,6 +3422,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;
},