TypeAhead没有't显示查询结果

TypeAhead doesn't show query results

本文关键字:显示 查询 结果 没有 TypeAhead      更新时间:2023-09-26

我非常绝望!

我对Bootstrap Typeahead有这个问题。

HTML标记:

<div class="form-group">
    <label for="">Recipients</label>
    <input id="recipients" name="recipients" 
        autocomplete="off" 
        class="form-control" 
        data-role="tagsinput">
</div>

JavaScript:

$('#recipientsContainer').tagsinput({
            allowDuplicates: false,
            trimValue: true,
            confirmKeys: [13, 44],
            typeahead: {
                hint: true,
                highlight: true,
                minLength: 4,
                limit: 10,
                source: function (query) {
                    $.ajax({
                        url: "/app/route",
                        type: 'POST',
                        dataType: 'JSON',
                        data: {'query' : query},
                        success: function(data) {
                            console.log(data);
                            return(data);
                        }
                    });
                },
                afterSelect: function() {
                    this.$element[0].value = '';
                }
            }
        });

Ajax调用后,我在控制台中得到了这个数组:

["Justin Demetria +393281893574", "Dylan Alea +393700488191", "Zahir Tatyana +393007841301", "Ryan Veda +393542236060", "Hunter Wanda +393393156943"]

问题是:我什么都没看到:(什么都没出现。打字不起作用。

在控制台中,我得到这个错误

bootstrap-tagsinput.js Uncaught TypeError: Cannot read property 'success' of undefined.

我该怎么修?

我使用的是Bootstrap 3,最后一个Typeahead js源代码。

James,现在我的ajax调用是:
$('#recipientsContainer').tagsinput({
            allowDuplicates: false,
            trimValue: true,
            confirmKeys: [13, 44],
            typeahead: {
                source: function(queryForHints) {
                    if (queryForHints.length < 4)
                        return '';
                    var parameters = {'queryForHints': queryForHints};
                    jQuery.ajax({
                        url: "/app/route",
                        data: parameters,
                        type: "POST",
                        dataType: "json",
                        success: function(data) {
                            var sourceData = [];
                            for (var i = 0; i < data.length; i++) {
                                sourceData.push(data[i].text);
                            }
                            return (sourceData);
                        },
                        error: function(data) {
                            console.log("error for xxxxx/xxxxx");
                        },
                        async: true
                    });
                }
            }
        });

但错误依然存在:

bootstrap-tagsinput.js:331 Uncaught TypeError: Cannot read property 'success' of undefined

错误出现在bootstrap-tagsinput.js的第331行,我发现:

if ($.isFunction(data.success)) {
              // support for Angular callbacks
              data.success(processItems);

我们能解决这个问题吗?

在我看来,问题是输入标记无法理解ajax调用

的SUCCESS

对于typeahead,使用Bloodhound获取数据,而不是ajax。

你的代码应该像这样:

var customers = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            remote: {
                url: '/api/customers?query=%QUERY',
                wildcard: '%QUERY'
            }
        });
        $('#customer').typeahead({
            minLength: 3,
            highlight: true
        }, {
            name: 'customers',
            display: 'name',
            source: customers
        }).on("typeahead:select", function(e, customer) {
            vm.customerId = customer.id;
        });

有关Bloodhound的更多信息:Bloodhound@github

这里有一些typeahead的例子:typeahead例子