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

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

问题描述

我正在使用带有 ajax 调用的 twitter typeahead (typeahead.js 0.11.1) 插件,该插件可以工作,但是当 ajax 结果的数量小于限制时会出现一些奇怪的行为(默认限制是 5,我没有'未在预先输入调用中指定).这是我的设置方式:

var limit = 6;populate_typeahead = 函数(){$('.typeahead').typeahead('destroy');$('.typeahead').typeahead({提示:真实,亮点:真实,最小长度:1},{来源:ajaxquery,});};var ajaxquery = 函数(查询,同步结果,进程){返回 $.ajax({url: $(this)[0].$el.closest('span.twitter-typeahead').find('input:last').data('mahiFindByPath'),类型:'获取',数据:{search_string:查询,限制:限制},数据类型:'json',成功:函数(json){返回 typeof json.options == 'undefined' ?假:进程(json.options);}});};

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

<预><代码>['新添加到 g1','新加入 grp1 然后从 grp 中删除','新拖','新分配']

但是出现的唯一建议是new to add to g1",当所有 4 个都应显示时.如果我继续输入new to d",则会出现new to drag"建议.如果我只输入new",那么我会得到完整的 5 条建议,包括上述大部分内容!

如果我更改 var limit = 5; 然后 ajax 调用最多只返回 5 个结果,并且建议列表根本不会显示,直到我得到新到 d"即"new ' 没有给出任何建议.这让我认为这与 ajax 结果的数量小于或等于 typeahead 限制有关.为了测试我的理论,我将 5 个无意义的项目推送到 json.options 数组中,所以总是有 5 个以上的结果,而且一切都按预期工作——只是我不想总是在我的列表底部有无意义的建议.非常感谢任何建议!

解决方案

我遇到了类似的问题.它看起来像是 Typeahead 0.11.1 版中的一个错误.在这里尝试解决方案:https://github.com/twitter/typeahead.js/pull/1212看看这是否有效.

具体的变化是这样的:https://github.com/per-nilsson/typeahead.js/commit/387290b1e7065ahead5c55c55c65c6d55c55c55c65d55c57a57a5c5d55c57a7d55c55c55c/p>

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);
            }
        });
    };

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'
]

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!

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!

解决方案

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.

The specific change is this: https://github.com/per-nilsson/typeahead.js/commit/387290b1e70be0052c6dd9be84069f55d54a7ce7

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

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