I'm currently using .data( "ui-autocomplete" )._renderItem = function( ul, item )
in three places in my page. Now I need to add one more and I'm done.
But this time I get the error message
TypeError: this._renderItem(...) is undefined
The strange thing is that I only get this if my IF
test is false.
.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
// If only one row is returned and ID = 0, then return 'no result' message
if(item.id == '0') {
return jQuery( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<div class='no-result'>"+ item.value + "</div>" )
.appendTo( ul );
}
I can't figure out why it's failing here, and not in the other 3 places. Is there a conflict somewhere?
This is my code
jQuery('#my-selector').autocomplete({
minLength: 2,
source: function( request, response ) {
jQuery.ajax({
url: callback_url,
dataType: "json",
data: {
term: request.term
},
success: function( data ) {
response( jQuery.map( data, function( item ) {
return {
id: item.id,
value: item.name,
name_encoded: item.name_encoded
}
}));
}
});
},
select: function( event, ui ) {
return false;
}
}).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
if(item.id == '0') {
return jQuery( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<div class='no-result'>"+ item.value + "</div>" )
.appendTo( ul );
}
};
Update
This change works. But I still don't understand why I must use a else
statement here, when I'm not using it in two other autocomplete
functions.
.data( "ui-autocomplete" )._renderItemData = function( ul, item ) {
if(item.id == '0') {
return jQuery( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<div class='no-result'>"+ item.value + "</div>" )
.appendTo( ul );
} else {
return this._renderItem( ul, item ).data( "ui-autocomplete-item", item );
}
};
Return data from query with search term some
[
{"id":"8186","name":"Some Are Thieves"},
{"id":"6149","name":"Somet"},
{"id":"6150","name":"Somethin'Else from SKECHERS"}
]