Search code examples
twitter-typeaheadbloodhound

Get Twitter Bloodhound Typeahead suggestions without opening dropdown


I am able to get all the Twitter Typeahead suggestions with the code below after user enters an input and the typeahead:render is called. I would like to hide the dropdown all the time and get the suggestions only in an array. Is there a way to achieve this since typeahead:render would probably require the dropdown be opened.

        var bloodhoundData = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.whitespace,
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            local: localData
        });

        $('filter .typeahead').typeahead({
                hint: true,
                highlight: true,
                minLength: 1
            },
            {
                source: bloodhoundData,
                limit: 99999
            }).on('typeahead:render', getSuggestions);

        function getSuggestions() {
            var suggestions = Array.prototype.slice.call(arguments, 1);
        }

Solution

  • Since Bloodhound.js is a standalone library, you don't have to use typeahead with it. You could tie the input for bloodhound to an ordinary text input and examine the result of the get method.

    Something like this might work, with q being the text from the input (borrowed from the NFL Teams example):

    var myBloodhound = new Bloodhound({
      datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      identify: function(obj) { return obj.name; },
      local: localData
    });
    
    function getMyData(q, sync) {
      myBloodhound.search(q, sync);
    }
    

    You can check out the bloodhound documentation here and the examples here.