1
0
mirror of synced 2024-11-25 22:36:03 +03:00

Merge pull request #2848 from tedliang/master

Fix performance issue with setVal on MultiSelect
This commit is contained in:
Kevin Brown 2014-12-11 14:56:27 -05:00
commit 8651dac4af

View File

@ -2970,21 +2970,18 @@ the specific language governing permissions and limitations under the Apache Lic
// multi // multi
updateSelection: function (data) { updateSelection: function (data) {
var ids = [], filtered = [], self = this; var ids = {}, filtered = [], self = this;
// filter out duplicates // filter out duplicates
$(data).each(function () { $(data).each(function () {
if (indexOf(self.id(this), ids) < 0) { if (!(self.id(this) in ids)) {
ids.push(self.id(this)); ids[self.id(this)] = 0;
filtered.push(this); filtered.push(this);
} }
}); });
data = filtered;
this.selection.find(".select2-search-choice").remove(); this.selection.find(".select2-search-choice").remove();
$(data).each(function () { this.addSelectedChoice(filtered);
self.addSelectedChoice(this);
});
self.postprocessResults(); self.postprocessResults();
}, },
@ -3058,6 +3055,14 @@ the specific language governing permissions and limitations under the Apache Lic
}, },
addSelectedChoice: function (data) { addSelectedChoice: function (data) {
var val = this.getVal(), self = this;
$(data).each(function () {
val.push(self.createChoice(this));
});
this.setVal(val);
},
createChoice: function (data) {
var enableChoice = !data.locked, var enableChoice = !data.locked,
enabledItem = $( enabledItem = $(
"<li class='select2-search-choice'>" + "<li class='select2-search-choice'>" +
@ -3070,7 +3075,6 @@ the specific language governing permissions and limitations under the Apache Lic
"</li>"); "</li>");
var choice = enableChoice ? enabledItem : disabledItem, var choice = enableChoice ? enabledItem : disabledItem,
id = this.id(data), id = this.id(data),
val = this.getVal(),
formatted, formatted,
cssClass; cssClass;
@ -3104,8 +3108,7 @@ the specific language governing permissions and limitations under the Apache Lic
choice.data("select2-data", data); choice.data("select2-data", data);
choice.insertBefore(this.searchContainer); choice.insertBefore(this.searchContainer);
val.push(id); return id;
this.setVal(val);
}, },
// multi // multi
@ -3237,14 +3240,16 @@ the specific language governing permissions and limitations under the Apache Lic
// multi // multi
setVal: function (val) { setVal: function (val) {
var unique;
if (this.select) { if (this.select) {
this.select.val(val); this.select.val(val);
} else { } else {
unique = []; var unique = [], valMap = {};
// filter out duplicates // filter out duplicates
$(val).each(function () { $(val).each(function () {
if (indexOf(this, unique) < 0) unique.push(this); if (!(this in valMap)) {
unique.push(this);
valMap[this] = 0;
}
}); });
this.opts.element.val(unique.length === 0 ? "" : unique.join(this.opts.separator)); this.opts.element.val(unique.length === 0 ? "" : unique.join(this.opts.separator));
} }
@ -3260,11 +3265,9 @@ the specific language governing permissions and limitations under the Apache Lic
for (var j = 0; j < old.length; j++) { for (var j = 0; j < old.length; j++) {
if (equal(this.opts.id(current[i]), this.opts.id(old[j]))) { if (equal(this.opts.id(current[i]), this.opts.id(old[j]))) {
current.splice(i, 1); current.splice(i, 1);
if(i>0){
i--; i--;
}
old.splice(j, 1); old.splice(j, 1);
j--; break;
} }
} }
} }