Skip to content

Instantly share code, notes, and snippets.

@dbarrionuevo
Created September 23, 2014 16:17
Show Gist options
  • Save dbarrionuevo/699b78363d5263aa5f0f to your computer and use it in GitHub Desktop.
Save dbarrionuevo/699b78363d5263aa5f0f to your computer and use it in GitHub Desktop.
show_autocomplete: function (itemName, collection) {
$('.' + itemName).autocomplete({
source: function (request, response) {
var contains1, containsMatcher1, startsWith1, startsWithMatcher1, term;
term = $.ui.autocomplete.escapeRegex(request.term);
startsWithMatcher1 = new RegExp('^' + term, 'i');
startsWith1 = $.grep(collection, function (value) {
return startsWithMatcher1.test(value.label || value.value || value);
});
containsMatcher1 = new RegExp('\\b' + term, 'i');
contains1 = $.grep(collection, function (value) {
return $.inArray(value, startsWith1) < 0 && containsMatcher1.test(value.label || value.value || value);
});
return response(startsWith1.concat(contains1));
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment