0

I have a textbox to search a city from the JSON file according to name but it does not work. For example when i search Antalia my autocomplete results just returns me all the cities listed in my JSON file:

This is my JSON file :

[
    { "label" : "Tehran", "hotelid" : 1203548.0 },
    { "label" : "Antalya", "hotelid" : 1168092.0 }
]

and here is my jquery autocomplete code:

<input autocomplete="off" name="_root.route.start.country" class="autocomplete-hint select" data-auto-complete-minlength="1" type="text" onFocus="searchCountry(this)" placeholder="Departure">

<script type="text/javascript">

    function searchCountry(a) {
        $(function() {
            var cache = {};
            $(a).autocomplete({
                appendTo: ".countrys",
                change: function (event, ui) {
                    if (ui.item == null || ui.item == undefined) {
                        $(a).val("");
                        $(a).empty();
                        $(a).next("#loading").hide();
                    } else {
                        var position = $(".countrys").position(),
                        left = position.left, top = position.top;
                        $(".countrys > ul").css({
                            left: left + 20 + "px",
                            top: top + 4 + "px"
                        });
                    }
                },
                minLength: 1,
                select: function (event, ui) {
                    // Set autocomplete element to display the label
                    this.value = ui.item.label;
                    $(this).closest("tr").find(".countryid").val(ui.item.hotelid);
                    $(this).closest("tr").find(".countryid").next(".countryid").val(ui.item.hotelid);

                    // Store value in hidden field
                    $('#hidden_field').val(ui.item.id);
                    $('#hidden_field1').empty().text(ui.item.label);

                    // Prevent default behaviour
                    return false;
                },
                source: function( request, response ) {
                    $(a).next("#loading").show();
                    var term = request.term;
                    if (term in cache) {
                        response(cache[term]);
                        return;
                    }

                    $.getJSON( "jsonloadi.bc", request, function( data, status, xhr ) {
                        $(a).next("#loading").hide();
                        cache[ term ] = data;
                        response( data );
                    });
                }
            });
        });
    }
</script>
3
  • can you provide a jsfiddle? Commented Jul 25, 2017 at 6:34
  • yes i will provide. Commented Jul 25, 2017 at 6:38
  • @Anami but when i change the json file to Array type it does not work again. Commented Jul 25, 2017 at 6:46

1 Answer 1

1

Because you are loading the whole JSON file and returning it without any filtering by your query.

$(a).next("#loading").show(); // move loading animation here

$.getJSON("jsonloadi.bc", request, function(data, status, xhr) {
  // YOU NEED TO FILTER DATA FIRST!
  var filtered = data.filter(function(hotel) {
    return hotel.label.indexOf(term) !== -1;
  });
  cache[term] = filtered;
  $(a).next("#loading").hide();
  response(filtered);
});

You should also move $(a).next("#loading").show(); as shown in my example, because you don't need a loading animation when just returning a cached response and you would also leave that animation there indefinitely.

Sign up to request clarification or add additional context in comments.

2 Comments

you mean i have to remove the $(a).next("#loading").show(); from the above code and put it at the top of my codes?
@inaz Yes, but that is not that important. First of all carefully check how I modified the $.getJSON callback. I am filtering the data there with the term and caching/returning the filtered data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.