Solved

Jquery Autocomplete - No results Found

Posted on 2013-01-22
9
407 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Geolocation works...sometimes 2 36
Data Analysis 7 59
Cannot find JQuery answer to looping through nested objects 8 32
Difference between Highcharts and Mapbox 10 44
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

732 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