1
0
mirror of synced 2024-11-28 23:56:03 +03:00

option to hide the search field if there are just a few results. closes #4 and closes #6

This commit is contained in:
Igor Vaynberg 2012-03-28 15:21:43 -07:00
parent 7cd54f5358
commit 1ab192ac7b

View File

@ -255,7 +255,8 @@
formatResult: function (data) { return data.text; }, formatResult: function (data) { return data.text; },
formatSelection: function (data) { return data.text; }, formatSelection: function (data) { return data.text; },
formatNoMatches: function () { return "No matches found"; }, formatNoMatches: function () { return "No matches found"; },
formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; } formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; },
minimumResultsForSearch: 0
}, opts); }, opts);
element = opts.element; element = opts.element;
@ -382,8 +383,7 @@
this.container.addClass("select2-dropdown-open").addClass("select2-container-active"); this.container.addClass("select2-dropdown-open").addClass("select2-container-active");
this.updateResults(); this.updateResults(true);
this.ensureHighlightVisible();
this.alignDropdown(); this.alignDropdown();
this.dropdown.show(); this.dropdown.show();
this.focusSearch(); this.focusSearch();
@ -511,7 +511,10 @@
} }
}; };
AbstractSelect2.prototype.updateResults = function () { /**
* @param initial whether or not this is the call to this method right after the dropdown has been opened
*/
AbstractSelect2.prototype.updateResults = function (initial) {
var search = this.search, results = this.results, opts = this.opts; var search = this.search, results = this.results, opts = this.opts;
search.addClass("select2-active"); search.addClass("select2-active");
@ -551,7 +554,7 @@
var d = data.results[i]; var d = data.results[i];
$(this).data("select2-data", d); $(this).data("select2-data", d);
}); });
this.postprocessResults(); this.postprocessResults(data, initial);
})}); })});
}; };
@ -737,15 +740,28 @@
} }
}; };
SingleSelect2.prototype.postprocessResults = function () { SingleSelect2.prototype.postprocessResults = function (data, initial) {
var selected = 0, self = this; var selected = 0, self = this;
// find the selected element in the result list
this.results.find(".select2-result").each(function (i) { this.results.find(".select2-result").each(function (i) {
if ($(this).data("select2-data").id === self.opts.element.val()) { if ($(this).data("select2-data").id === self.opts.element.val()) {
selected = i; selected = i;
return false; return false;
} }
}); });
// and highlight it
this.highlight(selected); this.highlight(selected);
// hide the search box if this is the first we got the results and there are a few of them
if (initial===true) {
this.search.toggle(data.results.length>=this.opts.minimumResultsForSearch);
}
}; };
SingleSelect2.prototype.onSelect = function (data) { SingleSelect2.prototype.onSelect = function (data) {
@ -898,8 +914,6 @@
}); });
this.updateSelection(data); this.updateSelection(data);
// preload all results
this.updateResults();
} }
// set the placeholder if necessary // set the placeholder if necessary