Solved

Jquery UI autocomplete not narrowing results...

Posted on 2011-02-21
4
884 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
[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
  • 3
4 Comments
 
LVL 43

Expert Comment

by:Rob
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

628 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