Skip to content

Instantly share code, notes, and snippets.

@diegobarros0701
Created October 8, 2017 23:17
Show Gist options
  • Save diegobarros0701/c07c31e1a557ba8375c8c17d62cd5634 to your computer and use it in GitHub Desktop.
Save diegobarros0701/c07c31e1a557ba8375c8c17d62cd5634 to your computer and use it in GitHub Desktop.
Example of how to use typeahead remote
$(document).ready(() => {
initializeTypeahead('/materias/buscar', '/materias', '#subject_study_plan')
});
function initializeTypeahead(remote_url, prefetch_url, selector) {
var subjects = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: {
url: prefetch_url,
cacheKey: 'name'
},
remote: {
url: `${remote_url}/%QUERY`,
wildcard: '%QUERY'
}
});
$(selector).typeahead( null, {
display: 'name',
source: subjects,
}).on('typeahead:select', function(ev, suggestion) {
// subject_study_plan_subject_id
$(ev.target).closest('span').siblings('input').val(suggestion.id);
// console.log($(ev.target));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment