显示有关typeahead和bloodhound点击的完整建议列表

时间:2014-11-25 22:39:29

标签: javascript jquery typeahead.js bloodhound

我正在使用带有血腥建议引擎的Typeahead.js,并希望用户在搜索框中点击后立即显示该列表。

我发现这个stackoverflow问题(Twitter TypeAhead show all results programmatically)和我一样,答案指向解决问题的jsfiddle:http://jsfiddle.net/5xxYq/

然而,似乎只有在不使用猎犬作为先行者的来源时才会起作用。

e.g。我分叉了他们的工作示例并将typeahead源切换为使用bloodhound:http://jsfiddle.net/5xxYq/31/。建议引擎工作正常(当我键入jo列表出现时),但是点击上显示建议的小黑客似乎不再起作用了。

关于如何制作建议清单的任何想法都会出现在点击血腥的地方?

谢谢!

2 个答案:

答案 0 :(得分:3)

如果您将此解决方案与bloudhound结合使用,则需要更改bloodhound.js或bundle.js。

在typeahead.bundle.js或bloodhound.js中添加查找此代码(第450行)

return matches ? _.map(unique(matches), function(id) {
                return that.datums[id];
            }) : [];

如果令牌输入为空,请添加此检查以返回所有建议:

if (tokens == '') return that.datums;

现在看起来像:

if (tokens == '') return that.datums;
return matches ? _.map(unique(matches), function(id) {
                    return that.datums[id];
                }) : [];

我已经在你的小提琴中测试了这个并且它有效。

答案 1 :(得分:2)

我认为可能有更好的方法。没有改变猎犬/边界js,但它仍然取决于可能改变的内部猎犬实施。

var searchEngine = new Bloodhound({...});
function searchWithDefaults(q, sync) {
  if (q === '') {
    sync(searchEngine.index.all());
  } else {
    searchEngine.search(q, sync);
  }
}
$("#typeahead").typeahead({
  minLength : 0,
}, {
  name : 'typeahead',
  source : searchWithDefaults
});

此代码利用名为SearchIndex的Bloodbound内部搜索引擎及其函数all()的实现,该函数返回Bloodhound存储的完整数据列表。

答案受到启发: