Jquery Autocomplete - no result message
Asked Answered
M

2

5

I would like the autocomplete to display "no results" in it's drop down list if no result are found.

My situation is like the JQuery default example.

$(function() {
    var availableTags = [
        "ActionScript",
        "AppleScript",
        "Asp",
        "BASIC",
        "C",
        "C++",
        "Clojure",
        "COBOL",
        "ColdFusion",
        "Erlang",
        "Fortran",
        "Groovy",
        "Haskell",
        "Java",
        "JavaScript",
        "Lisp",
        "Perl",
        "PHP",
        "Python",
        "Ruby",
        "Scala",
        "Scheme"
        ];
    $( "#tags" ).autocomplete({
        source: availableTags
    });
});

Thank you for your help.

Mallee answered 29/12, 2011 at 1:43 Comment(2)
possible duplicate of Detecting no results on jQuery UI autocompleteLackluster
Actually, this one is slightly different than the one I linked as a duplicate. Please disregard my close vote.Lackluster
L
10

Here's one way you could accomplish this:

$(function() {
    var availableTags = [ /* snip */];  
    var NoResultsLabel = "No Results";

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

            if (!results.length) {
                results = [NoResultsLabel];
            }

            response(results);
        },
        select: function (event, ui) {
            if (ui.item.label === NoResultsLabel) {
                event.preventDefault();
            }
        },
        focus: function (event, ui) {
            if (ui.item.label === NoResultsLabel) {
                event.preventDefault();
            }
        }
    });
});

Basically, you need to provide a function reference as the source to the autocomplete. Inside of that function, you can use the same utility function ($.ui.autocomplete.filter) to filter down the results. Then you can see if the results array is empty. If it is, you can add a default message to the results list.

The other two options I've specified prevent the No Results option from being selected or focused.

Example: http://jsfiddle.net/er6LF/

Lackluster answered 29/12, 2011 at 2:41 Comment(1)
Perfect! Thank you Andrew. I can now go home.Mallee
T
0

This fiddle has a working is a functional example for you have: http://jsfiddle.net/andrewodri/wAg4g/

I changed this:

$("#tags").autocomplete({source: availableTags});

To this:

$("#tags").autocomplete(availableTags);

You can see it's running on the latest version of jQuery, and has the plugin linked on under "Manage Resources" taken from: http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/.

Update: The code above works if you happen to be using the plugin referenced... If not, it seems to work as is :) Please note that I did add the code within $(document).ready();, which may have been preventing if from working. Please see this forked fiddle: http://jsfiddle.net/andrewodri/VLKwe/

Hope that helps!

Transpierce answered 29/12, 2011 at 1:53 Comment(3)
BTW, additional documentation on this plugin is available here: docs.jquery.com/Plugins/AutocompleteTranspierce
The OP is not using this plugin, he/she is using jQueryUI autocomplete (jqueryui.com/demos/autocomplete)Lackluster
Ahh, my bad! Check out this forked fiddle then: jsfiddle.net/andrewodri/VLKwe. Post updated :)Transpierce

© 2022 - 2024 — McMap. All rights reserved.