views:

876

answers:

2

Hey I'm trying to return a message when there are no results for the users current query! i know i need to tap into the keyup event, but it looks like the plugin is using it

+3  A: 

You could try supplying a parse option (function to handle data parsing) and do what you need when no results are returned to parse.

This example assumes you're getting back an array of JSON objects that contain FullName and Address attributes.

   $('#search').autocomplete( {
       dataType: "json",
       parse: function(data) {
         var array = new Array();
         if (!data || data.length == 0) {
             // handle no data case specially
         }
         else {
            for (var i = 0; i < data.length; ++i) {
               var datum = data[i];
               array[array.length] = { 
                                       data: datum,
                                       value: data.FullName + ' ' + data.Address,
                                       result: data.DisplayName
                                     };
            }
         }
         return array;
       }
   });
tvanfosson
hmmm nice idea thanks dude!
Aaron Mc Adam
Thanks a million dude, workin a charm!
Aaron Mc Adam
A: 

I can't get it working... I tried like this:

$("input#db_search_box").autocomplete({
    source: "/my_relative_path/db_search.php",
    select: function(event, ui) {
        $('#db_search_box').val(ui.item.value);
        $('#db_search_recid').val(ui.item.recid);
    return false;
    },
    parse: function(data) {
         var array = new Array();
         if (!data || data.length == 0) {
             row = ['No result. Try another search', null];
             array[0] = {
                data: row,
                value: row[0],
                result: row[0]
                }
        }
    return array;
    }
});

But I'm not getting the "No result" message in the results list. So what am I missing?

Lwangaman