编程语言
首页 > 编程语言> > javascript – 推特使用JSON对象预先输入Bloodhound

javascript – 推特使用JSON对象预先输入Bloodhound

作者:互联网

我无法使用JSON对象.我在这里关注过多个问题,没有一个答案对我有帮助.

$(function() {
 var items = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    limit: 10,
    prefetch: {
        url: 'items.json',
        filter: function(list) {
            return $.map(list, function(item) {
                return {
                    name: item.name,
                    category: item.category,
                    release: item.release,
                    id: item.id
                };
            });
        }
    }
 });

 items.initialize();

 $('.typeahead').typeahead(null, {
    name: 'items',
    displayKey: 'name',
    source: items.ttAdapter()
 });
});

JSON如下:

[
 {"id":"4","name":"Name 1","release":"July 28, 2014","category":"Bow"},
 {"id":"1","name":"Name 2","release":"October 29, 2014","category":"Bow"},
 {"id":"13","name":"Name 3","release":"November 27, 2014","category":"Arrow"}
]

解决方法:

它不起作用的原因是因为Typeahead的LocalStorage使用(或者我滥用它,我想我们永远不会知道).一旦我清除了它,它立即起作用.

标签:json,javascript,typeahead,bloodhound
来源: https://codeday.me/bug/20190628/1320170.html