Solved

Jquery Autocomplete - No results Found

Posted on 2013-01-22
9
404 Views
Last Modified: 2013-01-22
If no results found in my jquery autocomplete I want to display "no results found" in the textbox, I am using JSON, I have tried various solutions but can't get them to work. here is my script:

<script type="text/javascript">
               $(document).ready(function () {
                   $("#<%=txtQtOccupation.ClientID%>").autocomplete({
                       source: function (request, response) {
                           $.ajax({
                               url: '<%=ResolveUrl("~/Service.asmx/GetOccupationDescription") %>',
                               data: "{ 'prefix': '" + request.term + "'}",
                               dataType: "json",
                               type: "POST",
                               contentType: "application/json; charset=utf-8",
                               success: function (data) {
                                   response($.map(data.d, function (item) {
                                       return {
                                           label: item.split('|')[0],
                                           val: item.split('|')[1]

                                       }
                                   }))
                               },
                               error: function (response) {
                                   alert(response.responseText);
                               },
                               failure: function (response) {
                                   alert(response.responseText);
                               }
                           });
                       },
                       select: function (event, i) {
                           $("#<%=hdnQtOccupation.ClientID %>").val(i.item.val);
                           //cancel the event to prevent it populating the value
                           //event: preventDefault();
                       },
                       minLength: 3
                   });
               });
            </script>
0
Comment
Question by:TCBob
  • 5
  • 4
9 Comments
 
LVL 10

Expert Comment

by:gavsmith
ID: 38804995
Have you tried using the response event?

<script type="text/javascript">
               $(document).ready(function () {
                   $("#<%=txtQtOccupation.ClientID%>").autocomplete({
                       source: function (request, response) {
                           $.ajax({
                               url: '<%=ResolveUrl("~/Service.asmx/GetOccupationDescription") %>',
                               data: "{ 'prefix': '" + request.term + "'}",
                               dataType: "json",
                               type: "POST",
                               contentType: "application/json; charset=utf-8",
                               success: function (data) {
                                   response($.map(data.d, function (item) {
                                       return {
                                           label: item.split('|')[0],
                                           val: item.split('|')[1]

                                       }
                                   }))
                               },
                               error: function (response) {
                                   alert(response.responseText);
                               },
                               failure: function (response) {
                                   alert(response.responseText);
                               }
                           });
                       },
                       select: function (event, i) {
                           $("#<%=hdnQtOccupation.ClientID %>").val(i.item.val);
                           //cancel the event to prevent it populating the value
                           //event: preventDefault();
                       },
                       minLength: 3,
                       response: function(event, ui) {
                              if (ui.content.length === 0) {
                                       $("#<%=txtQtOccupation.ClientID%>").text("No results found");
                              } else {
                                       $("#<%=txtQtOccupation.ClientID%>").empty();
                              }
                       }               
                   });
               }); 
            </script>

Open in new window

0
 
LVL 10

Expert Comment

by:gavsmith
ID: 38805011
Hold on that won't work, you may have to append an 'No results found' to the selection list to get it work correctly. Give me a second I'll give an example.
0
 

Author Comment

by:TCBob
ID: 38805017
ok thanks for your feedback
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:TCBob
ID: 38805165
I tried adding a if clause into the success function


          <script type="text/javascript">
              $(document).ready(function () {
                  $("#<%=txtQtOccupation.ClientID%>").autocomplete({
                      source: function (request, response) {
                          $.ajax({
                              url: '<%=ResolveUrl("~/Service.asmx/GetOccupationDescription") %>',
                              data: "{ 'prefix': '" + request.term + "'}",
                              dataType: "json",
                              type: "POST",
                              contentType: "application/json; charset=utf-8",
                              success: function (data, status, xhr) {
                                  if (!data.d.length) {
                                      var result = [
                            {
                                label: 'No matches found'
                            }
                             ];
                                      response(result);
                                  }
                                  else {
                                      response($.map(data.d, function (item) {
                                          return {
                                              label: item.split('|')[0],
                                              val: item.split('|')[1]

                                          }
                                      }))
                                  }
                              },
                              error: function (response) {
                                  alert(response.responseText);
                              },
                              failure: function (response) {
                                  alert(response.responseText);
                              }
                          });
                      },
                      select: function (event, i) {
                          $("#<%=hdnQtOccupation.ClientID %>").val(i.item.val);
                          //cancel the event to prevent it populating the value
                          //event: preventDefault();
                      },
                      minLength: 3
                  });
              });
            </script>

which is now working, although more testing to follow
0
 

Author Comment

by:TCBob
ID: 38805173
I now need to stop 'No matches found' being selected in the textbox
0
 
LVL 10

Expert Comment

by:gavsmith
ID: 38805225
you should be able to do that in your select event something like:

                   select: function (event, i) {
                           if ($(this).val() == "No matches found") return false;
                          $("#<%=hdnQtOccupation.ClientID %>").val(i.item.val);
                          //cancel the event to prevent it populating the value
                          //event: preventDefault();
                      },
0
 
LVL 10

Accepted Solution

by:
gavsmith earned 500 total points
ID: 38805353
correction of the above code:

                select: function (event, i) {
                           if (i.item.value == "No matches found"){
                               $(this).val("");
                               return false;
                          }
                          $("#<%=hdnQtOccupation.ClientID %>").val(i.item.val);
                          //cancel the event to prevent it populating the value
                          //event: preventDefault();
                      },
0
 

Author Comment

by:TCBob
ID: 38805645
thanks for select function update that worked script is:

<script type="text/javascript">
              $(document).ready(function () {
                  $("#<%=txtQtOccupation.ClientID%>").autocomplete({
                      source: function (request, response) {
                          $.ajax({
                              url: '<%=ResolveUrl("~/Service.asmx/GetOccupationDescription") %>',
                              data: "{ 'prefix': '" + request.term + "'}",
                              dataType: "json",
                              type: "POST",
                              contentType: "application/json; charset=utf-8",
                              success: function (data, status, xhr) {
                                  if (!data.d.length) {
                                      var result = [
                                {
                                    label: 'No matches found'
                                }
                                    ];
                                      response(result);
                                  }
                                  else {
                                      response($.map(data.d, function (item) {
                                          return {
                                              label: item.split('|')[0],
                                              val: item.split('|')[1]

                                          }
                                      }))
                                  }
                              },
                              error: function (response) {
                                  alert(response.responseText);
                              },
                              failure: function (response) {
                                  alert(response.responseText);
                              }
                          });
                      },
                      select: function (event, i) {
                          if (i.item.value == "No matches found") {
                              $(this).val("");
                              return false;
                          }
                          $("#<%=hdnQtOccupation.ClientID %>").val(i.item.val);
                      },
                      minLength: 3
                  });
              });
            </script>
0
 

Author Closing Comment

by:TCBob
ID: 38805648
thanks for your help gavsmith
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question