JSFiddle - React, Tailwind, and code Playground
HTML
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css">
<script src="http://twitter.github.com/bootstrap/assets/js/bootstrap-typeahead.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="http://backbonejs.org/backbone-min.js"></script>
<div>
Type primo: <input type="text" class="full_name" style="margin: 0 auto;" data-provide="typeahead" data-items="4">
<hr>
id: <input type="text" class="id" style="margin: 0 auto;" >
</div>
JavaScript
var getSource = function () {
var users = new Backbone.Collection([
{
first_name: 'primo',
last_name: 'ultimo',
id: 1
},
{
first_name: 'altro_primo',
last_name: 'altro_ultimo',
id: 2
}
]).models;
return _.map(users, function (user) {
return {
id: user.get('id'),
full_name: user.get('first_name') + ' ' + user.get('last_name'),
// these functions allows Bootstrap typehead to use this item in places where it was expecting a string
toString: function () {
return JSON.stringify(this);
},
toLowerCase: function () {
return this.full_name.toLowerCase();
},
indexOf: function (string) {
return String.prototype.indexOf.apply(this.full_name, arguments);
},
replace: function (string) {
return String.prototype.replace.apply(this.full_name, arguments);
}
};
});
};
$('.full_name').typeahead({
minLength: 3,
source: getSource(),
display: 'full_name',
updater: function (itemString) {
var item = JSON.parse(itemString);
$('.id').val(item.id);
return item.full_name;
}
});