How to create generic (reusable) JavaScript autocomplete function

别等时光非礼了梦想. 提交于 2019-12-01 11:43:39
Bob Jones

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

function AutoComplete(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:

<script type="text/javascript" src="js/DOTAutocomplete.js"></script>

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

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;
      }
    });
    return this;
  };
})(jQuery);

Usage:

$("#dotmatch").bobsAutocomplete("dotnumber", "/AutoSuggestJSTest/AutoSuggest.asmx/DOTList");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!