【问题标题】:TypeAhead doesn't show query resultsTypeAhead 不显示查询结果
【发布时间】:2016-08-03 12:51:44
【问题描述】:

我很绝望!

我在使用 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 源代码。

【问题讨论】:

    标签: javascript php html mysql bootstrap-typeahead


    【解决方案1】:

    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 调用的成功

    【讨论】:

      【解决方案2】:

      对于 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 Examples

      【讨论】:

      • 我阅读了你所有的代码,但我不明白如何在我的打字头函数中使用它,用于 tagsinput 函数(如我的代码)..你能为我提供更多帮助吗?非常感谢..
      • 您不必使用 tagsinput 函数,只需使用我的代码中看到的 typeahead 函数并指定 api 中记录的行为。
      • 如果我将您的代码放在我的页面中,例如,我收到此错误:未定义寻血犬
      • 哦,我的错...对于这种预输入,您需要安装正确运行的 twitter-typeahead Nuget-package
      • 哦 :( 另一个我不知道的组件 :( 如果我找到它,你能帮我让它满足我的需要吗?
      猜你喜欢
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多