我无法获取typeahead.js以仅返回与输入查询匹配的结果。例如,如果我在我的公司搜索栏中输入“Facebook”,它将返回所有公司(“雅虎”,“谷歌”等),即使其中大多数与查询不匹配。我没有做任何服务器端的数据处理。我的datumTokenizer函数应该处理这个过滤吗?
另外,我注意到每次修改查询时,它都会为每个数据输入filter()函数。因此,当我将查询从“G”更改为“Go”时,filter:function(companies_list)中的console.log()语句将打印3000次。
这是我的代码:
var companies = new Bloodhound({
datumTokenizer: function (datum) {
return Bloodhound.tokenizers.whitespace(datum.name);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/json/company_list.json',
filter: function (companies_list) {
// Map the remote source JSON array to a JavaScript object array
return $.map(companies_list, function (company) {
console.log('mapping')
return {
name: company.name
};
});
}
}
});
// Initialize the Bloodhound suggestion engine
var promise = companies.initialize();
promise.done(function() {console.log('Bloodhound initialized!')});
// passing in `null` for the `options` arguments will result in the default
// options being used
$('#form-company').typeahead(null, {
name: 'companies',
displayKey: 'name',
// `ttAdapter` wraps the suggestion engine in an adapter that
// is compatible with the typeahead jQuery plugin
source: companies.ttAdapter()
});
我的网址返回的示例:
[{"name": "Yahoo"}, {"name": "Sanchai Technologies "}, {"name": "Oliver Wyman"}, {"name": "University of Oregon"}, ...]
我正在使用远程,因为prefetch对我来说绝对不起作用。它只给我一个建议[object Object],这没有任何意义。我想使用prefetch / remote在初始化时加载整个.json文件,而不是向服务器发出任何进一步的请求。所以我认为预取是我更好的选择(小文件,77kB),但它根本就不起作用。
非常感谢你的帮助!
答案 0 :(得分:0)
我认为你错过了#34;准备"。尝试像我一样的事情
<servlet>
<servlet-name>my-servlet-name</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>my.package.name</param-value>
</init-param>
<!-- <load-on-startup>1</load-on-startup> -->
</servlet>
或者,你可以在那里查看我的帖子。它只返回用户进入文本框的结果。 My Stackoverflow post
答案 1 :(得分:0)
我也有这个问题。问题是远程URL返回标准列表,没有能力将查询传递给它,以便它返回与搜索项匹配的过滤子集。 因此,过滤器实现必须处理过滤结果,以便它们与查询匹配。