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

javascript - Why typeahead.js is not showing all items in the ajax response? - Stack Overflow

programmeradmin2浏览0评论

I am trying to use typeahead.js for showing ajax results in a form. At first I tried with Bloodhound suggestion engine which es with typeahead by default. But it wasn't showing all the items (i just shows 1 or sometimes two) returned by the server.

Following is the code I used;

 $('.autoplete').livequery(function () {
            var input = this;
            $(input).removeClass('autoplete');

            var _source = new Bloodhound({
                limit: 25,
                datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Value'),
                queryTokenizer: Bloodhound.tokenizers.obj.whitespace('Text'),
                prefetch: $(input).attr('data-source'),
                remote: {
                    url: $(input).attr('data-source') + '?query=%QUERY',
                    wildcard: '%QUERY'
                }
            });

            _source.initialize();

            $(input).typeahead({
                hint: true,
                highlight: true,
                minLength: 0
            }, {
                displayKey: 'Text',
                source: _source,
                templates: {
                    notFound: 'No results found'
                }
            });

            $(input).on('typeahead:selected', function (evt, item) {
                $(input).parent().parent().find('input[type="hidden"]').val(item.Value);
            });
        })

Then I tried to do it without Bloodhound using following code without any change in results;

 $('.autoplete').livequery(function () {
            var input = this;
            $(input).removeClass('autoplete');

            $(input).typeahead({
                hint: true,
                highlight: true,
                limit: 50,
                minLength: 0
            }, {
                displayKey: 'Text',
                source: function (query, syncResults, asyncResults) {
                    $.get($(input).attr('data-source') + '?query=' + query, function (data) {
                        asyncResults(data);
                    });
                },
                templates: {
                    notFound: 'No results found'
                }
            });

            $(input).on('typeahead:selected', function (evt, item) {
                $(input).parent().parent().find('input[type="hidden"]').val(item.Value);
            });
        })

The response from the Server for the requests is as follows;

[
{
    "Text": "Marketing Executive",
    "Value": 1
},
{
    "Text": "CEO",
    "Value": 5
},
{
    "Text": "Manager",
    "Value": 6
},
{
    "Text": "Accountant",
    "Value": 7
}]

What is wrong with this? and how can I get typeahead to display all results returned by the Server?

I am trying to use typeahead.js for showing ajax results in a form. At first I tried with Bloodhound suggestion engine which es with typeahead by default. But it wasn't showing all the items (i just shows 1 or sometimes two) returned by the server.

Following is the code I used;

 $('.autoplete').livequery(function () {
            var input = this;
            $(input).removeClass('autoplete');

            var _source = new Bloodhound({
                limit: 25,
                datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Value'),
                queryTokenizer: Bloodhound.tokenizers.obj.whitespace('Text'),
                prefetch: $(input).attr('data-source'),
                remote: {
                    url: $(input).attr('data-source') + '?query=%QUERY',
                    wildcard: '%QUERY'
                }
            });

            _source.initialize();

            $(input).typeahead({
                hint: true,
                highlight: true,
                minLength: 0
            }, {
                displayKey: 'Text',
                source: _source,
                templates: {
                    notFound: 'No results found'
                }
            });

            $(input).on('typeahead:selected', function (evt, item) {
                $(input).parent().parent().find('input[type="hidden"]').val(item.Value);
            });
        })

Then I tried to do it without Bloodhound using following code without any change in results;

 $('.autoplete').livequery(function () {
            var input = this;
            $(input).removeClass('autoplete');

            $(input).typeahead({
                hint: true,
                highlight: true,
                limit: 50,
                minLength: 0
            }, {
                displayKey: 'Text',
                source: function (query, syncResults, asyncResults) {
                    $.get($(input).attr('data-source') + '?query=' + query, function (data) {
                        asyncResults(data);
                    });
                },
                templates: {
                    notFound: 'No results found'
                }
            });

            $(input).on('typeahead:selected', function (evt, item) {
                $(input).parent().parent().find('input[type="hidden"]').val(item.Value);
            });
        })

The response from the Server for the requests is as follows;

[
{
    "Text": "Marketing Executive",
    "Value": 1
},
{
    "Text": "CEO",
    "Value": 5
},
{
    "Text": "Manager",
    "Value": 6
},
{
    "Text": "Accountant",
    "Value": 7
}]

What is wrong with this? and how can I get typeahead to display all results returned by the Server?

Share Improve this question asked Sep 12, 2015 at 5:14 Libin TKLibin TK 1,4972 gold badges26 silver badges48 bronze badges 4
  • by default only 5 suggestions will be shown , use limit options to increase it . check docs – J Santosh Commented Sep 12, 2015 at 9:51
  • @JSantosh limit: 50, check the question. – a better oliver Commented Sep 12, 2015 at 9:55
  • you can't see 50 results right ? – J Santosh Commented Sep 12, 2015 at 9:58
  • @JSantosh I read somewhere that typeahead has some issues with showing all asyncresults when syncresults are fewer. Setting a higher limit was a workaround found on the internet. – Libin TK Commented Sep 12, 2015 at 12:16
Add a ment  | 

1 Answer 1

Reset to default 8

This is a bug in the control. This can be fixed by below code change in typeahead.bundle.js

Switch lines 1723 and 1724 so it looks like this

that._append(query, suggestions.slice(0, that.limit - rendered));
rendered += suggestions.length;

Kudos for the post Bootstrap Typeahead not showing hints as expected

发布评论

评论列表(0)

  1. 暂无评论