Detecting no results on jQuery UI autocomplete
Asked Answered
R

6

89

Before you point me to them, yes, I have reviewed the half dozen posts on this topic, but I am still stymied as to why this doesn't work.

My goal is to detect when the autocomplete yields 0 results. Here's the code:

 $.ajax({
   url:'sample_list.foo2',
   type: 'get',
   success: function(data, textStatus, XMLHttpRequest) {
      var suggestions=data.split(",");

  $("#entitySearch").autocomplete({ 
    source: suggestions,
    minLength: 3,
    select: function(e, ui) {  
     entityAdd(ui.item.value);
     },
    open: function(e, ui) { 
     console.log($(".ui-autocomplete li").size());
     },
    search: function(e,ui) {
     console.log("search returned: " + $(".ui-autocomplete li").size());

    },
    close: function(e,ui) {  
     console.log("on close" +  $(".ui-autocomplete li").size());    
     $("#entitySearch").val("");
    }
   }); 

  $("#entitySearch").autocomplete("result", function(event, data) {

   if (!data) { alert('nothing found!'); }

  })
 }
}); 

The search itself works fine, I can get results to appear without a problem. As I understand it, I should be able to intercept the results with the autocomplete("result") handler. In this case, it never fires at all. (Even a generic alert or console.log that doesn't reference the number of results never fires). The open event handler shows the correct number of results (when there are results), and the search and close event handlers report a result size that is always one step behind.

I feel like I'm missing something obvious and glaring here but I just don't see it.

Reside answered 17/1, 2011 at 23:21 Comment(1)
It looks like there isn't an easy way to accomplish this with an autocomplete widget driven by client-side data. Is using a remote source for the widget an option?Sporangium
S
201

jQueryUI 1.9

jQueryUI 1.9 has blessed the autocomplete widget with the response event, which we can leverage to detect if no results were returned:

Triggered after a search completes, before the menu is shown. Useful for local manipulation of suggestion data, where a custom source option callback is not required. This event is always triggered when a search completes, even if the menu will not be shown because there are no results or the Autocomplete is disabled.

So, with that in mind, the hacking we had to do in jQueryUI 1.8 is replaced with:

$(function() {
    $("input").autocomplete({
        source: /* */,
        response: function(event, ui) {
            // ui.content is the array that's about to be sent to the response callback.
            if (ui.content.length === 0) {
                $("#empty-message").text("No results found");
            } else {
                $("#empty-message").empty();
            }
        }
    });
});​

Example: http://jsfiddle.net/andrewwhitaker/x5q6Q/


jQueryUI 1.8

I couldn't find a straightforward way to do this with the jQueryUI API, however, you could replace the autocomplete._response function with your own, and then call the default jQueryUI function (updated to extend the autocomplete's prototype object):

var __response = $.ui.autocomplete.prototype._response;
$.ui.autocomplete.prototype._response = function(content) {
    __response.apply(this, [content]);
    this.element.trigger("autocompletesearchcomplete", [content]);
};

And then bind an event handler to the autocompletesearchcomplete event (contents is the result of the search, an array):

$("input").bind("autocompletesearchcomplete", function(event, contents) {
    $("#results").html(contents.length);
});

What's going on here is that you're saving autocomplete's response function to a variable (__response) and then using apply to call it again. I can't imagine any ill-effects from this method since you're calling the default method. Since we're modifying the object's prototype, this will work for all autocomplete widgets.

Here's a working example: http://jsfiddle.net/andrewwhitaker/VEhyV/

My example uses a local array as a data source, but I don't think that should matter.


Update: You could also wrap the new functionality in its own widget, extending the default autocomplete functionality:

$.widget("ui.customautocomplete", $.extend({}, $.ui.autocomplete.prototype, {

  _response: function(contents){
      $.ui.autocomplete.prototype._response.apply(this, arguments);
      $(this.element).trigger("autocompletesearchcomplete", [contents]);
  }
}));

Changing your call from .autocomplete({...}); to:

$("input").customautocomplete({..});

And then bind to the custom autocompletesearchcomplete event later:

$("input").bind("autocompletesearchcomplete", function(event, contents) {
    $("#results").html(contents.length);
});

See an example here: http://jsfiddle.net/andrewwhitaker/VBTGJ/


Since this question/answer has gotten some attention, I thought I'd update this answer with yet another way to accomplish this. This method is most useful when you have only one autocomplete widget on the page. This way of doing it can be applied to an autocomplete widget that uses a remote or local source:

var src = [...];

$("#auto").autocomplete({
    source: function (request, response) {
        var results = $.ui.autocomplete.filter(src, request.term);

        if (!results.length) {
            $("#no-results").text("No results found!");
        } else {
            $("#no-results").empty();
        }

        response(results);
    }
});

Inside the if is where you would place your custom logic to execute when no results are detected.

Example: http://jsfiddle.net/qz29K/

If you are using a remote data source, say something like this:

$("#auto").autocomplete({
    source: "my_remote_src"
});

Then you'll need to change your code so that you make the AJAX call yourself and can detect when 0 results come back:

$("#auto").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "my_remote_src", 
            data: request,
            success: function (data) {
                response(data);
                if (data.length === 0) {
                    // Do logic for empty result.
                }
            },
            error: function () {
                response([]);
            }
        });
    }
});
Sporangium answered 18/1, 2011 at 2:3 Comment(14)
@Andrew, any idea how I can access the elements in "contents" array using jQuery???Gravettian
@Bongs: You should be able to access it directly by index contents[0]Sporangium
Actually the thing is the contents array was populated with username and its image, and was not able to access it by specifying the index value. But figured out the solution. Had to mention like, contents[i].user.username... :) thanks for the reply and the awesome solution...Gravettian
The above solution also works great for PrimeFaces autocomplete (2.2.x) which is based on the same jQuery plugin.Nolitta
The one problem I'm having with the last code part of this answer is using jQuery(this).siblings('myselector').show(); Since this is an Object and not an array of applicable tags. Ugh, i just read the part about ONE autocomplete widget... Andrew, friend, what method would work when you have 'X' widgets on a page, due to a dynamic 'add more fields' button on a page?Urbina
gist.github.com/9b6634b1b673022bab85 is the code i have and all of the fields show when triggered, yet using this doesn't work or more accurately I don't know enough to make it work. I'm sure it's me, but i only want this widget to show the fields, not this field in all widgets. (missed edit window... oops).Urbina
@pjammer: I'd like to help, but It's probably worth opening a new question :)Sporangium
@AndrewWhitaker #10818988 and thanks! You are so right it needs it's own question!Urbina
In JqueryUI 1.8.19, the _response function was renamed to __response. ( goo.gl/zAl88 ). So, $.ui.autocomplete.prototype._response becomes $.ui.autocomplete.prototype.__responseTeutonism
Exactly like crazyphoton said, the _response function was renamed to __response here is an updated example jsfiddle.net/UfENR Thanks a lot Andrew Whitaker for your solution! You rock.Kwangtung
@AdanArchila: I haven't updated the answer yet because jQueryUI 1.9 hasn't officially been released yet.Sporangium
This is a great solution but I notice one minor issue. If you leave the autocomplete to complete other elements of the form the error label remains.Geanine
@haymansfield: You could add a blur event handler to clear the text.Sporangium
@AndrewWhitaker any idea on this question, its similar to this #30591452Rameses
G
2

If you are using a remote data source (like a MySQL database, PHP, or whatever on the server side) there are a couple of other cleaner ways to handle a situation when there's no data to return to the client (without the need for any hacks or core code UI code changes).

I use PHP and MySQL as my remote data source and JSON to pass information between them. In my case I seemed to get jQuery exception errors if the JSON request did not get some sort of response from the server, so I found it easier to just return an empty JSON response from the server side when there's no data and then handle the client response from there:

if (preg_match("/^[a-zA-Z0-9_]*$/", $_GET['callback'])) {//sanitize callback name
    $callback = $_GET['callback'];
} else { die(); }

die($callback . "([])");

Another way would be to return a flag in the response from the server to indicate that there's no matching data and perform actions client side based on the presence (and or value) of the flag in the response. In this case the servers response would be something like:

die($callback . "([{'nodata':true}])");

Then based on this flag actions can be performed client side:

$.getJSON('response.php?callback=?', request, function (response) {
    if (typeof response[0].nodata !== 'undefined' && response[0].nodata === true) {
        alert('No data to display!');
    } else {
        //Do whatever needs to be done in the event that there is actually data to display.
    }
});
Gayden answered 30/1, 2012 at 23:50 Comment(0)
Z
2

After initializing your autocomplete element, set the messages option if you wanna use the default spans for message indication:

$(<yourselector>).autocomplete('option', 'messages', {
    noResults: 'myKewlMessage',
    results: function( amount ) {
        return amount + ( amount > 1 ? " results were" : " result was" ) + " found.";
    }
});

NOTE: This is an experimental API (not documented). jQuery UI developers are still investigating a full solution for string manipulation and internationalization.

Zack answered 27/5, 2014 at 8:39 Comment(0)
S
0

After hours playing I finally found a trick to display No match found in jQuery autocomplete. Look at the above code and simply add a div, in my case #ulNoMatch and its style set to displap:none. In the callback success method check if the array returned has length == 0. If it is there you go, you made your day! :)

<pre><div class="ui-widget1" style="width: auto;">
    <asp:TextBox ID="txtSearch" class="tb" runat="server" Width="150px">
    </asp:TextBox>
    <ul id="ulNoMatch" class="ui-autocomplete ui-menu ui-widget1 ui-widget1-content ui-corner-all"
        role="listbox" aria-activedescendant="ui-active-menuitem" style="z-index: 16;
        display: none; width: 150px;">
        <li class="ui-menu-item" role="menuitem"><a class="ui-corner-all" tabindex="-1">No Matches
            Found</a></li>
    </ul>
    </div><pre>
<b>
<b>

Enter code here

<script>
    $(function () {
        $("input[id$='txtSearch']").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "splah.aspx/GetByName",
                    data: "{ 'strName': '" + request.term.trim() + "' }",
                    dataType: "json",
                    type: "POST",
                    //cacheLength: 1,
                    contentType: "application/json; charset=utf-8",
                    dataFilter: function (data) {
                        return data; },
                    success: function (data) {
                        var found = $.map(data.d, function (item) {
                            return {
                                value: item.Name,
                                id: item.id
                            }
                         });

                         if (found.length == 0)
                         {
                             $("#ulNoMatch").show();
                         }
                         else
                         {
                             $("#ulNoMatch").hide();
                         }
                         response(found);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus);
                    }
                });
            },
            select: function (event, ui) {
                $("input[id$='txtSearch']").val(ui.item.label);
                $("input[id$='txtID']").val(ui.item.id);
                return false;
            },
            minLength: 1
        });
    });
</script>
Sitton answered 29/5, 2012 at 14:17 Comment(0)
D
0
The easiest straight forward way to do it.

$("#search-box").autocomplete({
                    minLength: 2,
                    source:function (request, response) {
                        $.ajax({
                            url: urlPref + "/Api/SearchItems",
                            data: {
                                term: request.term
                            },
                            success: function (data) {
                                if (data.length == 0) {
                                    data.push({
                                        Id: 0,
                                        Title: "No results found"
                                    });
                                }
                                response(data);
                            }
                            });
                        },
Depurative answered 9/1, 2014 at 5:21 Comment(1)
This answer does not contribute anything new, accepted answer has the same code.Banna
D
-1
function SearchText() {
 $(".autosuggest").autocomplete({
   source: function (request, response) {
    $.ajax({
     type: "POST",
     contentType: "application/json; charset=utf-8",
      url: "Default.aspx/GetAutoCompleteData",
      data: "{'username':'" + document.getElementById('txtSearch').value + "'}",
        dataType: "json",
        success: function (data.d) {
        if ((data.d).length == 0) {
         alert("no result found");
          }
           response(data.d);
         },
         error: function (result) {
              alert("Error");
         }
         });
        }
     });
  }
Dampier answered 27/9, 2013 at 11:34 Comment(1)
This answer does not contribute anything new, accepted answer has the same code.Banna

© 2022 - 2024 — McMap. All rights reserved.