Selaa lähdekoodia

Fix minimumResultsForSearch by recursively counting items

Signed-off-by: Igor Vaynberg <[email protected]>
Maurus Cuelenaere 12 vuotta sitten
vanhempi
commit
c2fa04529b
1 muutettua tiedostoa jossa 13 lisäystä ja 3 poistoa
  1. 13 3
      select2.js

+ 13 - 3
select2.js

@@ -404,6 +404,18 @@
         return $.isFunction(val) ? val() : val;
     }
 
+    function countResults(results) {
+        var count = 0;
+        $.each(results, function(i, item) {
+            if (item.children) {
+                count += countResults(item.children);
+            } else {
+                count++;
+            }
+        });
+        return count;
+    }
+
     /**
      * blurs any Select2 container that has focus when an element outside them was clicked or received focus
      *
@@ -1534,9 +1546,7 @@
             // hide the search box if this is the first we got the results and there are a few of them
 
             if (initial === true) {
-                // TODO below we use data.results.length, but what we really need is something recursive to calc the length
-                // TODO in case there are optgroups
-                showSearchInput = this.showSearchInput = data.results.length >= this.opts.minimumResultsForSearch;
+                showSearchInput = this.showSearchInput = countResults(data.results) >= this.opts.minimumResultsForSearch;
                 this.dropdown.find(".select2-search")[showSearchInput ? "removeClass" : "addClass"]("select2-search-hidden");
 
                 //add "select2-with-searchbox" to the container if search box is shown