Abdul basit
Abdul basit

Reputation: 307

Jquery Autocomplete too slow

I have written this code what it does is if user types postcode or city name it fetches from database using like query now the problem is i have around 1260 records with two fields one is city and other is post code

        SELECT code, area FROM post_codes WHERE code LIKE '$q%' or area LIKE '$q%'  ORDER BY area LIMIT 4

i have read many questions posted by users and researched online as well but nothing works used query delay as well .i have even indexed both these fields in database as well .. its getting records too slow that is the first problem now my second problem is when user is in that text field for search and he presses tab he can bypass the search and write any invalid code he wants how to restrict that here is my code for reference.

      $("#Postcode").autocomplete("get_codes2.php", {


        width: 260,
 queryDelay:0, 

   dataType: 'json',
   parse: function(data) {
            var array = new Array();
            for(var i=0;i<data.length;i++)
            {

            array[array.length] = { data: data[i], value: data[i].areacode, result: data[i].areacode};

            }
            return array;
    }, 

    formatItem: function(row) {                     





    return row.areacode;
    }






}).result(function (){

Upvotes: 0

Views: 573

Answers (1)

Tobias Sj&#246;sten
Tobias Sj&#246;sten

Reputation: 1094

I think Drupal's API search has a really nice approach to solving this problem. Their alternative to letting every incremental search hit the backend is to serve one big JSON file, which is used to autocomplete on client side.

In their implementation they're listening on the focus event for the search input, to fetch the JSON only when it's actually needed.

Upvotes: 1

Related Questions