Skip to content Skip to sidebar Skip to footer

How To Create Generic (reusable) Javascript Autocomplete Function

I now have a working JavaScript autocomplete function, thanks to help from many of you. Now I want to make the function reusable. There are three variables that need to be specifie

Solution 1:

insin was close. The solution I worked out this morning is;

functionAutoComplete(matchFieldName, resultFieldName, lookupURL) {
    $('#' + matchFieldName).autocomplete({
        source: function(request, response) {
            $.ajax({
                type: "POST",
                url: lookupURL,
                contentType: 'application/json',
                dataType: "json",
                data: JSON.stringify({ prefixText: request.term, count: 20 }),
                success: function(data) {
                    var output = jQuery.parseJSON(data.d);
                    response($.map(output, function(item) {
                        return {
                            value: item.Value,
                            label: (item.Label == item.Value) ? item.Label : item.Label + "(" + item.Value + ")"
                        }
                    }));
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        },
        minLength: 2,
        select: function(event, ui) {
            $('#' + resultFieldName).val(ui.item.value);
        }
    });
}

On the web page:

<div id="AutoSuggest">
    DOT Job Title or Number:
    <input type="text"id="dotmatch" style="width:300px;" />
</div>

And on the web page, after the tag:

<scripttype="text/javascript"src="js/DOTAutocomplete.js"></script><scripttype="text/javascript">
    $(function() {
        AutoComplete("dotmatch", "dotnumber", "/AutoSuggestJSTest/AutoSuggest.asmx/DOTList");
    });
</script>

Solution 2:

It looks like you're using jQuery, so you might want to implement it as a plugin.

(function($) {
  $.fn.bobsAutocomplete = function(resultFieldName, lookupURL) {
    this.autocomplete({
      source: function(request, response) {
          $.ajax({
              type: "POST",
              url: lookupURL,
              contentType: 'application/json',
              dataType: "json",
              data: JSON.stringify({prefixText: request.term, count: 20}),
              success: function(data) {
                  var output = jQuery.parseJSON(data.d);
                  response($.map(output, function(item) {
                      return {
                          label: item.Label + "(" + item.Value+ ")",
                          value: item.Value
                      }
                  }));
              },
              error: function(XMLHttpRequest, textStatus, errorThrown) {
                  alert(textStatus);
              }
          });
      },
      minLength: 2,
      select: function(event, ui) {
          $('#' + resultFieldName).val(ui.item.value);
          return ui.item.label;
      }
    });
    returnthis;
  };
})(jQuery);

Usage:

$("#dotmatch").bobsAutocomplete("dotnumber", "/AutoSuggestJSTest/AutoSuggest.asmx/DOTList");

Post a Comment for "How To Create Generic (reusable) Javascript Autocomplete Function"