Solved

Jquery Autocomplete - No results Found

Posted on 2013-01-22
9
397 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

770 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