I've been working on some practice code based on the examples from Twitter typeahead.js 0.10.5. The typeahead and autosuggest functionality are working fine and the dropdown menu is being populated with data from a MySQL database. However, I would like to be able to grab another field from a JSON object and plug that value into another field, div or whatever element.
I can't find anywhere in the examples how this would be done. I'd like to display the field user_name and grab the field actor_id from the selected object and store it in a variable for later use. Below is the typeahead jquery code:
$(document).ready(function() {
var users = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('first_name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: 'users.php?query=%QUERY'
});
users.initialize();
$('#users').typeahead({
hint: true,
highLight: true,
minLength: 2
}, {
name: 'users',
displayKey: 'user_name',
source: users.ttAdapter()
});
});
This is a sample object:
[
{
actor_id: "38",
first_name: "tom"
},
{
actor_id: "42",
first_name: "tom"
}
]
Any advice or help would be appreciated. Hopefully I've been clear as to what I'm trying to accomplish :)
Aucun commentaire:
Enregistrer un commentaire