Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter Typeahead remote

Tags:

typeahead

I am trying to use Twitter typeahead but I am facing a problem. I don't know how typeahead passes the string to the server. Is it through a GET parameter? If so, what is the name of the parameter?

like image 618
averageman Avatar asked Aug 01 '13 01:08

averageman


2 Answers

Easiest through a GET parameter, you can choose whatever parameter you want.

In JS:

$('#search').typeahead({
    name: 'Search',
    remote: '/search.php?query=%QUERY' // you can change anything but %QUERY, it's Typeahead default for the string to pass to backend
});

In PHP (or whatever backend you have):

$query = $_GET['query'];

Hope you get the basic idea.

like image 87
Hieu Nguyen Avatar answered Oct 21 '22 13:10

Hieu Nguyen


You might want to consider something like this, it is a very basic remote datasource example. The get parameter in this example is 'q'

// Get your data source
var dataSource = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
        url: 'path/to/your/url/json/datasource/?q=%QUERYSTRING',
        wildcard: '%QUERYSTRING'
    }
});

// initialize your element
var $typehead = $('#form input').typeahead(null, {
    source: dataSource
});

// fire a select event, what you want once a user has selected an item
$typehead.on('typeahead:select', function(obj, datum, name) {
    //your code here
});

////////////////////////////////////
# in python (django) we get a query string using the request object passed through a view like this
query = request.GET.get('q') or ""
//the caveat [or ""] is just to prevent null exceptions

///////////////////////////////////
# using php
$query = ($_GET['q']) ? $_GET['q'] : "";
like image 39
Peter Bob Ukonu Avatar answered Oct 21 '22 13:10

Peter Bob Ukonu