简体   繁体   English

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

[英]How to show “No Results Found” with typeahead.js?

I am trying to show results not found message when there's no suggestion from user input. 当没有来自用户输入的建议时,我试图显示results not found消息。 typeahead.js shows input suggestion while giving input to text field..if there no suggestion found then how to show results not found message?.. version is typeahead.js 0.11.1 typeahead.js显示输入建议,同时给文本字段输入..如果没有找到建议,那么如何显示results not found消息?.. version is typeahead.js 0.11.1

You can check if no result with empty parameter: 您可以使用empty参数检查是否没有结果:

I have edited above code a little bit so that it may help to others searching the same thing. 我已经编辑了上面的代码,以便它可以帮助其他人搜索相同的东西。

$('.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');
        }
    }

I have the same issue. 我有同样的问题。 But i'm using ajax for my source not adapter. 但我使用ajax作为我的源而不是适配器。
You can try adding popover if suggestions length is 0. 如果建议长度为0,您可以尝试添加popover。

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;
        }
    });
}

I tried showing this " no results found " warning using bootstrap-popover . 我尝试使用bootstrap-popover显示这个“ 找不到结果 ”的警告。 I know its not a good one to try but i just shared my way to achieve this if i had the same issue. 我知道它不是一个好的尝试,但我只是分享了我的方式来达到这个,如果我有同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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