twitter typeahead ajax 结果未全部显示

twitter typeahead ajax results not all shown

我正在使用带有 ajax 调用的 Twitter typeahead (typeahead.js 0.11.1) 插件,但当 ajax 结果数量较少时会出现一些奇怪的行为超过限制(默认限制为 5,我没有在 typeahead 调用中指定)。以下是我的设置方式:

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",其中 returns 以下 json.options:

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

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

如果我更改 var limit = 5;,那么 ajax 只会调用 returns 最多 5 个结果,并且建议列表根本不会显示,直到我得到 'new to d' 即 'new ' 没有给出任何建议。这让我认为这与 ajax 结果的数量小于或等于预输入限制有关。为了检验我的理论,我将 5 个无意义的项目推入 json.options 数组,因此总是有 5 个以上的结果,并且都按预期工作 - 只是我不想在我的列表底部总是有无意义的建议。非常感谢任何建议!

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

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

听从 agustaf 的建议。我正在使用 twitter-typeahead-rails gem。我创建了一个分支并将版本升级到 1.0.1,这为我修复了这个可能相同或相关的错误。

在我的例子中,我有两个来自远程源的结果,但只有其中一个用于建议。

我做了一个拉取请求 - 但不确定它是否会被合并。但是,对于可能会看到此内容的未来 rails 用户,请使用我的叉子,特别是我声明的分支。

添加到您的gem文件

gem 'twitter-typeahead-rails', :git => "git://github.com/pitops/twitter-typeahead-rails.git" , :branch => "bump_version_to_1.0.1"