Skip to content

Commit 1906d93

Browse files
author
Tomas Kirda
committed
Enable support for minChars: 0. Fixes #95.
1 parent 2f260ce commit 1906d93

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

scripts/demo.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ $(function () {
5656
// Initialize autocomplete with local lookup:
5757
$('#autocomplete').autocomplete({
5858
lookup: countriesArray,
59+
minChars: 0,
5960
onSelect: function (suggestion) {
6061
$('#selection').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
6162
}

src/jquery.autocomplete.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,18 @@
179179
that.el.on('keydown.autocomplete', function (e) { that.onKeyPress(e); });
180180
that.el.on('keyup.autocomplete', function (e) { that.onKeyUp(e); });
181181
that.el.on('blur.autocomplete', function () { that.onBlur(); });
182-
that.el.on('focus.autocomplete', function () { that.fixPosition(); });
182+
that.el.on('focus.autocomplete', function () { that.onFocus(); });
183183
that.el.on('change.autocomplete', function (e) { that.onKeyUp(e); });
184184
},
185185

186+
onFocus: function () {
187+
var that = this;
188+
that.fixPosition();
189+
if (that.options.minChars <= that.el.val().length) {
190+
that.onValueChange();
191+
}
192+
},
193+
186194
onBlur: function () {
187195
this.enableKillerFn();
188196
},
@@ -259,7 +267,7 @@
259267
that.intervalId = window.setInterval(function () {
260268
that.hide();
261269
that.stopKillSuggestions();
262-
}, 300);
270+
}, 50);
263271
},
264272

265273
stopKillSuggestions: function () {
@@ -430,15 +438,16 @@
430438
if ($.isFunction(options.serviceUrl)) {
431439
serviceUrl = options.serviceUrl.call(that.element, q);
432440
}
433-
if(this.currentRequest != null) {
434-
this.currentRequest.abort();
441+
if (that.currentRequest) {
442+
that.currentRequest.abort();
435443
}
436-
this.currentRequest = $.ajax({
444+
that.currentRequest = $.ajax({
437445
url: serviceUrl,
438446
data: options.ignoreParams ? null : options.params,
439447
type: options.type,
440448
dataType: options.dataType
441449
}).done(function (data) {
450+
that.currentRequest = null;
442451
that.processResponse(data, q);
443452
options.onSearchComplete.call(that.element, q);
444453
});

0 commit comments

Comments
 (0)