From 2ee7fadfce44af81ed0036968b65e3e9ffad4158 Mon Sep 17 00:00:00 2001 From: Ted Liang Date: Tue, 9 Dec 2014 16:15:51 +1100 Subject: [PATCH 1/2] improves selectAll performance in MultiSelect2 --- select2.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/select2.js b/select2.js index 7c918e99..044bf1e8 100644 --- a/select2.js +++ b/select2.js @@ -2966,21 +2966,18 @@ the specific language governing permissions and limitations under the Apache Lic // multi updateSelection: function (data) { - var ids = [], filtered = [], self = this; + var ids = {}, filtered = [], self = this; // filter out duplicates $(data).each(function () { - if (indexOf(self.id(this), ids) < 0) { - ids.push(self.id(this)); + if (!(self.id(this) in ids)) { + ids[self.id(this)] = 0; filtered.push(this); } }); - data = filtered; this.selection.find(".select2-search-choice").remove(); - $(data).each(function () { - self.addSelectedChoice(this); - }); + this.addSelectedChoice(filtered); self.postprocessResults(); }, @@ -3054,6 +3051,14 @@ the specific language governing permissions and limitations under the Apache Lic }, 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, enabledItem = $( "
  • " + @@ -3066,7 +3071,6 @@ the specific language governing permissions and limitations under the Apache Lic "
  • "); var choice = enableChoice ? enabledItem : disabledItem, id = this.id(data), - val = this.getVal(), formatted, cssClass; @@ -3100,8 +3104,7 @@ the specific language governing permissions and limitations under the Apache Lic choice.data("select2-data", data); choice.insertBefore(this.searchContainer); - val.push(id); - this.setVal(val); + return id; }, // multi @@ -3233,14 +3236,16 @@ the specific language governing permissions and limitations under the Apache Lic // multi setVal: function (val) { - var unique; if (this.select) { this.select.val(val); } else { - unique = []; + var unique = [], valMap = {}; // filter out duplicates $(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)); } From 5c9e81053e98519564f5bd005d6c4da4d52d7fe5 Mon Sep 17 00:00:00 2001 From: Ted Liang Date: Wed, 10 Dec 2014 14:02:30 +1100 Subject: [PATCH 2/2] fix buildChangeDetails for MultiSelect --- select2.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/select2.js b/select2.js index 044bf1e8..e9460db1 100644 --- a/select2.js +++ b/select2.js @@ -3261,11 +3261,9 @@ the specific language governing permissions and limitations under the Apache Lic for (var j = 0; j < old.length; j++) { if (equal(this.opts.id(current[i]), this.opts.id(old[j]))) { current.splice(i, 1); - if(i>0){ - i--; - } + i--; old.splice(j, 1); - j--; + break; } } }