Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using jquery autocomplete difficulty

I am using jquery autocomplete but having dificulty to load the autocomplete in the textbox

My model is as follows:

Users = new List<string>();
foreach (var item in User.LoadSortedByName())
{
    Users.Add(item.Name+"\n");
} 

View:

<p>@Html.TextBox("user", "")
 $(function () {
           $("input#user").autocomplete('@Model.Users');
});

UPDATE - SIMPLIFIED ATTEMPT and STILL NOT WORKING

_layout

<script src="@Url.Content("~/Scripts/jquery.autocomplete.js")" type="text/javascript"></script>

View


  <p><input type="text" id="tags" /></p>

<script type="text/javascript">
    $(function () {
            var availableTags = [
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
            "C++",
            "Clojure",
            "COBOL",
            "ColdFusion",
            "Erlang",
            "Fortran",
            "Groovy",
            "Haskell",
            "Java",
            "JavaScript",
            "Lisp",
            "Perl",
            "PHP",
            "Python",
            "Ruby",
            "Scala",
            "Scheme"
        ];
        $( "#tags" ).autocomplete({
            source: availableTags
        });


    });
like image 760
learning Avatar asked Mar 18 '11 11:03

learning


People also ask

Why is jQuery autocomplete not working?

autocomplete is not a function" jQuery error occurs for multiple reasons: Forgetting to include the jQuery UI library. Loading the jQuery UI library before the jQuery library. Loading the jQuery library twice.

How does autocomplete work in jQuery?

In the process of searching a specific value, the jQuery UI autocomplete selection feature provides the user with some string suggestions for the input area which effectively saves time. The autocomplete select action is triggered when the user selects one of the options from the pre-populated list.

What is JavaScript autocomplete?

The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are tags for programming languages, give "ja" (for Java or JavaScript) a try. The datasource is a simple JavaScript array, provided to the widget using the source-option.


2 Answers

You should actually be attaching the autocomplete behaviour to your text box.

That said, the autocomplete version included in the jQuery library isn't totally straightforward if you ask me.

$("input#user").autocomplete({
    source: function (request, response) {
        // define a function to call your Action (assuming UserController)
        $.ajax({
            url: '/user/GetUsers', type: "POST", dataType: "json",

            // query will be the param used by your action method
            data: { query: request.term },
            success: function (data) {
                response($.map(data, function (item) {
                    return { label: item, value: item };
                }))
            }
        })
    },
    minLength: 1, // require at least one character from the user
}); 

In your controller, define the following Action

public ActionResult GetUsers(string query) 
{
    var users = (from u in User.LoadSortedByName()
                where u.Name.StartsWith(query)
                orderby u.Name // optional but it'll look nicer
                select u.Name).Distinct().ToArray();

    return Json(users);
}

This implementation will not allow multiple values on your textbox; for examples on how to do that check the jQuery autocomplete examples

UPDATE based on final resolution

Make sure you have a reference to the jQuery UI Core.

From the Microsoft CDN:

<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.11/jquery-ui.min.js" type="text/javascript"></script>`

From the Google CDN:

<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js" type="text/javascript"></script>

Or download it yourself from the official jQuery UI Page

like image 163
Sergi Papaseit Avatar answered Nov 02 '22 13:11

Sergi Papaseit


I developed an html extension in the form of plugin, to use the jquery ui autocomplete but in a very simple and dynamic. the syntax was something like this here

     [email protected](model => model.Id, x => x.Code , "List")

I left all the source code available (suggestions are welcome) and a zip file containing all the necessary files. Hope that helps!

Project URL http://autocompletemvc.codeplex.com/releases/view/70140

Bin files http://autocompletemvc.codeplex.com/releases/70140/download/259741

like image 26
Paulo Avatar answered Nov 02 '22 13:11

Paulo