Skip to content

Instantly share code, notes, and snippets.

@ypxu
Last active December 14, 2015 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ypxu/4998881 to your computer and use it in GitHub Desktop.
Save ypxu/4998881 to your computer and use it in GitHub Desktop.
// use case sample
this.$(this.selectItemsTag).select2({
placeholder:"Enter an items",
multiple:true,
minimumInputLength:1,
maximumSelectionSize:200,
initSelection : function (element, callback) {
var data = [];
var val = element.val() || "";
_.each(val.split(","), function(item){
data.push({id: item, text:item, label: item});
});
callback(data);
},
});
// code from select2
// multi
setVal: function (val) {
var unique;
if (this.select) {
this.select.val(val);
} else {
unique = [];
// filter out duplicates
$(val).each(function () {
if (indexOf(this, unique) < 0) unique.push(this);
});
this.opts.element.val(unique.length === 0 ? "" : unique.join(this.opts.separator));
}
},
@ypxu
Copy link
Author

ypxu commented Feb 20, 2013

The performance issue on setVal function when you have large list of items. The de-duplicate process slow thing down.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment