-
-
Save roperzh/683e14b61cfafefd48dd to your computer and use it in GitHub Desktop.
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_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