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

2

I'm using the jquery-iu Autocomplete and everything is working perfectly. But I would like to improve the script at one point. I'd like to limit the results. Because if I type A in the input, it appears INPUTS results and this is bad from the aesthetic point of view. At least in my view. I would like when I type A to appear a 5 results.

Here's everything I'm talking about: link

    
asked by anonymous 28.12.2014 / 05:10

2 answers

2

You only need to use a function in the source instead of using the Array. This filters the result and only the quantity you want for the callback function. In other words, change:

source: availableTags

To:

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

jsFiddle: link

    
28.12.2014 / 09:23
0

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

That

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

Turned this over

<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>
    
28.12.2014 / 05:26