简体   繁体   中英

Twitter typeahead.js not initializing / working as expected using remote data source

Twitter typeahead not working as expected, when I comment out code in the library I do get a non-styled drop down of suggestions.

    jQuery('input#test').typeahead(
    {
        hint: true,
        highlight: true,
        limit:25, //still using 5 which is default in the library
        minLength: 3
    },
    {
        name: 'customLookup',
        source: function(query, result) {
            return jQuery.ajax({
                url: url, //my dynamic url, returns custom json array that needs to be mapped
                data: 'shop_name=' + query + "&limit=25", //custom limit against backend api
                dataType: "json",
                type: "post",
                success: function(data, textStatus, xhr) {
                    var suggestions = [];
                    
                    jQuery.map(data.data, function(item){
                        suggestions.push(item.name + " - " + item.address);
                    });

                    result(suggestions); //stepping in, it just skips because syncCalled already = true? Then remove that code and it gives me back a list of 5 that isn't styled...

                    console.log(suggestions); //has array of strings as expected
                },
                error: function (request, status, error) {
                    alert(error);
                }
            });
        }
    });

So are there options or updates I've missed capturing when configuring? Using a back end custom data source that needs JSON mapped to an array for typeahead.

Just ended up modifying the library directly and having our own copy, not sure the issue but seems to work fine with that approach and using custom CSS to style.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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