Skip to content

Instantly share code, notes, and snippets.

@Lycisca
Last active May 16, 2018 04:30
Show Gist options
  • Save Lycisca/5b8ba932aeccbd1c06d2f44f5d3adfd8 to your computer and use it in GitHub Desktop.
Save Lycisca/5b8ba932aeccbd1c06d2f44f5d3adfd8 to your computer and use it in GitHub Desktop.
Autocomplete to show all results order by text introduced in the input.
// Show matched results. First beggining results with match string and then the rest of them.
// Using Jquery autocomplete and Lodash library
// items is array of objects where label is the text to show in the input.
$('.autocomple').autocomplete({
source: function(request, response) {
var matcher = new RegExp( $.ui.autocomplete.escapeRegex( request.term ), "i" );
var results = _.filter(items, function(item) {
return matcher.test(item.label);
});
_.map(results, function(result){
return result.index = result.label.indexOf(request.term)
});
results = _.sortBy(results, [function(result) { return result.index; }])
response(results);
}
});
@deliaahm
Copy link

I have these code
image

I want to sort by alphabet and keep display containing the word entered even if the word is in the middle or at the end.
Could you help me? I'm stuck:(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment