?
Solved

Jquery UI autocomplete not narrowing results...

Posted on 2011-02-21
4
Medium Priority
?
886 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

718 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