Limit results in jQuery UI Autocomplete
I am using jQuery UI Autocomplete.
$("#task").autocomplete({
max:10,
minLength:3,
source: myarray
});
The max parameter doesn't work and I still get more than 10 results. Am I missing something?
Here is the proper documentation for the jQueryUI widget. There isn't a built-in parameter for limiting max results, but you can accomplish it easily:
$("#auto").autocomplete({
source: function(request, response) {
var results = $.ui.autocomplete.filter(myarray, request.term);
response(results.slice(0, 10));
}
});
You can supply a function to the source
parameter and then call slice
on the filtered array.
Here's a working example: http://jsfiddle.net/andrewwhitaker/vqwBP/
You can set the minlength
option to some big value or you can do it by css like this,
.ui-autocomplete { height: 200px; overflow-y: scroll; overflow-x: hidden;}
Same like "Jayantha" said using css would be the easiest approach, but this might be better,
.ui-autocomplete { max-height: 200px; overflow-y: scroll; overflow-x: hidden;}
Note the only difference is "max-height". this will allow the widget to resize to smaller height but not more than 200px
Adding to Andrew's answer, you can even introduce a maxResults
property and use it this way:
$("#auto").autocomplete({
maxResults: 10,
source: function(request, response) {
var results = $.ui.autocomplete.filter(src, request.term);
response(results.slice(0, this.options.maxResults));
}
});
jsFiddle: http://jsfiddle.net/vqwBP/877/
This should help code readability and maintainability!
here is what I used
.ui-autocomplete { max-height: 200px; overflow-y: auto; overflow-x: hidden;}
The overflow auto so the scroll bar will not show when it's not supposed to.