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

Custom lookup function cannot produce string[] of suggestions #837

Closed nebaughman closed 2 years ago

nebaughman commented 2 years ago

jquery-autocomplete v1.4.11

The documentation implies that lookup suggestions can be provided as a string[] or an array of { value: string, data: any } objects. When using a serviceUrl, transformResult can produce a response like:

{ 
  suggestions: string[] 
}

However, when using a lookup function, the response (given to the done function) cannot be formatted like that. It must be formatted as:

{ 
  suggestions: [
    { value: string, data: any },
    ...
  ]
}

The data properties are optional, but you cannot just use a string[] for the suggestions.

For instance:

// this one fails
myLookupFn(query, done) { 
  done({ suggestions: [ "Animal", "Apple", "Avalanche" ] })
}

// this one works
myLookupFn(query, done) {
  done({
    suggestions: [
      { value: "Animal" },
      { value: "Apple" },
      { value: "Avalanche" },
    ]
  })
}

Please clarify the documentation or (better) allow the lookup function to produce the simpler { suggestions: string[] } format. Thanks!

tkirda commented 2 years ago

There is a sample in a readme that shows this. Because final format is always suggestion object.

You can always open a PR with an updated documentation.

nebaughman commented 2 years ago

Thank you for your quick reply. I think the issue, then, is that lookup must return results in the "final format" suggestion object, which does not support string[] of suggestions.

Response Format in the documentation mentions that:

Alternatively, if there is no data you can supply just a string array for suggestions

... which is misleading, because it's not true for the return type of lookup. Is this intentional? I wouldn't want to add special-cases to the documentation if this is just an unintentional bug. Thanks for clarifying.

tkirda commented 2 years ago

No it is not intentional, this issue never came up. Just how it was done initially.

nebaughman commented 2 years ago

Ok, thanks for clarifying. Looking at the code, the done callback given to lookup is the getSuggestions() function. Seems that, by this time, the results must be in the final format you mentioned (cannot accept a string[] at this point). I'm not very familiar with the inner workings (yet?), but maybe the workflow could be altered to handle string[] at that point. Also, instead of a done callback, this could be handled with a Promise. I'll look into it when I have some time. Thanks for helping me understand the intent.