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

时间:2014-06-03 08:15:32

标签: jquery 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);

但是,什么都没有用!

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

这对我有用:

( 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 );
相关问题