Twitter TypeAhead以编程方式显示所有结果



我已经为此绞尽脑汁一段时间了。还有其他的答案漂浮在SO和其他网站说,如果我改变我的input的值为其他东西,然后回到我的原始值,TypeAhead将显示。

在我的情况下什么都不起作用。

我像这样检索我的结果:

$(element).typeahead({
    hint      : true,
    highlight : true, // This is to bold words that match the query
    minLength : queryLength
}, {
    name      : "results",
    displayKey: "value",
    source    : bhResults.ttAdapter()
});

之后,我试图强制显示一个下拉列表,其中包含所有结果,如下所示:

    window.setTimeout(function () {
        $(element).typeahead('val', value);
        $(element).focus();
        $(element).trigger("input");
        $(element).val('mood');
    }, 1000);

然而,什么都不工作!

其中一个github问题包含了一个解决方法:https://github.com/twitter/typeahead.js/issues/798

这个适合我:

( function( $ )
{
    function obtainer(query, cb) 
    {
        var filteredList = $.grep( allowed_sites, function (item, index) {
            return item.value.match(query);
        });
        var mapped = $.map( filteredList, function (item) { return item }); // data is already formatted as array of objects with with id/value keys, so return whole "row" object
        cb( mapped );
    }
    $('#my-container .typeahead').typeahead(
        {
            hint: true,
            highlight: true,
            minLength: 0
        },
        {
            name: 'my-typeahead',
            displayKey: 'value',
            source: obtainer 
        }
    ).on( 'typeahead:autocompleted typeahead:selected keyup', function(e, row) 
        {
            // do stuff with data
        } 
    ).on( 'typeahead:opened', function() 
        {
            var ev = $.Event("keydown");
            ev.keyCode = ev.which = 40;
            $(this).trigger(ev);
            return true
        } 
    );
} )( jQuery );