微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery – Typeahead和Bloodhound – 如何获取JSON结果

我有国家列表: http://vocab.nic.in/rest.php/country/json

而我正在尝试通过Bloodhound建议引擎获取country_id和country name. O尝试以下代码

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()
    });

哪个不行.我应该如何更改代码以使其工作?

解决方法

// instantiate the bloodhound suggestion engine
var countries = new Bloodhound({  
  datumTokenizer: function(countries) {
      return Bloodhound.tokenizers.whitespace(countries.value);
  },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,minLength: 1
  },{
  name: 'countries',displayKey: function(countries) {
    return countries.country.country_name;        
  },source: countries.ttAdapter()
});

Example Codepen

前置输出

笔记:

>您的服务器上的数据=“prefetch”.> data from outside =“remote”

原文地址:https://www.jb51.cc/jquery/176381.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐