Javascript jquery自动完成-下拉结果的条件样式
根据记录状态ACTIVE与INACTIVE的特定值,我希望为下拉结果的每条记录指定特定的背景色。如何做到这一点?我尝试了以下方法: json: js: 函数在到达if项之前返回。状态。。。所以这个代码永远不会被计算。考虑将标记建立为字符串,然后在完成后返回HTML字符串。 我将向元素添加一个active或inactive类,然后添加css规则。active{background color:eaffd6;}。inactive{background color:ffe5e5} 编辑您可以尝试以下操作Javascript jquery自动完成-下拉结果的条件样式,javascript,jquery,autocomplete,jquery-ui-autocomplete,Javascript,Jquery,Autocomplete,Jquery Ui Autocomplete,根据记录状态ACTIVE与INACTIVE的特定值,我希望为下拉结果的每条记录指定特定的背景色。如何做到这一点?我尝试了以下方法: json: js: 函数在到达if项之前返回。状态。。。所以这个代码永远不会被计算。考虑将标记建立为字符串,然后在完成后返回HTML字符串。 我将向元素添加一个active或inactive类,然后添加css规则。active{background color:eaffd6;}。inactive{background color:ffe5e5} 编辑您可以尝试以下操
$(this).data('ui-autocomplete')._renderItem = function (ul, item) {
var str_html = '<li class="' + item.status.toLowerCase() + '">'
+ '<a href="/"' + item.id + '" >' + item.label + '</a>'
+ '</li>'
return str_html''
};
有道理。我将添加类。。。但是,您能否更具体地说明第一点“考虑将标记构建为字符串,然后在完成后返回html字符串”,并提供一个如何实现的示例?很高兴提供帮助:
...
autocomplete: ({
source: function( request, response ) {
...
},
create: function() {
$(this).data('ui-autocomplete')._renderItem = function (ul, item) {
return $( "<li>" )
if ( item.status == 'ACTIVE' ) {
.append( "<a style='background-color: #eaffd6' href='/"+ item.id +"' >" + item.label + "</a>" )
}
if ( item.status == 'INACTIVE' ) {
.append( "<a style='background-color: #ffe5e5' href='/"+ item.id +"' >" + item.label + "</a>" )
}
//.append( "<a style='background-color: #ffe5e5' href='/"+ item.id +"' >" + item.label + "</a>" )
.appendTo( ul );
};
$(this).data('ui-autocomplete')._renderMenu = function( ul, items ) {
var that = this;
$.each( items, function( index, item ) {
that._renderItemData( ul, item );
});
};
}
})
...
$(this).data('ui-autocomplete')._renderItem = function (ul, item) {
var str_html = '<li class="' + item.status.toLowerCase() + '">'
+ '<a href="/"' + item.id + '" >' + item.label + '</a>'
+ '</li>'
return str_html''
};