0

I already made the autocomplete work. The only problem is, the results is not optimized.

For example:

data: [Banana, Apple, Orange] input: a output in autocomplete: [Banana, Apple, Orange]

the output is arranged by their order in the array. It shows all the data because they all have the letter 'a' in them, which is correct but I need to show the first result that starts with letter 'a' (input).

Example:

my expected output: [Apple, Banana, Orange]

This is what I have done so far, unfortunately It is not showing any result. I am using Laravel Framework by the way. Thank you for your help.

 source: function(request, response) {
          var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(request.term), "i");
          response($.grep("data", function(item){
              return matcher.test(item);
          }) );
      }
JaneTho
  • 321
  • 1
  • 2
  • 13
  • Something like this https://stackoverflow.com/questions/8302534/sorting-autocomplete-ui-results-based-on-match-location – astrocrack Jul 18 '17 at 14:57
  • @astrocrack as you can see my code and their code is almost the same. the only different here is I am fetching the data from laravel controller ("data"). and that doesnt seem to work. Im sure make an array inside the script that will work too. I think the problem here is how I put "data" in the response. :( – JaneTho Jul 18 '17 at 15:26
  • can you show your controller? I guess you should order the result by the desired field before returning it. – Oluwatobi Samuel Omisakin Jul 18 '17 at 21:55
  • i use this `select('r_address')->where('r_address','LIKE','%'.$term.'%')->orderBy('r_address','asc')->get(); ` when I input 'a' yes the first on list will be a result starts with letter 'a' but when I input 'b' if the like the first list before has letter 'b' on it. it will still pop up as the first because it is how they are arranged in the array – JaneTho Jul 19 '17 at 01:00
  • Shouldn't data be a json object? – astrocrack Jul 19 '17 at 08:16
  • yup, I am returning it as json object `return json_encode($data);` – JaneTho Jul 19 '17 at 08:20

0 Answers0