JQuery autocomplete – limit search results

By default, JQuery autocomplete would pull and display all matching results. In order to restrict the number of results so that the menu doesn’t go crazy long, I found a solution to resolve this issue:

Default autocomplete source method:

$("#auto").autocomplete({
    source: function( request, response ) {
        // delegate back to autocomplete, but extract the last term
        response( $.ui.autocomplete.filter(email_contacts, extractLast( request.term ) ) );
    }
});

Slice the results to return top 10.

$("#auto").autocomplete({
    source: function(request, response) {
        var results = $.ui.autocomplete.filter(myarray, request.term);
        response(results.slice(0, 10));
    }
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s