Skip to content Skip to sidebar Skip to footer

Bloodhound / Typeahead : Using Remote With Simple Strings

I'm trying to use typeahead.js with bloodhound to dynamically fetch autocomplete sugestions for an input field while the user is typing. However, I'm a noob at it and I can't figur

Solution 1:

Use bloodhound's filter method like this

filter: function(data) {
  return $.map(data, function(item) {
    return {
      'value': item
    };
  });
}

Bloodhound object should looks like this

var dataSource = newBloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    prefetch: {
        url: "http://output.jsbin.com/cajebe/1.json",
        filter: function(data){
            return $.map(data, function(item){
                return {'value' : item};
            });
        }
    }
});

PS: The data from the server should be of JSON format

Here is a demo http://jsfiddle.net/dhirajbodicherla/pegp21r7/35/


update

Typeahead is missing correct parameters. It should be

typeaheadjs: [ {options}, {datasets} ]

$('#txtAutoSearchBox').typeahead(null, { // <--- missing this nullitems: 4,
    displayKey: 'value',
    source: suggestions.ttAdapter()
});

Post a Comment for "Bloodhound / Typeahead : Using Remote With Simple Strings"