我正在使用猎犬并尝试重置local
选项。在我的网页上我有一种情况,如果用户选择两个选项之一,建议数据将会改变。但是当我设置本地数据时,我无法得到建议。下面是我如何设置猎犬设置它以使用一个名为' spray'的数组:
var dataSetBloodhound = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: spray
});
因此,如果用户选择喷雾jQuery捕获它并尝试将喷雾阵列设置为本地
//If spray is picked
if( checked == "Spray") {
// Clear the object
dataSetBloodhound.clear();
// Set the local 'data' to spray
dataSetBloodhound.local = spray;
// Reinit the object
dataSetBloodhound.initialize()
// Prints out the local data.(This prints out the data of spray)
console.log(dataSetBloodhound.local);
//etc }
如果用户选择肥料,除了传递fertilizer
数组外,该功能与上述相同。
如果我在列表上使用我可以让他们工作,但我似乎无法交换猎犬的本地数据。然而在我initialize()
之后我可以console.log(dataSetBloodhound.local)
我可以在控制台中查看元素,但如果我尝试从下拉列表中获取建议,它就无法工作。有什么想法吗?
答案 0 :(得分:1)
我认为你需要重新初始化,你可以设置
if( checked == "Spray") {
// Clear the object
dataSetBloodhound.clear();
// Set the local 'data' to spray
dataSetBloodhound.local = spray;
// Reinit the object
dataSetBloodhound.initialize(true);
// Prints out the local data.(This prints out the data of spray)
console.log(dataSetBloodhound.local);
请查看文档https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md