Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-52179

Ajax auto complete form field should wait a bit before refreshing

    XMLWordPrintable

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.0
    • None
    • Forms Library
    • MOODLE_30_STABLE

    Description

      When typing in an autocompleted Ajax auto complete field for each key pressed an ajax request will be triggered, which causes the suggestions to be updated multiple times.

      E.g. When you type 'bob', the suggestions jump from 'b' to 'bo', to 'bob'.

      Here is a patch that fixes it:

      diff --git a/lib/amd/src/form-autocomplete.js b/lib/amd/src/form-autocomplete.js
      index ed6ed9c..e5db353 100644
      --- a/lib/amd/src/form-autocomplete.js
      +++ b/lib/amd/src/form-autocomplete.js
      @@ -759,7 +759,15 @@ define(['jquery', 'core/log', 'core/str', 'core/templates', 'core/notification']
                                   updateAjax(e, options, state, originalSelect, ajaxHandler);
                               };
                               // Trigger an ajax update after the text field value changes.
      -                        inputElement.on("input keypress", handler);
      +                        var changeTimeout;
      +                        inputElement.on("input keypress", function(e) {
      +                            if (typeof changeTimeout !== 'undefined') {
      +                                clearTimeout(changeTimeout);
      +                            }
      +                            changeTimeout = setTimeout(function() {
      +                                handler(e);
      +                            }, 200);
      +                        });
                               var arrowElement = $(document.getElementById(state.downArrowId));
                               arrowElement.on("click", handler);
                           });
      

      Also note that it would be better to show a spinner in the suggestion box to indicate that the search is active. Especially that when you type 'bob', get the results, then add a letter, the suggestions will be filtered once more by that letters while triggering an ajax request which may show different results or ordering.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              fred Frédéric Massart
              David Woloszyn, Huong Nguyen, Jake Dallimore, Michael Hawkins, Stevani Andolo
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: