From f368464132ea55f979693e4587603c1a84f75ee8 Mon Sep 17 00:00:00 2001 From: Igor Vaynberg Date: Tue, 29 May 2012 17:04:52 -0700 Subject: [PATCH] add drag and drop sort support, fixes #60 --- select2.js | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/select2.js b/select2.js index 9f4cf380..b5ee47b0 100755 --- a/select2.js +++ b/select2.js @@ -1376,15 +1376,15 @@ }, setVal: function (val) { - var unique = []; + 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(",")); } }, @@ -1416,6 +1416,35 @@ } this.clearSearch(); + }, + onSortStart: function() { + if (this.select) { + throw new Error("Sorting of elements is not supported when attached to instead."); + } + + // collapse search field into 0 width so its container can be collapsed as well + this.search.width(0); + // hide the container + this.searchContainer.hide(); + }, + onSortEnd:function() { + + var val=[], self=this; + + // show search and move it to the end of the list + this.searchContainer.show(); + // make sure the search container is the last item in the list + this.searchContainer.appendTo(this.searchContainer.parent()); + // since we collapsed the width in dragStarteed, we resize it here + this.resizeSearch(); + + // update selection + + this.selection.find(".select2-search-choice").each(function() { + val.push(self.opts.id($(this).data("select2-data"))); + }); + this.setVal(val); + this.triggerChange(); } }); @@ -1424,7 +1453,7 @@ var args = Array.prototype.slice.call(arguments, 0), opts, select2, - value, multiple, allowedMethods = ["val", "destroy", "open", "close", "focus", "isFocused", "container"]; + value, multiple, allowedMethods = ["val", "destroy", "open", "close", "focus", "isFocused", "container", "onSortStart", "onSortEnd"]; this.each(function () { if (args.length === 0 || typeof(args[0]) === "object") {