Kaynağa Gözat

add drag and drop sort support, fixes #60

Igor Vaynberg 13 yıl önce
ebeveyn
işleme
f368464132
1 değiştirilmiş dosya ile 32 ekleme ve 3 silme
  1. 32 3
      select2.js

+ 32 - 3
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 <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),
             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") {