Solved

Jquery Autocomplete - No results Found

Posted on 2013-01-22
9
391 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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 …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

757 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

19 Experts available now in Live!

Get 1:1 Help Now