1
0
mirror of synced 2024-11-26 14:56:07 +03:00

Fix minimumResultsForSearch by recursively counting items

Signed-off-by: Igor Vaynberg <igor.vaynberg@gmail.com>
This commit is contained in:
Maurus Cuelenaere 2012-08-08 18:16:48 +02:00 committed by Igor Vaynberg
parent 4d3f8b6332
commit c2fa04529b

View File

@ -404,6 +404,18 @@
return $.isFunction(val) ? val() : val; 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 * 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 // hide the search box if this is the first we got the results and there are a few of them
if (initial === true) { if (initial === true) {
// TODO below we use data.results.length, but what we really need is something recursive to calc the length showSearchInput = this.showSearchInput = countResults(data.results) >= this.opts.minimumResultsForSearch;
// TODO in case there are optgroups
showSearchInput = this.showSearchInput = data.results.length >= this.opts.minimumResultsForSearch;
this.dropdown.find(".select2-search")[showSearchInput ? "removeClass" : "addClass"]("select2-search-hidden"); this.dropdown.find(".select2-search")[showSearchInput ? "removeClass" : "addClass"]("select2-search-hidden");
//add "select2-with-searchbox" to the container if search box is shown //add "select2-with-searchbox" to the container if search box is shown