2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2024-11-22 12:55:12 +00:00

Add ability to limit results for local lookup. Closes #115.

This commit is contained in:
Tomas Kirda 2013-11-23 23:38:50 -06:00
parent 8278419a04
commit 6dad72898a
3 changed files with 36 additions and 3 deletions

View File

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

View File

@ -574,4 +574,27 @@ describe('Autocomplete', function () {
expect(instance.cachedResponse[cacheKey]).toBeTruthy();
});
});
it('Should limit results for local request', function () {
var input = $('<input />'),
instance,
limit = 3;
input.autocomplete({
lookup: [{ value: 'Jamaica' }, { value: 'Jamaica' }, { value: 'Jamaica' }, { value: 'Jamaica' }, { value: 'Jamaica' }]
});
input.val('Jam');
instance = input.autocomplete();
instance.onValueChange();
// Expect all items to be displayed:
expect(instance.suggestions.length).toBe(5);
// Set lookup result limit and verify:
instance.setOptions({ lookupLimit: limit });
instance.onValueChange();
expect(instance.suggestions.length).toBe(limit);
});
});

View File

@ -449,14 +449,23 @@
getSuggestionsLocal: function (query) {
var that = this,
options = that.options,
queryLowerCase = query.toLowerCase(),
filter = that.options.lookupFilter;
filter = options.lookupFilter,
limit = parseInt(options.lookupLimit, 10),
data;
return {
suggestions: $.grep(that.options.lookup, function (suggestion) {
data = {
suggestions: $.grep(options.lookup, function (suggestion) {
return filter(suggestion, query, queryLowerCase);
})
};
if (limit && data.suggestions.length > limit) {
data.suggestions = data.suggestions.slice(0, limit);
}
return data;
},
getSuggestions: function (q) {