Created
October 8, 2017 23:17
-
-
Save diegobarros0701/c07c31e1a557ba8375c8c17d62cd5634 to your computer and use it in GitHub Desktop.
Example of how to use typeahead remote
This file contains 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
$(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