Bootstrap 3 + typeahead.js 0.9.3. Prompt if no results found
Asked Answered
B

2

5

Now that Bootstrap has dropped typeahead, they are recommending the native twitter typeahead (0.9.3 at the time of this writing)

I am having trouble finding examples of how to prompt the user when there are no results found.

In the native bootstrap, you could do this : http://bootply.com/61459

Perhaps this functionality is not possible?

Bedwarmer answered 15/8, 2013 at 20:47 Comment(0)
A
11

How about something like this?

  $(document).ready(function() {
    $("#search").typeahead([
      {
        limit: 10,
        remote: {
          url: "//my.tld/gimmesomejson.php?searchuser=%QUERY",
          filter: function(parsedResponse) {
            var dataset = [];
            for (i = 0; i < parsedResponse.length; i++) {
              dataset.push({
                value: parsedResponse[i].value,
                tokens: parsedResponse[i].tokens
              });
            }
            if (parsedResponse.length == 0) {
              dataset.push({
                value: "No results" 
              });
            }
            return dataset;
          },
        },
        template: '<p>{{value}}</p>',
        engine: Hogan
      }
    ]);
  });
Around answered 16/8, 2013 at 12:19 Comment(0)
L
0

I tried this and its working well for me.
Here i'm showing the "no results found" using bootstrap popover.

 $('#facility_names'+id).typeahead({
        items: "all",
        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;
        }
    });
Lastly answered 8/1, 2018 at 13:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.