简体   繁体   English

typeahead.js - 渲染事件 - 在一个争论中没有获得建议数组

[英]typeahead.js - render event - Not getting suggestion array in one arguement

I am trying to use the typeahead render event, but can't get the arguments passed in correctly.. 我试图使用typeahead渲染事件,但无法正确传递参数..

Referring to https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#custom-events the render event should pass though 4 arguments.. 参考https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#custom-events ,渲染事件应通过4个参数。

I've setup my typeahead and event handler as follows: 我已经设置了我的typeahead和事件处理程序,如下所示:

         $('#input').typeahead({
         hint: true,
         highlight: true,
         minLength: 1
     },
 {
     name: 'items',
     source: items
 })
 .on('typeahead:render', onRender);

 function onRender($event, $suggestions, $async, $dataSet)
         {
}

The render event files when expected, but it's not passing the arguments correctly. 预期的渲染事件文件,但它没有正确传递参数。

$event is the jQuery event object as specified.. But, I'd expect the second argument, $suggestions to be an array containing the current suggestions, but it only contains the first suggestion.. The next two arguments contain the 2nd and 3rd suggestions, and not the async flag, and dataset name as expected. $ event是指定的jQuery事件对象..但是,我希望第二个参数,$ suggestions是一个包含当前建议的数组,但它只包含第一个建议..接下来的两个参数包含第二个和第三个建议,而不是async标志,以及预期的数据集名称。

See below for an example of what I am doing.. Arguments sent to console. 请参阅下面的示例,了解我正在做的事情。发送到控制台的参数。

 var substringMatcher = function(strs) { return function findMatches(q, cb) { var matches, substringRegex; // an array that will be populated with substring matches matches = []; // regex used to determine if a string contains the substring `q` substrRegex = new RegExp(q, 'i'); // iterate through the pool of strings and for any string that // contains the substring `q`, add it to the `matches` array $.each(strs, function(i, str) { if (substrRegex.test(str)) { matches.push(str); } }); cb(matches); }; }; var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming' ]; console.log("starting"); $('#the-basics .typeahead').typeahead({ hint: true, highlight: true, minLength: 1 }, { name: 'states', source: substringMatcher(states) }) .on('typeahead:render', onRender); function onRender($event, $suggestions, $async, $dataSet) { console.log($event); console.log($suggestions); console.log($async); console.log($dataSet); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script> <div id="the-basics"> <input type="text" class="typeahead" /> </div> 

I found this to get the suggestions 我发现这是为了得到建议

x.bind('typeahead:render',
            function (ev) {
                var suggestions = Array.prototype.slice.call(arguments, 1);
            }
        );

the async and datasets arguments appear to be completely missing async和datasets参数似乎完全缺失

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM