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

Add autoSelectFirst option.

This commit is contained in:
Tomas Kirda 2013-01-20 14:26:30 -06:00
parent b6482b8a89
commit 56437a2ec5
3 changed files with 36 additions and 5 deletions

View File

@ -35,6 +35,7 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
* `tabDisabled`: Default `false`. Set to true to leave the cursor in the input field after the user tabs to select a suggestion.
* `paramName`: Default `query`. The name of the request parameter that contains the query.
* `transformResult`: `function(response) {}` called after the result of the query is ready. Converts the result into response.suggestions format.
* `autoSelectFirst`: if set to `true`, first item will be selected when showing suggestions. Default value `false`.
##Usage
Html:

View File

@ -142,7 +142,8 @@ describe('Autocomplete', function () {
var input = document.createElement('input'),
autocomplete = new $.Autocomplete(input, {
lookup: [{ value: 'Jamaica', data: 'B' }],
tabDisabled: false
tabDisabled: false,
autoSelectFirst: true
});
input.value = 'Jam';
autocomplete.onValueChange();
@ -168,7 +169,8 @@ describe('Autocomplete', function () {
var input = document.createElement('input'),
autocomplete = new $.Autocomplete(input, {
lookup: [{ value: 'Jamaica', data: 'B' }],
tabDisabled: true
tabDisabled: true,
autoSelectFirst: true
});
input.value = 'Jam';
autocomplete.onValueChange();
@ -189,4 +191,29 @@ describe('Autocomplete', function () {
expect(event.preventDefault).toHaveBeenCalled();
expect(autocomplete.suggest).not.toHaveBeenCalled();
});
it('Should not autoselect first item by default', function () {
var input = document.createElement('input'),
autocomplete = new $.Autocomplete(input, {
lookup: ['Jamaica', 'Jamaica', 'Jamaica']
});
input.value = 'Jam';
autocomplete.onValueChange();
expect(autocomplete.selectedIndex).toBe(-1);
});
it('Should autoselect first item autoSelectFirst set to true', function () {
var input = document.createElement('input'),
autocomplete = new $.Autocomplete(input, {
lookup: ['Jamaica', 'Jamaica', 'Jamaica'],
autoSelectFirst: true
});
input.value = 'Jam';
autocomplete.onValueChange();
expect(autocomplete.selectedIndex).toBe(0);
});
});

View File

@ -71,6 +71,7 @@
var noop = function () { },
that = this,
defaults = {
autoSelectFirst: false,
serviceUrl: null,
lookup: null,
onSelect: null,
@ -92,7 +93,7 @@
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
},
paramName: 'query',
transformResult: function(response) {
transformResult: function (response) {
return response.suggestions;
}
};
@ -439,8 +440,10 @@
that.visible = true;
// Select first value by default:
that.selectedIndex = 0;
container.children().first().addClass(classSelected);
if (that.options.autoSelectFirst) {
that.selectedIndex = 0;
container.children().first().addClass(classSelected);
}
},
verifySuggestionsFormat: function (suggestions) {