2

I have a legacy project with JQuery Autocomplete (not part of JQuery-UI) input inside a JQGrid with the following code:

{name : 'city',index : 'city',width : 300, editable: true,
    editoptions: {
        dataInit : function (elem){
            $(elem).autocomplete('json/get_cities', {mustMatch: true, matchContains: true, minChars: 2, max:10});
        }
    } 
},

When I start typing the ajax call triggers on the second letter, but only once. No consecutive calls on 3-rd, 4-th and so on letters typed. Also, when I start deleting symbols another ajax call is trigger when there is only one character left in the input.

Any ideas how to fix this?

RossTsachev
  • 921
  • 1
  • 7
  • 19

1 Answers1

1

The problem seems to me independent from jqGrid. Nevertheless the problem is clear. The first request to the URL json/get_cities will be cached and it will be used for some next calls too.

The request to the URL json/get_cities is HTTP GET request. Thus you can solve the problem by setting HTTP header

Cache-Control: private, max-age=0

in the response of json/get_cities. See the tutorial for more details or the discussion here about alternative caching headers.

I'm not PHP developer, but it seems that you can use something like

<?php
header("Cache-Control: private, max-age=0");
?>
Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks for the answer. In my response header I can see: Cache-Control no-store, no-cache, must-revalidate, max-age=0 , but still only one request is triggered from JQuery autocomplete. – RossTsachev Feb 15 '16 at 14:36
  • I finally managed to make it work correctly by adding to the settings cacheLength: 0 and removing mustMatch: true. – RossTsachev Feb 15 '16 at 14:56
  • @Ross_102: Sorry, but I included the reference to [the post](http://stackoverflow.com/a/9269567/315935) to show that one insert many unneeded cache headers only if one don't sure what to do. Combination of `no-store` with other has no sense. If `Cache-Control: private, max-age=0` or `Cache-Control: private, no-store` not work and **you really verified that the headers are placed in HTTP response**, then you should search for another reason of the problem. You should use Developer Tools of IE/Chrome or [Fiddler](http://www.telerik.com/fiddler) to see HTTP traffic. Do you made HTTP trace or not? – Oleg Feb 15 '16 at 15:00
  • Sorry, but neither `mustMatch` nor `cacheLength` exist in jQuery UI or jqGrid. Which version of jQuery UI or jqGrid you use? – Oleg Feb 15 '16 at 15:04
  • Oleg, I made it work. It was the caching as you said, I just disabled it in JQuery Autocomplete and now it behaves as expected. Thanks. The project is with jQuery Autocomplete plugin 1.2.2 :) – RossTsachev Feb 15 '16 at 15:06
  • @Ross_102: How you disabled it? The behavior of caching will be managed by HTTP header. **All web browsers** follows the requirements of HTTP headers. jQuery UI Autocomplate have no caching which you can disable. See [the code](https://github.com/jquery/jquery-ui/blob/1.11.4/ui/autocomplete.js#L379-L389). No `cacheLength` or `mustMatch` options exists too (just search for the text in the source code). I suppose that the previous changing in HTTP header work now. You need to clear the cache of the web browser and repeat your tests once more. I recommend to start Developer Tools (F12, Network) – Oleg Feb 15 '16 at 15:16
  • It used to be a separate plugin, before being merged into JQuery-UI. See here: http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/ – RossTsachev Feb 15 '16 at 15:27
  • @Ross_102: You can read in the link "This plugin is deprecated and not developed anymore". It's deprecated almost 6 years ago. Which sense is to use such retro plugin now? Do you use jQuery UI? In which version? You can't use both jQuery UI and the retro Autocomplete plugin together. By the way the Autocomplete plugin uses parameters of jQuery.ajax which not exist more. I would strictly recommend to remove the plugin and to use just jQuery UI. – Oleg Feb 15 '16 at 15:37
  • @Ross_102: If you write about jQuery Autocomplete then everybody understand you that you use jQuery UI Autocomplete. Even the tag which you used in your question say the same: see http://stackoverflow.com/tags/jquery-autocomplete/info. (see the last words "Part of jquery-ui.") – Oleg Feb 15 '16 at 15:39
  • Fixed my post according to your suggestions. As for why I use it - it is a legacy project that I have to support – RossTsachev Feb 15 '16 at 15:47
  • @Ross_102: I'd still recommend you don't use old autocomplete and to use either jQuery UI Autocomplete or another nice plugins which exist now like [select2](https://select2.github.io/examples.html) (see [here](https://select2.github.io/examples.html#data-ajax)) or [chosen](http://harvesthq.github.io/chosen/). I personally like select2 and use it. It's very comfortable for the users. – Oleg Feb 15 '16 at 16:00