agarzola / jQueryAutocompletePlugin

Jörn Zaefferer’s (now deprecated) jQuery plugin, with tweaks.
282 stars 151 forks source link

matchSubset fails with formatItem, formatResult #52

Closed vespadj closed 6 years ago

vespadj commented 6 years ago

Using formatItem and formatResult, "foo" show "foot" then typing "foot" show "No Records." matchSubset is true by default. The workaround is to set matchSubset: false, this may increases server load and decreases performance

agarzola commented 6 years ago

Please bear in mind this library is no longer officially supported. I recommend you use jQueryUI’s autocomplete features, or even a standalone autocomplete library, depending on your current project’s dependence on jQuery.

That said, I could take a look at this and try to identify what might be going wrong here. Can you please share what formatItem and formatResult functions look like? It would also be helpful to see what your data objects look like.

Thanks!

vespadj commented 6 years ago

Hi Alfonso, thanks for your reply. I don't like jQueryUI/autocomplete because it misses some features (or I can't find these), like zebra of rows and highlight of query term in results.

Probably, I compose bad the returned data object, that contains many fields ([0], [1], [2]), so I see first query, but after another typing, autocomplete search in cache in a wrong or empty field. How can I see the cache of autocomplete with browser console? I don't try to put the main field in row[0] yet.

formatItem: function(row, i, max, term) {
    return "<div class='box left'>" + row[0] + "</div>" + row[1].replace(new RegExp("(" + term + ")", "gi"), "<div class='box right small'>$1</div>") + "<div class='box right small'>" + row[2] + "</div>";
},
formatResult: function(row) {
    return row[0].replace(/(<.+?>)/gi, '');
}
agarzola commented 6 years ago

Do you see any errors in your console with those functions? I think that row[1].replace() is causing issues when row contains a single item.

vespadj commented 6 years ago

I find the solution. The query key must be in the first field row[0]. Using multiple returns like "foot|boo|ciao" for formatting items, the function parse (line 412 in v.1.2.3) creates this structure: split by "|" row[0]="foot" ; row[1]="boo" ; row[2]="ciao" ; .value = "foot" second query searches inside value.

Suppose a structure like Artist Title Artwork, this is wrong: Artwork|Artist|Title this is OK: "Artist Title"|other fields...

There is another option formatMatch, but I don't understand what it does and if it is for this goal.

agarzola commented 6 years ago

It has been very many years since I’ve touched this code, and I am not its original author. So I can’t quite remember what the use of formatMatch was. I suggest you open the docs directory in your browser.

In any case, it sounds like you’ve found a solution. If so, please feel free to close this issue.

Thanks!