The first issue is that right out of the box it will not work with Bootstrap 3. Support is still being looked at but in the mean time there are some fixes. Here is a thread about the issue on their GitHub page with some helpful people posting solutions for the interim. Below is the code we are using for ours which I'm sure will change in the coming days.
//Twitter Typeahead CSS //Updated 22 August 2013 .twitter-typeahead { width: 100%; position: relative; } .twitter-typeahead .tt-query, .twitter-typeahead .tt-hint { margin-bottom: 0; width:100%; height: 34px; position: absolute; top:0; left:0; } .twitter-typeahead .tt-hint { color:#a1a1a1; z-index: 1; padding: 6px 12px; border:1px solid transparent; } .twitter-typeahead .tt-query { z-index: 2; border-radius: 4px!important; /* add these 2 statements if you have an appended input group */ border-top-right-radius: 0!important; border-bottom-right-radius: 0!important; /* add these 2 statements if you have an prepended input group */ /* border-top-left-radius: 0!important; border-bottom-left-radius: 0!important; */ } .tt-dropdown-menu { min-width: 160px; margin-top: 2px; padding: 5px 0; background-color: #fff; border: 1px solid #ccc; border: 1px solid rgba(0,0,0,.2); *border-right-width: 2px; *border-bottom-width: 2px; -webkit-border-radius: 6px; -moz-border-radius: 6px; border-radius: 6px; -webkit-box-shadow: 0 5px 10px rgba(0,0,0,.2); -moz-box-shadow: 0 5px 10px rgba(0,0,0,.2); box-shadow: 0 5px 10px rgba(0,0,0,.2); -webkit-background-clip: padding-box; -moz-background-clip: padding; background-clip: padding-box; } .tt-suggestion { display: block; padding: 3px 20px; } .tt-suggestion.tt-is-under-cursor { color: #fff; background-color: #0081c2; background-image: -moz-linear-gradient(top, #0088cc, #0077b3); background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#0088cc), to(#0077b3)); background-image: -webkit-linear-gradient(top, #0088cc, #0077b3); background-image: -o-linear-gradient(top, #0088cc, #0077b3); background-image: linear-gradient(to bottom, #0088cc, #0077b3); background-repeat: repeat-x; filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff0088cc', endColorstr='#ff0077b3', GradientType=0) } .tt-suggestion.tt-is-under-cursor a { color: #fff; } .tt-suggestion p { margin: 0; }
Once we got the formatting and could create our typeahead inputs we needed data for them. We were using AJAX calls to our server to get the list as the user typed. This worked but was a little slow. Typeahead now has a few different ways of populating data into the input field.
$("#customer_search").typeahead({ name: "customers", //Local storage name so that other parts of your site don't need to make a request if we already have it remote: "customerlist/%QUERY/", //Fall back in case prefetch doesn't have the necessary results prefetch: "customerlist/", //Limited to last 1000 updated contacts limit: 10, //This is the limit displayed not returned template: "<p><strong>{{number}}</strong> - {{name}}", //Typeahead inline template engine: Hogan, }).on("typeahead:selected", function($e, datum){ //What to do on select window.location = "overview/" + datum['number'] + "/"; });
for customer in customers: output.append({'value':'%s' % customer.name, 'tokens':customer.name.split() + ['%s' % customer.customer_number,], 'name':customer.name, 'number':customer.customer_number})
Hope this is helpful to others out there.
EDIT 16 September 2013
Some people were complaining about the dropdown not dynamically scaling. Git user karimlahlou has posted this to fix that problem.
Thank you @ashleydw, finally i got it working. for those need to adjust the list width, use this function:$('.typeahead').typeahead({ remote: 'path/to/url' }).on('typeahead:opened',function(){$('.tt-dropdown-menu').css('width',$(this).width() + 'px');});Hope this helps a few people.