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

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

我发现这个stackoverflow问题( Twitter TypeAhead以编程方式显示所有结果 )与我相同,答案指向解决问题的jsfiddle: http : //jsfiddle.net/5xxYq/

大。

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

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

关于如何制作建议列表的任何想法出现在点击血腥?

谢谢!

如果您将此解决方案与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]; }) : []; 

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

我认为可能有更好的方法来做到这一点。 没有改变猎犬/边界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存储的完整数据列表。

答案的灵感来自:

  • Twitters使用默认建议键入示例
  • stackoverflow回答问题’typeahead.js:返回空查询的所有Bloodhound记录’