1
0
mirror of synced 2025-03-10 22:56:12 +03:00

add drag and drop sort support, fixes #60

This commit is contained in:
Igor Vaynberg 2012-05-29 17:04:52 -07:00
parent 1633d71b4a
commit f368464132

View File

@ -1376,15 +1376,15 @@
}, },
setVal: function (val) { setVal: function (val) {
var unique = []; var unique;
if (this.select) { if (this.select) {
this.select.val(val); this.select.val(val);
} else { } else {
unique = [];
// filter out duplicates // filter out duplicates
$(val).each(function () { $(val).each(function () {
if (indexOf(this, unique) < 0) unique.push(this); if (indexOf(this, unique) < 0) unique.push(this);
}); });
this.opts.element.val(unique.length === 0 ? "" : unique.join(",")); this.opts.element.val(unique.length === 0 ? "" : unique.join(","));
} }
}, },
@ -1416,6 +1416,35 @@
} }
this.clearSearch(); this.clearSearch();
},
onSortStart: function() {
if (this.select) {
throw new Error("Sorting of elements is not supported when attached to <select>. Attach to <input type='hidden'/> 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), var args = Array.prototype.slice.call(arguments, 0),
opts, opts,
select2, 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 () { this.each(function () {
if (args.length === 0 || typeof(args[0]) === "object") { if (args.length === 0 || typeof(args[0]) === "object") {