Bootstrap Typeahead:如何在typeahead bind函数中获取所有获取的远程值

时间:2017-07-25 11:09:52

标签: javascript jquery twitter-bootstrap bootstrap-typeahead twitter-typeahead

是否可以在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中的所有bankNames,但它给了我

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函数中的所有bankNames,我需要执行一些操作。

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

1 个答案:

答案 0 :(得分:1)

我在transform的帮助下得到了解决方案,Bloodhoundtransform的一部分。 配置远程选项时,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); }); 选项可用。

  

transform - 具有签名变换(响应)的函数   允许您在Bloodhound之前转换远程响应   实例对它进行操作。

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

bind

现在如果你在typeahead console.log(bankNameList);的{​​{1}}函数中打印bankNameList它将打印从远程获取的数据。