1

In my project, I am trying to create a autocomplete effect using the following plugin:

Devbridge jQuery Autocomplete

This plugin is working fine until I don't add space into my textbox (after adding a word). and when I just delete the entered word using backspace then the autocomplete is showing the previous list which should have shown before.

PS: Every time I am passing the full text of the text field to server through ajax call which is necessary in my application.

Here is my code:

JS Fiddle (not working because of ajax url)

JS

$(function () {

    var result = $('#result');
    var contents = {
        value: "",
        data: ""
    };
    /* Ajax call */
    result.keydown(function (event) {
        if (!event.shiftKey) {
            var sugData;
            var text = result.val(); //.split(" ").pop();
            //console.log(text);
            /* Send the data using post and put the results in a div */
            $.ajax({
                url: "http://localhost:9999/projects/1/autocomplete/suggestions",
                type: "POST",
                data: "drqlFragment=" + text, // + "  ",
                //data: "drqlFragment=when node_database_property  ",
                async: false,
                cache: false,
                headers: {
                    accept: "application/json",
                    contentType: "application/x-www-form-urlencoded"
                },
                contentType: "application/x-www-form-urlencoded",
                processData: true,
                success: function (data, textStatus, jqXHR) {
                    var resData = data.suggestions;
                    //console.dir(resData);
                    for (var i = 0; i < resData.length; i++) {
                        resData[i].value = resData[i].keyword;
                    }
                    sugData = resData;
                    //console.dir(sugData);
                },
                error: function (response) {
                    //console.dir(response);
                    $("#result").val('there is error while submit');
                }
            });
            console.dir(sugData);
            $('#result').autocomplete({
                lookup: sugData
            });
        }
    });
});

HTML

<form id="foo">
    <textarea id="result" rows="4" cols="50"></textarea>
    <input type="submit" value="Send" />
</form>

Sorry, I can't provide you the json data because it is being modified by the server whenever I press a key. (So, actually it is an object variable returning by the server on ajax call).

Mr_Green
  • 36,985
  • 43
  • 143
  • 241
  • Whenever I am entering a key, the ajax call is happening which is gathering the required data and showing as autocomplete suggestions. But when I am adding space, the ajax call is returning with required data but not showing anything as autocomplete suggestion. and later when I delete two or three characters of the entered word then the previous autocomplete suggestion is being visible. – Mr_Green Apr 18 '13 at 11:41
  • Could you provide code for "suggestions", backend function – Satpal Apr 18 '13 at 11:47
  • @SatpalSingh No, `suggestions` doesn't have space and even I can't provide the code related to it because whenever I am entering any key then the server is doing some functions and returning some data as response. (ajax call). So, the problem is not related to data. I am sure I am doing something wrong in the above code which I couldn't figure it out. – Mr_Green Apr 18 '13 at 11:48
  • `suggestions` doesn't have space then how will they filter text with space, as per my understanding everything is working fine as you don't have any data. Just for curiosity cook some data and test it – Satpal Apr 18 '13 at 11:53
  • @SatpalSingh Everything is happening at server side. If you see again at my code then you can find that the content type is `"application/x-www-form-urlencoded"` instead of `json`. I know it is hard to reproduce my problem though. – Mr_Green Apr 18 '13 at 11:57
  • Without knowing what your 'suggestions' is populated with it's impossible to reproduce. The best we can do is what @SatpalSingh has already done and shown you that if the suggestions contain spaces they do in fact work. Ergo, the problem must be that your suggestions do not contain the information you are trying to autocomplete. – rlemon Apr 18 '13 at 12:12
  • @rlemon For example, whenever I type `play` in my textbox (while typing `play` the autosuggestion is showing like `play, playing, played`) and press space then the server will respond with `cricket, football, volleybal` (as array object) like this. which is happening but not being visible as autocomplete suggestion for my above code. So, I strongly sense that I made some silly mistake in my above code. which I can't figure it out. for more clear explanation, please read my first comment. – Mr_Green Apr 18 '13 at 12:15

0 Answers0