如何使用typeahead.js显示“未找到结果”?

4

我正在尝试在用户未输入建议时显示“未找到结果”的消息。 typeahead.js 在向文本字段输入内容时会显示输入建议.. 如果没有找到建议,那么如何显示“未找到结果”消息?版本是typeahead.js 0.11.1

2个回答

8
你可以使用empty参数来检查是否没有结果:
我已经稍微修改了上面的代码,这样它可能会帮助其他搜索相同内容的人。
$('.typeahead').typeahead({
    hint: false,
    highlight: true,
    minLength: 3,
},
{
    name: 'firstnames',
    displayKey: 'value',
    source: firstnames.ttAdapter(), // this is your result variable
    templates: {
        empty: function(context){
        //  console.log(1) // put here your code when result not found
          $(".tt-dataset").text('No Results Found');
        }
    }

0

我有同样的问题。但是我使用的是ajax而不是适配器。
如果建议长度为0,您可以尝试添加弹出窗口。

function BindControls_facility(facility_names,facility_details,id) {
    var timeout;
    $('#facility_names'+id).typeahead({
        items: "all",
        // source: facility_names,
         source : function (query, result) {
              if (timeout) {
                    clearTimeout(timeout);
                }

                timeout = setTimeout(function() {
                        $.ajax({
                    url:  master_url + "/facility_name_dropdown_list",
                    method: 'POST',
                    xhrFields: {
                        withCredentials: false
                    },
                    data: { input_query : query},
                    success: function (data) {
                        if(Object.keys(data.facility_name).length > 0){
                            // $("#facility_names"+id).popover('destroy');
                             result($.map(data.facility_name, function (item) {
                                return item;
                            }));
                        }
                        else{
                             $('#facility_names'+id).popover({container: '#botdiv'+id,placement: 'top'}).popover('show');
                             $('#facility_names'+id).attr('data-content','No result found for \"'+$("#facility_names"+id).val()+'\"').data('bs.popover').setContent();
                             setTimeout(function () {
                                 $('#facility_names'+id).popover('destroy');
                             }, 2000);
                         }

                    }
                });
                }, 300);

        },
        hint: true,
        highlight: true,
        cache: true,
        compression: true,
        minLength: 3,
        updater: function(item) {
             var details = "";
              $.ajax({
                url:  master_url + "/get_facility_name",
                method: 'POST',
                xhrFields: {
                    withCredentials: false
                },
                data: { facility_name : item},
                success: function (data) {
                    console.log(data.status);
                }
            });
            return item;
        }
    });
}

我尝试使用 bootstrap-popover 显示 "没有找到结果" 的警告。我知道这不是一个好的尝试,但如果我遇到同样的问题,我只是分享了我的解决方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接