JQuery "Autocomplete" plugin messes up my data order

I use the Autocomplete Jorn Zaefferer plugin on several different pages. In both cases, the order of the displayed lines is a bit messed up.

Example 1: an array of strings: they are mostly in alphabetical order, with the exception of the general knowledge that was clicked at the top:

General knowledge, art and design, business research, citizenship, design and technology, English, geography, history, ICT, mathematics, MFL French, MFL German, MFL Spanish, Music, Physical Education, PSHE, Religious education, Something else

Displayed lines:

General knowledge, geography, art and design, business research, citizenship, design and technology, English, history, ICT, mathematics, MFL French, MFL German, MFL Spanish, Music, Physical Education, PSHE, Religious education, Something else

Note that geography has been transferred to the second subject after General Knowledge. The rest is all right.

Example 2: array of strings: as stated above, but with cross-curriculum instead of general knowledge.

Cross-educational institution, art and design, business research, citizenship, design and technology, English, geography, history, ICT, mathematics, MFL French, MFL German, MFL Spanish, Music, Physical Education, PSHE, Religious education, Science, What something else

Displayed lines:

Cross-textbook, citizenship, art and design, business studies, design and technology, English, geography, history, ICT, mathematics, MFL French, MFL German, MFL Spanish, music, physical education, PSHE, religious education, science, What something else

Here citizenship is pushed to position number 2.

I experimented a bit, and it seems that there is a mistake saying "to put things that begin with the same letter as the first element after the first element, and leave the rest alone." Type of hoax. I tried a bit of debugging by running warnings inside the autocomplete plugin code, but everywhere I see that it uses the correct order. it is like when it came to the conclusion that it went wrong.

Any ideas? Max

EDIT - response to Clint

Thank you for pointing to the corresponding bit of code. To simplify the diagnosis, I changed the array of values ​​to ["carrot", "apple", "cherry"], which autofills, reorders ["carrot", "cherry", "apple"].

Here is the array that it generates for stMatchSets:

stMatchSets = ({'': [# 1 = {value: "carrot", data: "carrot"], result: "carrot"}, No. 3 = {value: "apple", data: ["apple"], result: "apple"}, # 2 = {value: "cherry", data: ["cherry"], result: "cherry"}], c: [# 1 #, # 2 #], a: [# 3 #]})

So, he collects the first letters together into a card, which makes sense as a strategy for matching the first step. However, I would like him to use this array of values, not a map, when it comes to populating the displayed list. I cannot understand what is happening with the cache inside the gut of the code (I am not very good at javascript).

SOLVED - I fixed this by cracking javascript in the plugin.
On line 549 (or 565), we return the csub variable, which is the object containing the corresponding data. Before it comes back, I will reorder it so that the order matches the original array of values ​​that we gave, i.e. We used to create the index in the first place, which I put in another variable:

csub = csub.sort (function (a, b) {return originalData.indexOf (a.value)> originalData.indexOf (b.value);})

hacky but it works. Personally, I believe that this behavior (possibly cleaner coding) should be the default behavior of the plugin: i.e. The order of the results should correspond to the original transmitted array of possible values. Thus, the user can sort their array alphabetically if they want (which is trivial) to get the results in alphabetical order, or they can save their own "custom" order.

0
source share
2 answers

What I did instead of your decision is to add

if (!q && data[q]){return data[q];} 

a little higher

 var csub = []; 

found in line ~ 535.

What does this mean, if I understood correctly, this is a selection of cached data, when the input is empty, it is indicated on the line ~ 472: stMatchSets[""] = [] . Assuming the cached data when the input is empty is the first data you provided for a start, then all this is good.

+2
source

I'm not sure about this autocomplete plugin, but are you sure that it is not just trying to give you the best match? My autocomplete plugin does some heuristics and does reordering of this nature.

Which brings me to my other answer: there are a million jQuery autocomplete plugins out there. If this does not satisfy you, I am sure that there are others.

edit: Actually, I'm completely sure that he does it. Look at line 474:

 // loop through the array and create a lookup structure for ( var i = 0, ol = options.data.length; i < ol; i++ ) { /* some code */ var firstChar = value.charAt(0).toLowerCase(); // if no lookup array for this character exists, look it up now if( !stMatchSets[firstChar] ) 

etc. So this is a feature.

+1
source

Source: https://habr.com/ru/post/917628/


All Articles