Solved

Jquery UI autocomplete not narrowing results...

Posted on 2011-02-21
4
870 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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
How to get CSS styled menu to maintain the open selected menu? 13 45
Html split(text) Part2 6 21
Json and ajax 1 20
Getting selected value using Jquery 3 20
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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 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…

749 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