Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pasting in autocomplete is not triggering the AJAX request #861

Open
WilsonSant opened this issue Oct 10, 2024 · 0 comments
Open

Pasting in autocomplete is not triggering the AJAX request #861

WilsonSant opened this issue Oct 10, 2024 · 0 comments

Comments

@WilsonSant
Copy link

WilsonSant commented Oct 10, 2024

Problem:

When you paste in the autocomplete field it does'nt trigger the Ajax request.

Steps to reproduce:

  1. Paste/Insert a name in the autocomplete (Let's say John)
  2. The autocomplete will trigger the Ajax request and we will see the "John" name in there.
  3. Don't select John. Click outside of the autocomplete so it triggers the focusout and the search is cleared.
  4. Paste John again. Now it will not search anymore. You can clear the field with cut (Ctrl + X on windows) and paste again many times you want, it will not trigger the ajax search anymore.

My configuration for the autocomplete:

$('#student_name').devbridgeAutocomplete({
    serviceUrl:'service.php',
    paramName:'student_name',
    params:{
        request:"getStudentsByName",
    },
    showNoSuggestionNotice: true,
    noSuggestionNotice:messageNoSuggestion,
    autoSelectFirst:false,
    deferRequestBy:300,
    minChars: 2,
    noCache:true,
    triggerSelectOnValidInput:false,
    transformResult:function (response, originalQuery) {
        return {
            suggestions: $.map(JSON.parse(response), function(student)  {
                return {
                        name:student.student_name,
                        data:student.id
                    }
            })
        }
    },
});

Solution

So, debugging i found that the problem was in the "onKeyUp" function on line 451 of jquery.autocomplete.js . Is on the line 466 if

            if (that.currentValue !== that.el.val()) {

If you search and don't select like in the steps explained earlier, the that.currentValue would be the last requested value (in our case was John) and when you paste it again the that.el.val() would be John (the same as the currentValue).
So it will never fall under the if. My provisional solution right now is doing an else with that.currentValue = '':

            if (that.currentValue !== that.el.val()) {   
                that.findBestHint();  
                if (that.options.deferRequestBy > 0) {  
                    // Defer lookup in case when value changes very quickly:  
                    that.onChangeTimeout = setTimeout(function () {  
                        that.onValueChange();  
                    }, that.options.deferRequestBy);  
                } else {  
                    that.onValueChange();  
                }  
            } else {  
                that.currentValue = ''  
            }

It fixed the problem, but i don't know if that's the best solution or if will ending up creating new bugs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant