Last active
May 16, 2018 04:30
-
-
Save Lycisca/5b8ba932aeccbd1c06d2f44f5d3adfd8 to your computer and use it in GitHub Desktop.
Autocomplete to show all results order by text introduced in the input.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have these code

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:(