I'm trying to find a working example of the twitter bootstrap typeahead element that will make an ajax call to populate it's dropdown.
I have an existing working jquery autocomplete example which defines the ajax url to and how to process the reply
<script type="text/javascript">
//<![CDATA[
$(document).ready(function() {
var options = { minChars:3, max:20 };
$("#runnerquery").autocomplete('./index/runnerfilter/format/html',options).result(
function(event, data, formatted)
{
window.location = "./runner/index/id/"+data[1];
}
);
..
What do i need change to convert this to the typeahead example?
<script type="text/javascript">
//<![CDATA[
$(document).ready(function() {
var options = { source:'/index/runnerfilter/format/html', items:5 };
$("#runnerquery").typeahead(options).result(
function(event, data, formatted)
{
window.location = "./runner/index/id/"+data[1];
}
);
..
I'm going to wait for the 'Add remote sources support for typeahead' issue to be resolved.
when using ajax, try
$.getJSON()
instead of$.get()
if you have trouble with the correct display of the results.In my case i got only the first character of every result when i used
$.get()
, although i usedjson_encode()
server-side.To those looking for a coffeescript version of the accepted answer:
Try this if your service is not returning the right application/json content type header:
All of the responses refer to BootStrap 2 typeahead, which is no longer present in BootStrap 3.
For anyone else directed here looking for an AJAX example using the new post-Bootstrap Twitter typeahead.js, here's a working example. The syntax is a little different:
This example uses both synchronous (the call to processSync) and asynchronous suggestion, so you'd see some options appear immediately, then others are added. You can just use one or the other.
There are lots of bindable events and some very powerful options, including working with objects rather than strings, in which case you'd use your own custom display function to render your items as text.
i use
$().one()
for solve this; When page loaded, I send ajax to server and wait to done. Then pass result to function.$().one()
is important .Because force typehead.js to attach to input one time. sorry for bad writing.Starting from Bootstrap 2.1.0:
HTML:
Javascript:
Now you can make a unified code, placing "json-request" links in your HTML-code.