1
0
mirror of synced 2025-02-16 20:13:16 +03:00

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(){...}
});
This commit is contained in:
evangun 2013-09-05 18:18:50 +02:00
parent fb8bf6d51d
commit cfec9c6434

View File

@ -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);