mirror of
https://github.com/devbridge/jQuery-Autocomplete.git
synced 2024-11-26 06:46:32 +00:00
Add autoSelectFirst option.
This commit is contained in:
parent
b6482b8a89
commit
56437a2ec5
@ -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.
|
* `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.
|
* `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.
|
* `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
|
##Usage
|
||||||
|
|
||||||
Html:
|
Html:
|
||||||
|
@ -142,7 +142,8 @@ describe('Autocomplete', function () {
|
|||||||
var input = document.createElement('input'),
|
var input = document.createElement('input'),
|
||||||
autocomplete = new $.Autocomplete(input, {
|
autocomplete = new $.Autocomplete(input, {
|
||||||
lookup: [{ value: 'Jamaica', data: 'B' }],
|
lookup: [{ value: 'Jamaica', data: 'B' }],
|
||||||
tabDisabled: false
|
tabDisabled: false,
|
||||||
|
autoSelectFirst: true
|
||||||
});
|
});
|
||||||
input.value = 'Jam';
|
input.value = 'Jam';
|
||||||
autocomplete.onValueChange();
|
autocomplete.onValueChange();
|
||||||
@ -168,7 +169,8 @@ describe('Autocomplete', function () {
|
|||||||
var input = document.createElement('input'),
|
var input = document.createElement('input'),
|
||||||
autocomplete = new $.Autocomplete(input, {
|
autocomplete = new $.Autocomplete(input, {
|
||||||
lookup: [{ value: 'Jamaica', data: 'B' }],
|
lookup: [{ value: 'Jamaica', data: 'B' }],
|
||||||
tabDisabled: true
|
tabDisabled: true,
|
||||||
|
autoSelectFirst: true
|
||||||
});
|
});
|
||||||
input.value = 'Jam';
|
input.value = 'Jam';
|
||||||
autocomplete.onValueChange();
|
autocomplete.onValueChange();
|
||||||
@ -189,4 +191,29 @@ describe('Autocomplete', function () {
|
|||||||
expect(event.preventDefault).toHaveBeenCalled();
|
expect(event.preventDefault).toHaveBeenCalled();
|
||||||
expect(autocomplete.suggest).not.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);
|
||||||
|
});
|
||||||
});
|
});
|
@ -71,6 +71,7 @@
|
|||||||
var noop = function () { },
|
var noop = function () { },
|
||||||
that = this,
|
that = this,
|
||||||
defaults = {
|
defaults = {
|
||||||
|
autoSelectFirst: false,
|
||||||
serviceUrl: null,
|
serviceUrl: null,
|
||||||
lookup: null,
|
lookup: null,
|
||||||
onSelect: null,
|
onSelect: null,
|
||||||
@ -92,7 +93,7 @@
|
|||||||
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
|
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
|
||||||
},
|
},
|
||||||
paramName: 'query',
|
paramName: 'query',
|
||||||
transformResult: function(response) {
|
transformResult: function (response) {
|
||||||
return response.suggestions;
|
return response.suggestions;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -439,8 +440,10 @@
|
|||||||
that.visible = true;
|
that.visible = true;
|
||||||
|
|
||||||
// Select first value by default:
|
// Select first value by default:
|
||||||
that.selectedIndex = 0;
|
if (that.options.autoSelectFirst) {
|
||||||
container.children().first().addClass(classSelected);
|
that.selectedIndex = 0;
|
||||||
|
container.children().first().addClass(classSelected);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
verifySuggestionsFormat: function (suggestions) {
|
verifySuggestionsFormat: function (suggestions) {
|
||||||
|
Loading…
Reference in New Issue
Block a user