Skip to content

Commit f368464

Browse files
committed
add drag and drop sort support, fixes select2#60
1 parent 1633d71 commit f368464

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

select2.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,15 +1376,15 @@
13761376
},
13771377

13781378
setVal: function (val) {
1379-
var unique = [];
1379+
var unique;
13801380
if (this.select) {
13811381
this.select.val(val);
13821382
} else {
1383+
unique = [];
13831384
// filter out duplicates
13841385
$(val).each(function () {
13851386
if (indexOf(this, unique) < 0) unique.push(this);
13861387
});
1387-
13881388
this.opts.element.val(unique.length === 0 ? "" : unique.join(","));
13891389
}
13901390
},
@@ -1416,6 +1416,35 @@
14161416
}
14171417

14181418
this.clearSearch();
1419+
},
1420+
onSortStart: function() {
1421+
if (this.select) {
1422+
throw new Error("Sorting of elements is not supported when attached to <select>. Attach to <input type='hidden'/> instead.");
1423+
}
1424+
1425+
// collapse search field into 0 width so its container can be collapsed as well
1426+
this.search.width(0);
1427+
// hide the container
1428+
this.searchContainer.hide();
1429+
},
1430+
onSortEnd:function() {
1431+
1432+
var val=[], self=this;
1433+
1434+
// show search and move it to the end of the list
1435+
this.searchContainer.show();
1436+
// make sure the search container is the last item in the list
1437+
this.searchContainer.appendTo(this.searchContainer.parent());
1438+
// since we collapsed the width in dragStarteed, we resize it here
1439+
this.resizeSearch();
1440+
1441+
// update selection
1442+
1443+
this.selection.find(".select2-search-choice").each(function() {
1444+
val.push(self.opts.id($(this).data("select2-data")));
1445+
});
1446+
this.setVal(val);
1447+
this.triggerChange();
14191448
}
14201449
});
14211450

@@ -1424,7 +1453,7 @@
14241453
var args = Array.prototype.slice.call(arguments, 0),
14251454
opts,
14261455
select2,
1427-
value, multiple, allowedMethods = ["val", "destroy", "open", "close", "focus", "isFocused", "container"];
1456+
value, multiple, allowedMethods = ["val", "destroy", "open", "close", "focus", "isFocused", "container", "onSortStart", "onSortEnd"];
14281457

14291458
this.each(function () {
14301459
if (args.length === 0 || typeof(args[0]) === "object") {

0 commit comments

Comments
 (0)