Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autocomplete using jQuery callback (label/value pair)

I am trying to implement autocomplete jQuery, but am not understanding the autocomplete function that jQuery UI provides.

It uses a callback function and gets the response as a label/value pair. I have some sample code where I am trying to pass an arbitrary label/value pair back and display that option but it isn't working. If someone can help me out with that or show me a simple program it will be great.

http://jsfiddle.net/kB25J/

HTML:

<html>
    <body>
      Please enter your country name
      <input id ="book" type="text" value="">
    </body>
</html>​

JavaScript:

$("#book").autocomplete({
    source: function(request, response) {
        alert(request.term);
        response(function() {
            return {
                label: "hi",
                value: "bye"
            }
        });
        alert(reponse);
    }
});

​ Thank You

like image 941
Lakshmikanthan Vijayaraghavan Avatar asked Sep 19 '12 14:09

Lakshmikanthan Vijayaraghavan


4 Answers

When sending response, pass an array instead of function.

$(function() {
    $("#book").autocomplete({        
        source: function(request, response) {
            var data = [{
                    label: "hi",
                    value: "bye"
                    }];
            response(data);
        },
        select: function( event, ui ) {
            $( "#book" ).val( ui.item.label); //ui.item is your object from the array
            return false;
        }
    });
});​
like image 166
prashanth Avatar answered Nov 24 '22 04:11

prashanth


You should be returning an array in your source even if its just 1 match in this case 'hi'/'bye'

As seen in this example on jqueryui.com

response( $.map( data.geonames, function( item ) {
    return {
        label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
        value: item.name
    }
}));

Its returning an array of key/value pairs for label/value using a map on the remote response.

If your source is just a list of countries you can just use an array of strings as your source.

var countries = ["USA", "Canada", "Mexico", ...]

$('input').autocomplete({
    source : countries
});

If you are working with a remote source:

$('input').autocomplete({
    source : function (request, response) {
        $.ajax({
            url: "url_to_get_countries",
            dataType: "json",
            success: function( data ) {
            response( $.map( data.countries, function( item ) {
            return {
                label: item.country_name,
                value: item.country_id
            }
            }));
            }
        });
    }
});
like image 37
Mark Avatar answered Nov 24 '22 03:11

Mark


@LakshmikanthanVijayaraghavan

As you can see at Autocomplete guide, there are three ways to implement autocomplete with jquery plugin.

  • Providing an array with the list of values
  • Providing an array of objects whith pairs value/label
  • Providing an url to get the object array

The first two options are for a fixed list of values. If you want to populate the autocomplete list dinamically, you have to implement the last one.

To do that, you have to specify an url to get the objects array. If you want to keep the label and the key hidden, you need to create an input type hidden and set his value too.

For example

$( "#book" ).autocomplete({
           source: "getValues.htm",                     
           select: function(event, ui) {  
                $( "#book" ).val(ui.item.label);  
                $( "#book_hidden" ).val(ui.item.value);
                return false;  
            }                           
        });

getValues.html must return the array of label/values pair.

Hope this helps

like image 21
Tux9R Avatar answered Nov 24 '22 03:11

Tux9R


Ajax , Key Value pair ,Min Length to trigger search. Simple Code

<script>
    $(function () {
        $("#CompanySearch").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '/Admin/GetCompanyAutoComplete',
                    dataType: "json",
                    data: { term: request.term },
                    success: function (data) {
                        response(data);
                    }
                });
            },
            minLength: 2
        });
    });
</script>

For more

https://stackoverflow.com/a/40883309/5237614

like image 27
Arun Prasad E S Avatar answered Nov 24 '22 04:11

Arun Prasad E S