Skip to content

Commit 6dad728

Browse files
author
Tomas Kirda
committed
Add ability to limit results for local lookup. Closes #115.
1 parent 8278419 commit 6dad728

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
1616
* `lookup`: Lookup array for the suggestions. It may be array of strings or `suggestion` object literals.
1717
* `suggestion`: An object literal with the following format: `{ value: 'string', data: any }`.
1818
* `lookupFilter`: `function (suggestion, query, queryLowerCase) {}` filter function for local lookups. By default it does partial string match (case insensitive).
19+
* `lookupLimit`: Number of maximum results to display for local lookup. Default: no limit.
1920
* `onSelect`: `function (suggestion) {}` Callback function invoked when user selects suggestion
2021
from the list. `this` inside callback refers to input HtmlElement.
2122
* `minChars`: Minimum number of characters required to trigger autosuggest. Default: `1`.

spec/autocompleteBehavior.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,4 +574,27 @@ describe('Autocomplete', function () {
574574
expect(instance.cachedResponse[cacheKey]).toBeTruthy();
575575
});
576576
});
577+
578+
it('Should limit results for local request', function () {
579+
var input = $('<input />'),
580+
instance,
581+
limit = 3;
582+
583+
input.autocomplete({
584+
lookup: [{ value: 'Jamaica' }, { value: 'Jamaica' }, { value: 'Jamaica' }, { value: 'Jamaica' }, { value: 'Jamaica' }]
585+
});
586+
587+
input.val('Jam');
588+
instance = input.autocomplete();
589+
instance.onValueChange();
590+
591+
// Expect all items to be displayed:
592+
expect(instance.suggestions.length).toBe(5);
593+
594+
// Set lookup result limit and verify:
595+
instance.setOptions({ lookupLimit: limit });
596+
instance.onValueChange();
597+
598+
expect(instance.suggestions.length).toBe(limit);
599+
});
577600
});

src/jquery.autocomplete.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,14 +449,23 @@
449449

450450
getSuggestionsLocal: function (query) {
451451
var that = this,
452+
options = that.options,
452453
queryLowerCase = query.toLowerCase(),
453-
filter = that.options.lookupFilter;
454+
filter = options.lookupFilter,
455+
limit = parseInt(options.lookupLimit, 10),
456+
data;
454457

455-
return {
456-
suggestions: $.grep(that.options.lookup, function (suggestion) {
458+
data = {
459+
suggestions: $.grep(options.lookup, function (suggestion) {
457460
return filter(suggestion, query, queryLowerCase);
458461
})
459462
};
463+
464+
if (limit && data.suggestions.length > limit) {
465+
data.suggestions = data.suggestions.slice(0, limit);
466+
}
467+
468+
return data;
460469
},
461470

462471
getSuggestions: function (q) {

0 commit comments

Comments
 (0)