jquery autocomplete - noSuggestionNotice doesn't work

1.2k Views Asked by At

I am using this plugin: https://github.com/devbridge/jQuery-Autocomplete

I have added the following in my autocomplete function:

 showNoSuggestionNotice: true,

 noSuggestionNotice: 'No results found',

and the response I get from server side is:

 {"suggestions":[]}

However, the notice never gets shown. Other things work as expected whenever there are results in the response.

EDIT: my existing code -

  $('<selector>').autocomplete({
    serviceUrl: '<my service url>',
    minChars: 3,
    autoSelectFirst: true,
    showNoSuggestionNotice: true,
    noSuggestionNotice: 'No results found',
    formatResult: function (suggestion, currentValue) {
        <some formatting logic here>
        return resultStr;
    },
    onSelect: function (suggestion) {
        <onSelect logic here>
    }
  });
2

There are 2 best solutions below

0
On
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.0.min.js"  type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.min.js" type="text/javascript"></script>

<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/themes/blitzer/jquery-ui.css" rel="Stylesheet" type="text/css" />

<script type="text/javascript">
   $(function () {
      $("[id=txtSearch]").autocomplete({
      source: function (request, response) {
      $.ajax({
         url: 'URL',
         data: "{ 'prefix': '" + request.term + "'}",
         dataType: "json",
         type: "POST",
         contentType: "application/json; charset=utf-8",
         success: function (data) {
             if (data.d.length > 0) {
               response($.map(data.d, function (item) {
                  return {
                       label: item.split('-')[0],
                       val: item.split('-')[1]
                  };
               }))
           } else {
              response([{ label: 'No results found.', val: -1}]);
           }
       }
   });
 },
 select: function (e, u) {
     if (u.item.val == -1) {
        return false;
     }
 }
 });
 });
 </script>

 Enter search term: <input type = "text" id = "txtSearch" />
0
On

return as empty array [] will do. the noSuggestionNotice will not works if suggestion return undefined

showNoSuggestionNotice: true,
noSuggestionNotice: "No results found",
transformResult: function(response) {
    if (response){
        return {
            suggestions: $.map(response.results, function(dataItem) {
                return { value: dataItem.value, data: dataItem.id, object: dataItem };
            })
        };
    }
    else{
        return {
            suggestions: [],
        };
    }
},