最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I list objects for Typeahead.js andor with the Bloodhound engine? - Stack Overflow

programmeradmin1浏览0评论

I'm having a hard time figuring out how to display a list of objects using typeahead with a json file as the source. None of my data is being displayed.

I want to list the names, and use the other attributes for other things when selected.

../data/test.json

[   
    {"name": "John Snow", "id": 1},
    {"name": "Joe Biden", "id": 2},
    {"name": "Bob Marley", "id": 3},
    {"name": "Anne Hathaway", "id": 4},
    {"name": "Jacob deGrom", "id": 5}
]

test.js

$(document).ready(function() {
    var names = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.whitespace("name"),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        prefetch: {
          url: '../data/test.json'
        }
    });
    names.initialize();

    $('#test .typeahead').typeahead({
        name: 'names',
        displayKey: 'name',
        source: names.ttAdapter()
    });
)};

test.html

<div id="test">
    <input class="typeahead" type="text">
</div>

** And if someone can explain to me what the datumTokenizer and queryTokenizer is, that would be awesome **

I'm having a hard time figuring out how to display a list of objects using typeahead with a json file as the source. None of my data is being displayed.

I want to list the names, and use the other attributes for other things when selected.

../data/test.json

[   
    {"name": "John Snow", "id": 1},
    {"name": "Joe Biden", "id": 2},
    {"name": "Bob Marley", "id": 3},
    {"name": "Anne Hathaway", "id": 4},
    {"name": "Jacob deGrom", "id": 5}
]

test.js

$(document).ready(function() {
    var names = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.whitespace("name"),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        prefetch: {
          url: '../data/test.json'
        }
    });
    names.initialize();

    $('#test .typeahead').typeahead({
        name: 'names',
        displayKey: 'name',
        source: names.ttAdapter()
    });
)};

test.html

<div id="test">
    <input class="typeahead" type="text">
</div>

** And if someone can explain to me what the datumTokenizer and queryTokenizer is, that would be awesome **

Share Improve this question edited Oct 7, 2014 at 22:36 Ben Smith 20.3k6 gold badges73 silver badges97 bronze badges asked Oct 7, 2014 at 2:36 VongdarakiaVongdarakia 4791 gold badge14 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

The JSON file contains an array of JSON objects, but the Bloodhound suggestion engine expects an array of JavaScript objects.

Hence you need to add a filter to your prefetch declaration:

prefetch: {
 url: '../data/test.json',
 filter: function(names) {
   return $.map(names, function(name) { 
    return { name: name };
 });
}

As for the "datumTokenizer", it's purpose is to determine how the datums (i.e. the suggestion values) should be tokenized. It is these tokens which are then used to find a match with the input query.

For instance:

Bloodhound.tokenizers.whitespace("name")

This takes a datum (in your case a name value) and splits it into two tokens e.g. "Bob Marley" will be split into two tokens, "Bob" and "Marley".

You can see how the whitespace tokenizer works by viewing the typeahead source:

function whitespace(str) {
 str = _.toStr(str);
 return str ? str.split(/\s+/) : [];
}

Note how it splits the datums using the regex for whitespace (\s+ i.e. one or more occurrences of whitespace).

Similarly, the "queryTokenizer" also determines how to tokenize the search term. Again, in your example you are using the whitespace tokenizer, so a search term of "Bob Marley" will result in the datums "Bob" and "Marley".

Hence with the tokens determined, if you were to search for "Marley", a match would be found for "Bob Marley".

I have a simpler option, what you have done is correct except one small error. Actually you can use an object array as you have shown.

Replace displayKey: 'name' with display: 'name' and it should work.

So, the full typeahead function will look like

$('#test .typeahead').typeahead({
    name: 'names',
    display: 'name',
    source: names.ttAdapter()
});
发布评论

评论列表(0)

  1. 暂无评论