0

I have the same question as: Limit results autocomplete jquery ui with slice function , been looking for a solution (other than hiding using CSS) for the past 2 hours or so.

I want to add this slice function, demo here: https://stackoverflow.com/a/7617637/1607449

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

    response(results.slice(0, 10));
   }
});

And I would like it to work with the script in this example: http://jsfiddle.net/h5E6C/ (not my array/script but my setup is similar).

I'm looking for solutions to this question, so please don't respond with a suggestion to use PHP/SQL to filter the data before sending etc. Don't mean to be rude..

I can get either script to work independently of one another but not together :/

Community
  • 1
  • 1
GSimon
  • 347
  • 1
  • 3
  • 14
  • Why aren't you just using the minlength option? – drneel Nov 22 '13 at 05:58
  • because I'm using the search to look up room #s in a handful of buildings. If someone types in '2' or '5' it would be nice to have it start populating the results then. (in the off chance the room is only 1 number in length, (i.e room #8)) – GSimon Nov 22 '13 at 06:04
  • How about using the search event? – drneel Nov 22 '13 at 06:10
  • In what way? The search event doesn't affect the results once they are returned, it only prevents the search from happening if you assign it certain conditions to meet. – GSimon Nov 22 '13 at 06:16

1 Answers1

0

Figured it out, just needed to add

.slice(0, 10)

to the other response function.

Before:

function custom_source(request, response) {
    var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
    response($.grep(schools, function(value) {
        return matcher.test(value.value)
            || matcher.test(value.nickname);
    }));
}

After:

function custom_source(request, response) {
    var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
    response($.grep(schools, function(value) {
        return matcher.test(value.value)
            || matcher.test(value.nickname);
    }).slice(0, 10)
);
}

Thought I had tried that already but I must have placed the slice function in the wrong place before.

GSimon
  • 347
  • 1
  • 3
  • 14