动态填充 Twitter Bootstrap Typeahead [英] Dynamically populating Twitter Bootstrap Typeahead

查看:20
本文介绍了动态填充 Twitter Bootstrap Typeahead的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Typeahead works if I give a static array as the source, but when I try to generate the array with function it won't populate its dropdown list.

$("#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 接受这个数组?

What can I do to get typeahead to accept this array?

推荐答案

您需要 searchArtist 函数来接受 process 回调.您应该调用 process(artists) 而不是返回数组.

You need your searchArtist function to take the process callback. Instead of returning the array you should call process(artists).

这篇关于动态填充 Twitter Bootstrap Typeahead的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆