Typeahead and Bloodhound - how to get JSON result
Asked Answered
W

2

18

I have the json list of Countries: http://vocab.nic.in/rest.php/country/json

And I'm trying to get country_id and country name with Bloodhound suggestion engine. O tried following code:

var countries = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('country_name'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    limit: 10,
    prefetch: {
        url: 'http://vocab.nic.in/rest.php/country/json',
        filter: function(response) {
            return response.countries;
        }
    }
});

$('#my-input').typeahead({
        hint: true,
        highlight: true,
        minLength: 1
    },
    {
        name: 'states',
        displayKey: 'value',
        source: countries.ttAdapter()
    });

Which doesn't work. How should I change the code to make this work?

Weighin answered 29/3, 2014 at 11:20 Comment(1)
Is vocab.nic.in/rest.php/country/json in the same domain as your website? If not then you'll need to use "remote" instead of "prefetch"Joinery
S
16
// instantiate the bloodhound suggestion engine
var countries = new Bloodhound({  
  datumTokenizer: function(countries) {
      return Bloodhound.tokenizers.whitespace(countries.value);
  },
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: {
    url: "http://vocab.nic.in/rest.php/country/json",
    filter: function(response) {      
      return response.countries;
    }
  }
});

// initialize the bloodhound suggestion engine
countries.initialize();

// instantiate the typeahead UI
$('.typeahead').typeahead(
  { hint: true,
    highlight: true,
    minLength: 1
  }, 
  {
  name: 'countries',
  displayKey: function(countries) {
    return countries.country.country_name;        
  },
  source: countries.ttAdapter()
});

Example Codepen

Typeahead Output

Output of Typeahead

Notes:

  • data on your server = "prefetch".
  • data from outside = "remote"
Silversmith answered 29/3, 2014 at 13:8 Comment(9)
Thanks, looks good, but gives me Uncaught TypeError: Cannot call method 'split' of undefined in bloodhound.min.jsWeighin
in the datumTokenizer i used countries.countries, which must be countries.value. Updated code and Codepen accordingly. Chrome is clowny to me, use firefox :)Silversmith
This solution no longer works. I tried it myself, and also on your codepen example.Gree
True, stopped working. Well, its over a year old. I guess, one of the APIs changed again.Silversmith
I've updated the code so that it works again. The problem was the "prefetch"ing of the data. I've changed "prefetch" to "remote" . Updated the Codepen accordingly.Silversmith
I know it's been a while since this was written, codepen doesn't seem to work again ... try searching for 'Bulgaria'. I get the same list as in your screenshot.Pursuer
it displays 5 result always on any country search ... i.e. BnagladeshMicrobiology
I apologize. To be honest, i think twitter gave up on typeahead.js. We look at 13000 stars, a full bugtracker with no maintainer and a broken software, last release 2015. I think that speaks for itself, or not? ... So, try one of the forks: github.com/corejavascript/typeahead.js --- I believe they fixed this issue. Not tested. Feel also free to modify my codepen exchanging the typeahead.js against the script of the fork. Regards, JensSilversmith
FYI, the codepen is not working because of http/https - The page at 'codepen.io/anon/pen/NGMvJQ' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'vocab.nic.in/rest.php/country/json'.Hixon
D
1

NOTE: if you do all this and it still is not working examine your data.json file (whatever you have named it)

example of good format: https://github.com/twitter/typeahead.js/blob/gh-pages/data/countries.json

['data1','data2',.....]

I was tripped up on out of place quote.

Dishearten answered 16/9, 2019 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.