[SOLVED] Search and show resault on focus in autocomplete

Hi, in standalone autocomplete, is there any way to show for example first 5 result on focus before type anything in search field?

Hi, you could use the “Dropdown With All Values”. You can find it on the docs.
If you want to limit it to just 5 you could do something like this:

  var autocompleteDropdownSimple = app.autocomplete.create({
    inputEl: '#autocomplete-dropdown',
    openIn: 'dropdown',
    source: function (query, render) {
        var results = [];
        // Find matched items
        for (var i = 0; i < fruits.length; i++) {
          if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) {
            if (i < 5) { //<-- add this conditional here
            results.push(fruits[i]);
            }
          }
        }
        // Render items by passing array with result items
        render(results);
      }
  });

Thanks but i need in standalone, I found the solution myself.

Add:
requestSourceOnOpen: true,
limit: 5,

Remove:

if (query.length === 0) {
  render(results);
  return;
}

Enjoy :slight_smile:

1 Like