markzzz
markzzz

Reputation: 47945

is it possible to set a maximum number of result on autocomplete?

I'm using autocomplete of jQuery UI : excellent plugin!

The only thing I don't like is that, if I have a list of 40 items, I'd like to show only 10 items.

This is my actual code :

<script type="text/javascript">
    $(function() {
        var availableTags = [<%= m_strTags %>];
        $("#myTags").autocomplete({
            source: availableTags,
            max: 20
        });
    });
</script>

Is it possible put some parameters to do this thing?

Upvotes: 3

Views: 7111

Answers (5)

Richard Neil Ilagan
Richard Neil Ilagan

Reputation: 14747

You can try implementing your own filter function by using a callback function as a source. Something like:

$('input').autocomplete({
    source : function (request, response) {

        var max = 10;    // set this to something
        var j = 0;

        response($.map(availableTags, function(i) {
            if (j < max && i.toLowerCase().indexOf(request.term.toLowerCase()) != -1) {
                j++; return { label : i };
            } else { 
                return null;
            }   
        }));
    }
});

This is basically trying to check each of the elements in your string array, and performing a case-insensitive contains search. You may want to modify it to check only for startsWith or endsWith or whatever you have. Of course, you may also want to use regex instead to speed things up a bit.

I also setup a quick example here : http://jsfiddle.net/2exCC/ Try searching for something like C. This should return up to scala, and leave behind scheme.

Upvotes: 2

jk.
jk.

Reputation: 14435

If you do not want to modify the source list, you could try scrollable results:

http://jqueryui.com/demos/autocomplete/#maxheight

Upvotes: 2

Litek
Litek

Reputation: 4888

This is a nasty solution, and you really should look for a nicer one, but if you won't find anything you can hide the .ui-menu-item's when autocomplete opens popup with suggestions (open event).

Upvotes: 1

bjudson
bjudson

Reputation: 4083

From the migration guide:

max: Gone; if your server sends too many items, pass a function for the source option that calls $.ajax and truncates or filters the resulting list.

http://www.learningjquery.com/2010/06/autocomplete-migration-guide

Upvotes: 3

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

Yes it is. You need to use the setOptions() function.

Example:

$('input#suggest').setOptions({
 max: 15
});

http://docs.jquery.com/Plugins/Autocomplete/setOptions#options

Upvotes: 0

Related Questions