Skip to content

Instantly share code, notes, and snippets.

@jharding
Last active May 15, 2024 16:16
Show Gist options
  • Save jharding/9458749 to your computer and use it in GitHub Desktop.
Save jharding/9458749 to your computer and use it in GitHub Desktop.
<div id="bloodhound">
<input class="typeahead" type="text" placeholder="States of USA">
</div>
// constructs the suggestion engine
var states = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
// `states` is an array of state names defined in "The Basics"
local: states
});
$('#bloodhound .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'states',
source: states
});
@bhawi
Copy link

bhawi commented Apr 5, 2015

@jharding: I am new to typeahead and tried a similar demo to the one you explained, but I am not getting the suggestions in the textbox, Please help me out.
PFB the index.html file containing the code.

$(document).ready(function(){
alert('Auto Complete Demo');
var states=["Bihar","Bengal","Orissa","Jharkhand","Delhi","Daman & Diu"];
// constructs the suggestion engine
var states1 = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
// states is an array of state names defined in "The Basics"
local: $.map(states, function(state) { return { value: state }; })
});
// kicks off the loading/processing of local and prefetch
states1.initialize();

$('#bloodhound.typeahead').typeahead({
  hint: true,
  highlight: true,
  minLength: 1

},
{
  name: 'states1',
  displayKey: 'value',
  // `ttAdapter` wraps the suggestion engine in an adapter that
  // is compatible with the typeahead jQuery plugin
  source: states1.ttAdapter(),
  templates: {
      empty: [
          '<div class="noitems">',
          'No Items Found',
          '</div>'
      ].join('\n')
  }
});
 //Get the Typeahead Value on Following Events
$('input').on([
    'typeahead:initialized',
    'typeahead:initialized:err',
    'typeahead:selected',
    'typeahead:autocompleted',
    'typeahead:cursorchanged',
    'typeahead:opened',
    'typeahead:closed'
].join(' '), function(x) {
    //console.log(this.value); 
});

}
);

@mrchief
Copy link

mrchief commented Apr 8, 2015

This gist is confusing since it re-uses the same variable states to refer to the engine instance as well as the data array.

I think it'd better if we renamed the engine to something like
var engine = new Bloodhound({....

Revised gist here.

@phily245
Copy link

I agree with @mrchief above. His fork also has the bloodhound typeahead adapter and uses the jQuery map function to map the array of strings to an array of objects, which makes the example in the documentation work "out of the box".

@TinnSoft
Copy link

Hi, I am having the same issue.. I am trying with the basic example and did not work :(
Any suggestion?

@suharich
Copy link

suharich commented Jul 12, 2017

Had the same issue. While debugging recognize that data was wrong cached.
window.localStorage.clear()
fixed the issue.
when it can't find data in cache it goes to fetch functionality and then it creates correct data structure.

Also be sure that
datumTokenizer
and
queryTokenizer

is initilized like a function, for example:

datumTokenizer: function (data) {
var wordToken = A.Bloodhound.tokenizers.whitespace(data.name);
return wordToken;
},
queryTokenizer: function (searchStr) {
return A.Bloodhound.tokenizers.nonword(searchStr);
},
where arguments:
data - it's row with column 'name'
searchStr - it's string which were typed by user in a control

@ulvido
Copy link

ulvido commented Jul 21, 2017

for the newbeies like me; to make bloodhound work standalone...

$(document).ready(function() {

/ / add states var here (search data)
var states = ['Alabama', 'Chicago',etc ]

// constructs the suggestion engine
var states = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
// states is an array of state names defined in "The Basics"
local: states
});

$('#bloodhound .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'states',
source: states
});
});

@niketpathak
Copy link

niketpathak commented May 24, 2018

A nice article based on a maintained fork(v1.2.1) since the original project has been abandoned since 3+ years (Ref)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment