devbridge / jQuery-Autocomplete

Ajax Autocomplete for jQuery allows you to easily create autocomplete/autosuggest boxes for text input fields
https://www.devbridge.com/sourcery/components/jquery-autocomplete/
Other
3.56k stars 1.66k forks source link

[Feature request] add previousSuggestion to formatResult function #711

Closed abriginets closed 6 years ago

abriginets commented 6 years ago

Hello. Would you please add third parameter previousSuggestion to formatResult function? It would be very usefull to build child suggestions depending on previous suggestion value. Thanks.

tkirda commented 6 years ago

What do you mean by previousSuggestion?

abriginets commented 6 years ago

@tkirda I dare say that formatResult runs some loop to iterate over the suggestions array. If I'm right then there should be an ability to call for an element which was iterated before current.

tkirda commented 6 years ago

As a third parameter it passes suggestion index. So you can lookup previous suggestion in suggestions array. Suggestions are stored in suggestions array on autocomplete instance.

I don't see usefulness of it, but if you need it you can access previous suggestion by looking up by index.

abriginets commented 6 years ago

Excuse me for my silliness, but how could I access suggestions inside of formatResult? I could not access anythis by using $(this), this or suggestions. Much thanks in advance.

tkirda commented 6 years ago

It is little bit ugly but should work:

let instance;

$('#input').autocomplete({
  formatResult: (suggestion, value, i) => {
     console.log('previous', instance.suggestions[i-1]);
     return suggestion.value;
  }
});

// Assign to variable which can be accessed inside formatResult
instance = $('#input').autocomplete();