Skip to content

Commit cc0e124

Browse files
author
Tomas Kirda
committed
Implement onHint callback to enable hinted suggestions.
1 parent ee58414 commit cc0e124

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

demo.htm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ <h1>Ajax Autocomplete Demo</h1>
1010

1111
<h2>Ajax Lookup</h2>
1212
<p>Type country name in english:</p>
13-
<div>
14-
<input type="text" name="country" id="autocomplete-ajax"/>
13+
<div style="position: relative; height: 80px;">
14+
<input type="text" name="country" id="autocomplete-ajax" style="position: absolute; z-index: 2; background: transparent;"/>
15+
<input type="text" name="country" id="autocomplete-ajax-x" disabled="disabled" style="color: #CCC; position: absolute; background: transparent; z-index: 1;"/>
1516
</div>
1617
<div id="selction-ajax"></div>
1718

scripts/demo.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ $(function () {
1616
// Setup jQuery ajax mock:
1717
$.mockjax({
1818
url: '*',
19-
responseTime: 200,
19+
responseTime: 2000,
2020
response: function (settings) {
2121
var query = settings.data.query,
2222
queryLowerCase = query.toLowerCase(),
23-
suggestions = $.grep(countries, function(country) {
24-
return country.toLowerCase().indexOf(queryLowerCase) !== -1;
23+
re = new RegExp('\\b' + queryLowerCase, 'gi'),
24+
suggestions = $.grep(countriesArray, function (country) {
25+
// return country.value.toLowerCase().indexOf(queryLowerCase) === 0;
26+
return re.test(country.value);
2527
}),
2628
response = {
2729
query: query,
@@ -34,9 +36,17 @@ $(function () {
3436

3537
// Initialize ajax autocomplete:
3638
$('#autocomplete-ajax').autocomplete({
37-
serviceUrl: '/autosuggest/service/url',
39+
// serviceUrl: '/autosuggest/service/url',
40+
lookup: countriesArray,
41+
lookupFilter: function(suggestion, originalQuery, queryLowerCase) {
42+
var re = new RegExp('\\b' + queryLowerCase, 'gi');
43+
return re.test(suggestion.value);
44+
},
3845
onSelect: function(suggestion) {
3946
$('#selction-ajax').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
47+
},
48+
onHint: function (hint) {
49+
$('#autocomplete-ajax-x').val(hint);
4050
}
4151
});
4252

src/jquery.autocomplete.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@
318318
return;
319319
}
320320

321+
that.findBestHint();
322+
321323
clearInterval(that.onChangeInterval);
322324

323325
if (that.currentValue !== that.el.val()) {
@@ -421,6 +423,7 @@
421423
that.visible = false;
422424
that.selectedIndex = -1;
423425
$(that.suggestionsContainer).hide();
426+
that.signalHint(null);
424427
},
425428

426429
suggest: function () {
@@ -460,6 +463,32 @@
460463
that.selectedIndex = 0;
461464
container.children().first().addClass(classSelected);
462465
}
466+
467+
that.findBestHint();
468+
},
469+
470+
findBestHint: function () {
471+
var that = this,
472+
value = that.el.val().toLowerCase(),
473+
bestMatch = null;
474+
475+
$.each(that.suggestions, function (i, suggestion) {
476+
var foundMatch = suggestion.value.toLowerCase().indexOf(value) === 0;
477+
if (foundMatch) {
478+
bestMatch = suggestion;
479+
}
480+
return !foundMatch;
481+
});
482+
483+
that.signalHint(bestMatch);
484+
},
485+
486+
signalHint: function (suggestion) {
487+
var hintValue = '';
488+
if (suggestion) {
489+
hintValue = this.currentValue + suggestion.value.substr(this.currentValue.length);
490+
}
491+
(this.options.onHint || $.noop)(hintValue);
463492
},
464493

465494
verifySuggestionsFormat: function (suggestions) {
@@ -532,6 +561,7 @@
532561
$(that.suggestionsContainer).children().first().removeClass(that.classes.selected);
533562
that.selectedIndex = -1;
534563
that.el.val(that.currentValue);
564+
that.findBestHint();
535565
return;
536566
}
537567

@@ -571,6 +601,7 @@
571601
}
572602

573603
that.el.val(that.getValue(that.suggestions[index].value));
604+
that.signalHint(null);
574605
},
575606

576607
onSelect: function (index) {
@@ -580,6 +611,7 @@
580611

581612
that.currentValue = that.getValue(suggestion.value);
582613
that.el.val(that.currentValue);
614+
that.signalHint(null);
583615
that.suggestions = [];
584616

585617
if ($.isFunction(onSelectCallback)) {

0 commit comments

Comments
 (0)