aqiltariq65 aqiltariq65 - 28 days ago 7
Javascript Question

jquery ui autocomplete with ajax with custom search not working

My json response is:

[{"value":"admin@admin.com"},{"value":"customer@customer.com"},{"value":"customer1@customer.com"},{"value":"provider@provider.com"}]


My jQuery:

$( "#email1" ).autocomplete({
source: function (request, response) {
$.ajax({
url: "/useremail",
dataType: "json",
data: {
term: request.term
},
success: function (data) {
//response(data);
console.log(data);
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
response( $.grep( data, function( item ){
return matcher.test( item );
}));
}
});
},
minLength: 3
});


I cannot perform the custom search from my json data here is an example of document in which custom search is performed jQuery-ui autocomplete

Basically my search would only show those email which match my input field. If i use only response(data) it shows all the result but does not show search.

Answer

Please try this:

success: function (data) {
          //response(data);
          console.log(data);
          var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i");
          var d = $.grep( data, function(item) {
                return matcher.test( item.label || item.value || item );
          });
          response(d);
}