【发布时间】:2014-12-22 10:17:23
【问题描述】:
我正在尝试自定义category autocomplete widget 的下拉菜单,因为我对普通小部件的下拉菜单几乎没有问题,但我无法弄清楚如何做到这一点。我可以为普通的自动完成小部件找到资源,但不能为分类小部件找到资源。下面是我的代码。
<head>
<style type="text/css">
.ui-autocomplete-category {
font-weight: bold;
padding: .2em .4em;
margin: .8em 0 .2em;
line-height: 1.5;
z-index: 2000;
}
</style>
<script>
$.widget( "custom.catcomplete", $.ui.autocomplete, {
_create: function() {
this._super();
this.widget().menu( "option", "items", "> :not(.ui-autocomplete-category)" );
},
_renderMenu: function( ul, items ) {
var that = this,
currentCategory = "";
$.each( items, function( index, item ) {
var li;
if ( item.category != currentCategory ) {
ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
currentCategory = item.category;
}
li = that._renderItemData( ul, item );
if ( item.category ) {
li.attr( "aria-label", item.category + " : " + item.label );
}
});
}
});
</script>
<script>
$(function() {
var data = [
{ label: "TV", category: "Electornics" },
{ label: "WM", category: "Electornics" },
{ label: "M3", category: "Electornics" },
{ label: "Table", category: "Funiture" }
];
$( "#search" ).catcomplete({
delay: 0,
source: data
});
});
</script>
</head>
网页其他部分的 z-index 大于此值,因此下拉菜单被少数元素隐藏。
默认的下拉菜单样式看起来很基本,如果项目文本很长,下拉菜单的宽度会超过搜索输入的宽度,所以想像下面这样自定义。
有人可以对分类小部件的自定义有所了解吗?
【问题讨论】:
-
你有什么问题?你试过了吗?
-
自定义是什么意思?
标签: javascript jquery css jquery-autocomplete