From cfec9c64343c04fdb7ab88cd85119017640ad33b Mon Sep 17 00:00:00 2001 From: evangun Date: Thu, 5 Sep 2013 18:18:50 +0200 Subject: [PATCH] Edit the default initSelection function to pass all tag properties The function will now pass all properties of the objects passed as tags to the constructor, not just the "id" and "text" properties. Because a tag is not always just an id and a text, just like when you provide the items with the "data" object instead. This makes sure these properties are always available at the runtime of formatSelection() or formatResult(), which is not currently the case when the field is repopulated on a triggered "change" event. My code looks like this : $el.select2({ ..., tags: [{ //id id: 1, //the string that will matched against the user's query text: 'martin smith greatmartin@provider.com' //the label of the item that will be shown if this tag is selected label: 'M. Smith', }], formatSelection: function(object, container){ return object.label || object.text; }, //right now I have to override the default function: initSelection: function(){...} }); --- select2.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/select2.js b/select2.js index eb0a21f6..55ab52f9 100644 --- a/select2.js +++ b/select2.js @@ -974,10 +974,11 @@ the specific language governing permissions and limitations under the Apache Lic opts.initSelection = function (element, callback) { var data = []; $(splitVal(element.val(), opts.separator)).each(function () { - var id = this, text = this, tags=opts.tags; + var obj = { id: this, text: this }, + tags = opts.tags; if ($.isFunction(tags)) tags=tags(); - $(tags).each(function() { if (equal(this.id, id)) { text = this.text; return false; } }); - data.push({id: id, text: text}); + $(tags).each(function() { if (equal(this.id, obj.id)) { obj = this; return false; } }); + data.push(obj); }); callback(data);