简体   繁体   English

如何使带有Bloodhound的Typeahead与JSON响应一起使用?

[英]How to get Typeahead with Bloodhound to work with a JSON response?

I am trying to figure out how to get typeahead.js with bloodhound.js to work with my JSON structure. 我试图弄清楚如何使用bloodhound.js获取typeahead.js以使用我的JSON结构。 What I want is a type-ahead functionality with a preloaded JSON object which stays in scope. 我想要的是带有预加载的JSON对象的预定义功能,该对象保留在范围内。 Here are the parts: 以下是部分:

Data/All url returns an application/json response with the following structure: Data/All url返回具有以下结构的application/json响应:

[{"Id":1010,"Name":"Andijvie ", "Category":"Blad- en stengel gewassen"},{"Id":1020,"Name":"Broccoli ","Category":"Blad- en stengel gewassen", (...)] [{“ Id”:1010,“ Name”:“ Andijvie”,“ Category”:“ Blad- en stengel gewassen”},{“ Id”:1020,“ Name”:“ Broccoli”,“ Category”:“ Blad -en stengel gewassen“,(...)]

Furthermore, on the view I have: 此外,我认为:

<div id="select-crop">
    <input class="typeahead" type="text" placeholder="Select crop">
</div> 

And in JavaScript: 在JavaScript中:

var cropsSuggestionEngine = new Bloodhound({
    datumTokenizer: function (datum) {
        return Bloodhound.tokenizers.whitespace(datum.Name);
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    prefetch: {
        url: "/Crop/All",
        filter: function (data) {
            return $.map(data, function(crop) {
                return {
                    value: crop.Name
                };
            });
        }
    }
});

$(function() {

    cropsSuggestionEngine.initialize();

    $('#select-crop .typeahead').typeahead({
        hint: true,
        highlight: true,
        minLength: 1
    }, {
        name: 'Crops',
        displayKey: 'Name',
        source: cropsSuggestionEngine.ttAdapter(),
        templates: {
            empty: [
            '<div class="empty-message">',
            'unable to find any results that match the current query',
            '</div>'
            ].join('\n'),
            suggestion: Handlebars.compile('<p><strong>{{name}}</strong></p>')
        }
    }); 

});

When something is typed into the typeahead input field it always gives the message that there are not any results that match the query. 在“预输入”输入字段中键入内容时,它始终会提示您没有与查询匹配的结果。 When I look in the DOM via FireBug I see that datums consists of a long list with empty elements. 当我通过FireBug查看DOM时,我看到基准由长列表组成,其中包含空元素。

I hope someone with bloodhound/typeahead experience can point me in the right direction. 我希望有猎狗/快感经验的人能为我指明正确的方向。 I cannot seem to figure it out now. 我现在似乎无法弄清楚。 Thanks. 谢谢。

The typeahead can't find any 'Name' keys in the suggestion objects in the prefetched dataset. 预先输入在预提取的数据集中的建议对象中找不到任何“名称”键。

When you define the typeahead, you are saying that the 'displayKey' key is "Name". 定义快捷输入时,是说“ displayKey”键为“名称”。 But when you prefect the dataset, your filter function (in the cropSuggestionEngine) returns an array of json objects like this: [{value: "Andijvie "},{value: "Broccoli "}, ...] 但是,当您完善数据集时,您的filter函数(在cropSuggestionEngine中)将返回一个json对象数组,如下所示: [{value: "Andijvie "},{value: "Broccoli "}, ...]

So there aren't any "Name" keys. 因此,没有任何“名称”键。

If you change the filter function to return { Name: crop.Name } , your code works fine. 如果将过滤器功能更改为返回{ Name: crop.Name } ,则代码可以正常工作。

filter: function(data) {
    return $.map(data, function(crop) {
        return {
            Name: crop.Name
        };
    });
}

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM