From c2fa04529bba174819765b8a022f44fbfd873807 Mon Sep 17 00:00:00 2001 From: Maurus Cuelenaere Date: Wed, 8 Aug 2012 18:16:48 +0200 Subject: [PATCH] Fix minimumResultsForSearch by recursively counting items Signed-off-by: Igor Vaynberg --- select2.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/select2.js b/select2.js index f633a315..bc5eadc1 100755 --- a/select2.js +++ b/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