twitter typeahead ajax结果未全部显示 [英] twitter typeahead ajax results not all shown

查看:219
本文介绍了twitter typeahead ajax结果未全部显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有ajax调用的twitter typeahead(typeahead.js 0.11.1)插件,但是当ajax结果的数量小于限制(默认限制为5,我没有)时,它给出了一些奇怪的行为在预先输入调用中未指定)。我是这样设置的:

I am using the twitter typeahead (typeahead.js 0.11.1) plugin with an ajax call which works, but gives some strange behavior when the number of ajax results are less than the the limit (default limit is 5 which I haven't specified in the typeahead call). Here is how I have set it up:

var limit = 6;

populate_typeahead = function() {
    $('.typeahead').typeahead('destroy');
    $('.typeahead').typeahead({
      hint: true,
      highlight: true,
      minLength: 1
    },
    {
      source: ajaxquery,
    });
};

var ajaxquery = function(query, syncresults, process) {
        return $.ajax({
            url: $(this)[0].$el.closest('span.twitter-typeahead').find('input:last').data('mahiFindByPath'),
            type: 'get',
            data: {search_string: query, limit: limit},
            dataType: 'json',
            success: function(json) {
                return typeof json.options == 'undefined' ? false : process(json.options);
            }
        });
    };

导致我出现此问题的实际示例是在输入框中键入 new to返回以下json.options:

The actual example that is causing me the issue, is by typing "new to" in the input box which returns the following json.options:

[
    'new to add to g1',
    'new to be in grp1 then remove from grp',
    'new to drag',
    'new to assign'
]

但是出现的唯一建议是全部添加到g1中,这时所有4个都应显示。如果我继续输入新到d,则会出现新拖动建议。如果我只输入'new',那么我将得到包括上述大多数内容在内的完整5条建议!

But the only suggestion that comes up is 'new to add to g1', when all 4 should show. If I continue typing 'new to d' then the 'new to drag' suggestion comes up. If I type only 'new ' then I get a full 5 suggestions including most of the above!

如果我更改 var limit = 5; ,那么ajax调用最多只会返回5个结果,并且直到我收到 new to d(即 new)没有给出任何建议,建议列表才显示出来。这使我认为与ajax结果的数量小于或等于预输入限制有关。为了验证我的理论,我将5个无意义的项目放入json.options数组中,因此总会有5个以上的结果,并且全部按预期工作-只有我不想总是在列表的底部有无意义的建议。

If I change var limit = 5; then the ajax call only ever returns at most 5 results and the suggestion list doesn't show up at all until I get 'new to d' i.e. 'new ' gives no suggestions. This make me think it is to do with the number of ajax results being less than or equal to the typeahead limit. To test my theory I pushed 5 meaningless items into the json.options array so there were always more than 5 results and it all worked as expected - only I don't want to always have meaningless suggestions at the bottom of my list. Any advice greatly appreciated!

推荐答案

我遇到了类似的问题。它看起来像是Typeahead 0.11.1版中的错误。在此处尝试解决方案:
https://github.com/twitter/typeahead.js/pull/1212
,看看是否可行。

I had a similar problem. It looks like its a bug in version 0.11.1 of Typeahead. Try the solution here: https://github.com/twitter/typeahead.js/pull/1212 and see if that works.

具体的更改是:
https://github.com/per-nilsson/typeahead.js/commit/387290b1e70be0052c6dd9be84069f55d54a7ce7

这篇关于twitter typeahead ajax结果未全部显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆