Jqueryui autocomplete: lists records with search queries in the middle

I use the jqueryui autocomplete widget to autocomplete the country field, but you are facing a problem. Suppose the user enters "in", I want jqueryui to list countries such as "india", "indonesia" that begin with "in", but it also lists countries that are "located" somewhere in the name (ex: argentina) How can I solve this problem?

jQuery(".autocomplete").autocomplete({
    source: CountrySuggestions, //CountrySuggestions is an array in javascript containing the list of countries
    minLength: 2
});

Akshey

+5
source share
1 answer

, , jQueryUI , . , , _initSource, ( " " (^)).

, , , , , , . , , - , .

$.ui.autocomplete.prototype._initSource = function() {
  var array,
    url;
  if ( $.isArray(this.options.source) ) {
    array = this.options.source;
    this.source = function( request, response ) {
      // escape regex characters
      var matcher = new RegExp( "^"+$.ui.autocomplete.escapeRegex(request.term), "i" );
      response( $.grep( array, function(value) {
        return matcher.test( value.label || value.value || value );
      }) );
    };
  } else if ( typeof this.options.source === "string" ) {
    url = this.options.source;
    this.source = function( request, response ) {
      $.getJSON( url, request, response );
    };
  } else {
    this.source = this.options.source;
  }
};

$("#countries").autocomplete({
  source: ["India","Indonesia","Argentina"],
  minLength: 2
});
+3

All Articles