Solved

Jquery UI autocomplete not narrowing results...

Posted on 2011-02-21
4
858 Views
Last Modified: 2012-06-27
I have a JQuery UI that's connecting to a WCF rest service to search customers. It's connecting and pulling down the data and mapping it correctly. The problem is that it's not narrowing the results as I type. No matter what I put in the textbox, it comes back with the same 3 results.

My guess is that there's something I'm implementing wrong and it's a simple "just add option ____" (hopefully) or that I need to go back and make a much more robust WCF service to feed the data (most likely but I really hope not).

Any thoughts?

JQuery Code:
$(document).ready(function () {
            $("#txtCensus").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: "API/CensusSearch.svc/696/Search",
                        success: function (data) {
                            response($.map(data, function (item) {
                                return {
                                    label: item.FirstName + " " + item.LastName + "(SSN: " + item.SSN + ")",
                                    value: item.FirstName + " " + item.LastName + "(SSN: " + item.SSN + ")",
                                    desc: item.PID
                                }
                            }));
                        }
                    });
                },
                minLength: 2,
                select: function (event, ui) {
                    $("#lblPID").text(ui.item.desc);
                },
                open: function () {
                    $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
                },
                close: function () {
                    $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
                }
            });


        });

Open in new window


JSON returned from web service:
[
    {
        "FirstName": "Steve",
        "LastName": "Jobs",
        "PID": "123454325",
        "SSN": "1234"
    },
    {
        "FirstName": "Slappy",
        "LastName": "Kincade",
        "PID": "4542345",
        "SSN": "5432"
    },
    {
        "FirstName": "Carlos",
        "LastName": "Spicyweiner",
        "PID": "4374672",
        "SSN": "6789"
    }
]

Open in new window

0
Comment
Question by:fredstov
  • 3
4 Comments
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 34956051
What is the "response" function you've used?

                            response($.map(data, function (item) {
                                return {
                                    label: item.FirstName + " " + item.LastName + "(SSN: " + item.SSN + ")",
                                    value: item.FirstName + " " + item.LastName + "(SSN: " + item.SSN + ")",
                                    desc: item.PID
                                }
                            }));

Open in new window

0
 

Author Comment

by:fredstov
ID: 34959866
That was something that was shown on the official jqueryui example page that lets you customize the display and data of the returned jquery.
0
 

Accepted Solution

by:
fredstov earned 0 total points
ID: 34960650
Was able to fix it on my own. Near as I can tell, if you're going to use Ajax as the source, then it needs a search routine that narrows it down on the server side. If you want jquery to handle the narrowing down then you need a static json set.

With a slight altering of the code, it works just fine now:
$(document).ready(function () {
            $("#txtCensus").attr("disabled", "disabled");
            var jsonCensus = {};

            $.ajax({
                url: "API/CensusSearch.svc/696/Search",
                dataType: "json",
                success: function (data) {
                    jsonCensus = $.map(data, function (item) {
                        return {
                            label: item.FirstName + " " + item.LastName + " (SSN: " + item.SSN + ")",
                            value: item.FirstName + " " + item.LastName + " (SSN: " + item.SSN + ")",
                            desc: item.PID
                        }
                    });

                    makeCensusSearch(jsonCensus);
                }
            });

        });

        function makeCensusSearch(jsonSource) {
            $("#txtCensus").removeAttr("disabled");
            $("#txtCensus").autocomplete({
                source: jsonSource,
                minLength: 2,
                select: function (event, ui) {
                    $("#lblPID").text(ui.item.desc);
                },
                open: function () {
                    $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
                },
                close: function () {
                    $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
                }
            });

        }

Open in new window

0
 

Author Closing Comment

by:fredstov
ID: 34995332
No one replied and, after fiddling with it for a couple days I was able to figure it out. I have posted my solution to help others.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

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