问题
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"}
]
回答1:
It is because of the test, if the test fails then _renderItem
does not return any value which is equivalent to returning undefined
.
But _renderItem
is called as given below, which causes the error since jquery attempts to set the data value to the returned value.
return this._renderItem( ul, item ).data( "ui-autocomplete-item", item );
In your case you have to, override _renderItemData
instead of _renderItem
var el = $('<my-autocomplete-element>');
var _renderItemData = el.data("ui-autocomplete")._renderItemData;
el.data("ui-autocomplete")._renderItemData = 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).data(
"ui-autocomplete-item", item).append("<div class='no-result'>"
+ item.value + "</div>").appendTo(ul);
} else {
_renderItemData.apply(this, arguments)
}
}
Demo: Plunker
来源:https://stackoverflow.com/questions/15741639/jquery-autocomplete-gives-typeerror-this-renderitem-is-undefined