Skip to content

Instantly share code, notes, and snippets.

@roperzh
Forked from dbarrionuevo/monster.js
Created September 23, 2014 16:27
Show Gist options
  • Save roperzh/683e14b61cfafefd48dd to your computer and use it in GitHub Desktop.
Save roperzh/683e14b61cfafefd48dd to your computer and use it in GitHub Desktop.
show_autocomplete: function (itemName, collection) {
var accentMap = {
"á": "a",
"é": "e",
"í": "i",
"ó": "o",
"ú": "u",
"ñ": "n"
};
var normalize = function( term ) {
var ret = "";
for ( var i = 0; i < term.length; i++ ) {
ret += accentMap[ term.charAt(i) ] || term.charAt(i);
}
return ret;
};
$('.' + itemName).autocomplete({
source: function (request, response) {
var contains1, containsMatcher1, startsWith1, startsWithMatcher1, term;
term = normalize($.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