jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – typeahead.js没有返回所有结果大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我很难找到typeahead.js来返回/渲染我页面上的所有结果.这是我的代码

var places = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.whitespace,queryTokenizer: Bloodhound.tokenizers.whitespace,remote: {
        url: '/api/place/?country={{ country.id }}&name=%QUERY',transform: function (data) {
            return data.response;
        },wildcard: '%QUERY'
    }
});

var SELEcted = false;
$('.typeahead-place').typeahead({
    hint: true,highlight: true,minLength: 2
},{
    name: 'places',displayKey: function (obj) {

        if (obj.url != null && obj.url.length && (obj.street == null || obj.street.length == 0)) {
            return obj.name + " (Online storE)";
        }

        return obj.name + " (" + obj.street + "," + obj.city + "," + obj.postcode + ")";
    },source: places
});

Punn的示例查询从服务器返回JSON:

{
    "response": [
            {
                "id": "4","name": "Punnitse ja Säästä 2","street": "Simonkenttä,Simonkatu 9","city": "Helsinki","postcode": "00100","url": "http://www.punnitse.fi/"
            },{
        "id": "12","name": "Punnitse ja Säästä 3","url": "http://www.punnitse.fi/"
    },{
        "id": "13","name": "Punnitse ja Säästä 4",{
        "id": "9","name": "Punnitse ja Säästä Kamppi","url": "http://www.punnitse.fi/"
    }
    ]
}

在这呈现如下:

jquery – typeahead.js没有返回所有结果

解决方法

这似乎是typeahead.js新版本的 well-known bug.为了使您的代码正常工作,您宁愿切换到0.10.5或更低版本,并稍微转换您的代码

var places = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.whitespace,wildcard: '%QUERY',filter: function(data) {      // <-- Should use `filter`
            return data.response;
        }
    }
});

places.initialize();                  // <-- initialise suggestion

$('.typeahead-place').typeahead({
    hint: true,source: places.ttAdapter(),// <-- get adapter as a source
    displayKey: function(obj) {
        // ...
    }
});

DEMO:https://jsfiddle.net/uszcp6j3/

或者,如果您想坚持使用最新版本,可以将the following patch(前一段时间发布)应用于typeahead.js的源代码.

大佬总结

以上是大佬教程为你收集整理的jquery – typeahead.js没有返回所有结果全部内容,希望文章能够帮你解决jquery – typeahead.js没有返回所有结果所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。