Limit the amount of results that appear in the jquery-Iu autocomplete

Asked

Viewed 560 times

2

I’m using the jquery-Iu Autocomplete and everything is working perfectly. However I would like to improve the script at one point. I would like to limit the results. Because if I type To no input, appears NUMEROUS results and this is bad from the aesthetic point of view. At least to my view. I would like when I type To about five results.

Here’s all I’m talking about: http://jsfiddle.net/hr64a7r8/2/

2 answers

2


You only need to use a function in the source instead of using the Array. This way filters the result and only the amount you want for the callback function. That is to say change:

source: availableTags

To:

source: function (request, response) {
    var results = $.ui.autocomplete.filter(availableTags, request.term);
    response(results.slice(0, 5));
}

jsFiddle: http://jsfiddle.net/8cg9a966/

0

I found in English the perfect answer to my problem, was just change the part that displayed the results

That

<script>
    $(function() {
        var availableTags = [<?php echo $atcat ?>];
            $( "#blog" ).autocomplete({
              source: availableTags
        });
    });
</script>

turned into this

<script>    
$(function() {
var src = [<?php echo $atcat ?>];

 $("#categoria").autocomplete({ 
    source: function(request, response) {
        var results = $.ui.autocomplete.filter(src, request.term);

        response(results.slice(0, 5));
    }
});                    
});                
</script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.