gpt4 book ai didi

javascript - Bootstrap 提前输入 : How to get all fetched remote values in typeahead bind function

转载 作者:行者123 更新时间:2023-12-03 03:47:45 24 4
gpt4 key购买 nike

是否可以在 typeahead 绑定(bind)函数中获取所有获取的远程值?

var bankNames = new Bloodhound({
datumTokenizer: function (datum) {
return Bloodhound.tokenizers.whitespace(datum.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 10,
remote: {
url: '/payments/bankwithdrawal/bankdetails?str=%QUERY,
prepare: function (query, settings) {
var encoded = query.toUnicode();
settings.url = settings.url.replace('%QUERY', encoded);
return settings;
}
}
});

bankNames.initialize();

// Initializing the typeahead
$('.typeahead').typeahead({
hint: true,
highlight: true, // Enable substring highlighting
minLength: 1 // Specify minimum characters required for showing result
},
{
name: 'bankname',
source: bankNames
}).bind('change blur', function () {

console.log(bankNames);

console.log(bankNames.index.datums);

});

它应该给我 bankNames.index.datums 中的所有银行名称,但它给了我

Object {}
__proto__: Object
constructor: function Object()
hasOwnProperty: function hasOwnProperty()
isPrototypeOf: function isPrototypeOf()
propertyIsEnumerable: function propertyIsEnumerable()
toLocaleString: function toLocaleString()
toString: function toString()
valueOf: function valueOf()
__defineGetter__: function __defineGetter__()
__defineSetter__: function __defineSetter__()
__lookupGetter__: function __lookupGetter__()
__lookupSetter__: function __lookupSetter__()
get __proto__: function __proto__()
set __proto__: function __proto__()

我需要绑定(bind)函数中的所有银行名称,我需要在其中执行某些操作。

当我在绑定(bind)函数 console.log(bankNames) 中打印时,它应该返回所有获取的远程值。

最佳答案

我在 transform 的帮助下得到了解决方案,它是 Bloodhound 的一部分。配置远程选项时,transform 选项可用。

transform – A function with the signature transform(response) that allows you to transform the remote response before the Bloodhound instance operates on it.

您可以在此链接中阅读有关 Bloodhound 选项的更多信息 https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#remote

var bankNameList;
var bankNames = new Bloodhound({
datumTokenizer: function (datum) {
return Bloodhound.tokenizers.whitespace(datum.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 10,
remote: {
url: '/payments/bankwithdrawal/bankdetails?str=%QUERY,
prepare: function (query, settings) {
var encoded = query.toUnicode();
settings.url = settings.url.replace('%QUERY', encoded);
return settings;
},
transform : function (data) {
bankNameList = data;
return data;
}
}
});

bankNames.initialize();

// Initializing the typeahead
$('.typeahead').typeahead({
hint: true,
highlight: true, // Enable substring highlighting
minLength: 1 // Specify minimum characters required for showing result
},
{
name: 'bankname',
source: bankNames
}).bind('change blur', function () {

console.log(bankNameList);

});

现在,如果您在 typeahead console.log(bankNameList);bind 函数内打印bankNameList,它将打印从远程获取的数据。

关于javascript - Bootstrap 提前输入 : How to get all fetched remote values in typeahead bind function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45301529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com