动态填充Twitter Bootstrap Typeahead

时间:2012-12-11 22:29:26

标签: javascript twitter-bootstrap

如果我将静态数组作为源,Typeahead有效,但是当我尝试使用函数生成数组时,它不会填充其下拉列表。

$("#mix-artist" ).typeahead({
        source: function(query, process) {
            Mix.searchArtist(query);
        },          
        minLength: 3,
    });

来源的功能:

searchArtist: function(query) {
    $.get(API_MAIN_URL, {a: "search.artists", q: query}, 
        function(data) {
            artists = [];
            query = query.toLowerCase();
            if (data['aData']) {
                for (var i = 0; i <= data['aData'].length; i++) {
                    if (data['aData'][i] && data['aData'][i]['sName'].toLowerCase().indexOf(query) == 0) {
                        artists.push(data['aData'][i]['sName']);
                    }
                }
            }
            console.log(artists);
            return artists;
        });
}

源函数的示例输出:

["Arcangel", "Arc Angels", "Arcade Fire", "Arctic Monkeys", "Archers of Loaf", "Architecture in Helsinki", "Archie Shepp", "Arcade", "Arch Enemy", "Arcadia", "Archie Eversole"]

我该怎么做才能让typeahead接受这个数组?

1 个答案:

答案 0 :(得分:2)

您需要searchArtist功能才能进行process回调。您应该拨打process(artists)

,而不是返回数组