Fahad Khan
Fahad Khan

Reputation: 1635

jQuery autocomplete not properly working

autocomplete opens as I type 1st character, but when I type another character or two it does not filter, remains same.

Even on 1st character, list open is same for anything typed, in order (without filter) as coming from web service.

Here is my code:

$(function() {
  $('#to').autocomplete({
    source: 'json/searchobjects.php',
          minLength: 1,
          select: function(event, ui) {
              globalDest = ui.item.latlng;
          }
  });
});

Here is JSON object:

[
{
label: "label-1",
value: "val-1"
},
{
label: "label-2",
value: "val-2"
} 
]

Its looks I am missing something.

Thanks for any help

Upvotes: 2

Views: 139

Answers (2)

Bellash
Bellash

Reputation: 8184

According to your edit, your json must be of type ["el1","el2",...,"el3"] or if you need to keep [{},{},...] type. but if you need to use your object list, please use it as follows

      source: function (request, response) {
            jQuery.getJSON(u, {
                term: request.term*,
                json: 'yep'*/
            }).done(function (_result2) {

                //alert(_result2[0].Label)
                response($.map(_result2, function (item) {
                    return {
                        label: item.Label,
                        value: item.Value,
                        id: item.Value
                    }
                }));
            }).fail(function () {
                alert('fail');
            })
                .error(function () {
                    alert('error');
                });
        }

having term as request param in your 'json/searchobjects.php?term=TERM' to filter server-sidely using select * from table where Label='%$_GET["term"]' or something

Upvotes: 1

osuddeth
osuddeth

Reputation: 162

I've lived this issue before. In your PHP script that is serving the results, you need to filter based off of the value your jQuery autocomplete script is passing to it. Use $_GET['term'] for this filter, as that's what's autocomplete uses.

The jQuery autocomplete plugin is sending a GET request silently to your script with "term" as the variable, and to my knowledge, you can't configure this setting.

Do note that you may want to up the number of needed characters beyond one, as you're going to get a ton of results returned that your users don't care about. I find that three characters is often a great balance between speed and usefulness.

Upvotes: 2

Related Questions