Previous Suggestion Results cached?

moonlight293's Avatar

moonlight293

08 Oct, 2012 12:59 AM

Hi everyone,

I have an autosuggest textbox that changes what list it suggests via a checkbox. When I tick the checkbox, the list is changed over - however if I put in the same starting letters as I did the previous search it will come up with the old list.
Example:
Say the lists are as follows:
Old List: cat, carton, cast, Camilla, Casanova, Cassandra
New List: Camilla, Casanova, Cassandra

Now first I start entering some letters - 'ca'. The Old List shows up.
Now I check the checkbox. I enter some other letters - 'cas' and only those in the New List show up (Casanova, Cassandra).
However if I now do 'ca' (with the checkbox still checked) - the entire old list will come up instead.

Are there any ways to solve this problem? It looks like it's caching the old suggestion list for just 'ca'.
Thank you for your help.

  1. 1 Posted by moonlight293 on 08 Oct, 2012 01:00 AM

    moonlight293's Avatar

    Please note that I did not choose anything from the list inbetween checking the textbox.

  2. 2 Posted by Jon on 08 Oct, 2012 08:36 AM

    Jon's Avatar

    Post some code and we'll try to see what's happening.

  3. 3 Posted by moonlight293 on 08 Oct, 2012 07:38 PM

    moonlight293's Avatar

    Ok - this will be a long post. I am currently using ASP.NET MVC:

    On document.ready the code to initialise it is:
    $('#Text1').autoSuggest('<%= Url.Action("Autocomplete2", "Search") %>', default_params2);

    default_params2 is:

    var default_params2 = {
                    dataType: 'json',
                    emptyText: "Not found in list",
                    startText: "Enter a word",
                    asHtmlID: "check_code",
                    width: width,
                    selectedValuesProp: "key",
                    selectionAdded: function (elem) { gotoPage(); },
                    mustMatch: false
                };
    

    The onclick for the checkbox is as follows:

    $("#<%= checkbox1.ClientID %>").click(function () {
                    var a = $('#<%= checkbox1.ClientID %>')[0].checked;
                    var b = $('#<%= checkbox2.ClientID %>')[0].checked;
                    $.ajax({
                        url: '<%: Url.Action("updateList", "Home") %>',
                        type: 'POST',
                        data: { check1: a, check2: b },
                        dataType: 'json',
                        success: function (data) {
                            var params = [];
                            var vals = document.getElementById('as-values-check_code').value.replace(/^,|,$/g, "");
                            if (vals) {
                                vals = vals.split(",");
                                descriptions = document.getElementById('as-selections-check_code'').childNodes;
                                for (i = 0; i < vals.length; i++) {
                                    key = vals[i];
                                    value = descriptions[i].childNodes[1].nodeValue;
                                    obj = new Object();
                                    obj.key = key;
                                    obj.value = value;
                                    params.push(obj);
                                }
                            }
                            var element = document.getElementById('as-selections-check_code'');
                            var parent = element.parentNode;
                            parent.removeChild(element);
                            var element = document.getElementById('as-results-check_code'');
                            var parent = element.parentNode;
                            parent.removeChild(element);
    
                            var newinput = document.createElement('input');
                            newinput.setAttribute('id', 'textinput');
                            newinput.setAttribute('type', 'text');
                            parent.appendChild(newinput);
    
                            default_params2["preFill"] = params;
    
                            $('#interactions-wrapper>p>input[type=text]').autoSuggest('<%= Url.Action("Autocomplete2", "Home") %>', default_params2);
    
                        }
                    });
    });
    

    And the updateList method in the C# code:

    [HttpPost]
            public ActionResult updateList(bool check1, bool check2)
            {
                themainlist = null;
                if (check1 && check2)
                    themainlist = bothlists;
                else if (check1 && !check2)
                    themainlist = list1;
                else if (!check1 && check2)
                    themainlist = list2;
                else
                    themainlist = null;
    
                return Json(true, JsonRequestBehavior.AllowGet);
            }
    

    I hope that is helpful.

  4. 4 Posted by moonlight293 on 08 Oct, 2012 08:15 PM

    moonlight293's Avatar

    And the Autocomplete2 Method, for good measure:

    public JsonResult Autocomplete2(string q)
            {
                if (themainlist != null)
                {
                    var filteredItems = themainlist.Where(
                        item => item.Value.IndexOf(q, StringComparison.InvariantCultureIgnoreCase) == 0
                        ).Take(50);
                    filteredItems = filteredItems.Union(themainlist.Where(
                        item => item.Value.IndexOf(q, StringComparison.InvariantCultureIgnoreCase) > 0
                        ).Take(50));
                    var sortedList = filteredItems.ToList();
                    var autoCompleteData = sortedList.Select(x => new { key = x.Key, value = x.Value }).ToArray();
    
                    return Json(autoCompleteData, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
                    return Json(list, JsonRequestBehavior.AllowGet);
                }
            }
    
  5. 5 Posted by moonlight293 on 08 Oct, 2012 10:25 PM

    moonlight293's Avatar

    Apologies for the reply spam - but after testing around I have realised that this issue is specific to IE (which, unfortunately, many of our users use).

    IE caches AJAX requests, so to fix this I used
    $.ajaxSetup({ cache: false });

    Now it works perfectly! :)

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac