Solved

Jquery Autocomplete - No results Found

Posted on 2013-01-22
9
394 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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)

947 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now