Reputation: 10400
I'm using the jQuery ui from google repository. I'm getting the data in the following format:
[{name:"test", param1:"test"},{name:"test2", param1:"test2"}...]
but the jQuery autocomplete are searching in the field named "label" So how could I change the "label" to "name" because I want to filter the information where the name fields are contains the typed value.
Upvotes: 1
Views: 353
Reputation: 16025
Reading between the lines on your post, I see that you can't edit the portion that's providing the data to the page, so you have to manipulate what you have. but it would be so much easier to just change what's being provided to match what you need
I believe to get this to work you're going to have to rework the options in the source callback, using the request object, and creating a item on each child that matches the one you want. So something like:
$( selector ).autocomplete({
source: function( request, response ) {
response( function(data){
for(key in data){
data[key].label = data[key].name;
}
return data;
} );
}
});
But I'm not 100% sure about this code.
alternately you could override the "filter" function here:
$.extend( $.ui.autocomplete, {
escapeRegex: function( value ) {
return value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
},
filter: function(array, term) {
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
return $.grep( array, function(value) {
return matcher.test( value.label || value.value || value );
});
}
});
to change the behavior to use what you're getting back. But I would call that bad design/implementation.
Upvotes: 2