是他们现有的插件,或者要执行的代码可以使类别在水平上被拆解,例如表头:
http://jqueryui.com/demos/autocomplete/#categories
或者从头开始自己开始更容易?
你最有可能在某处有一个冲突的CSS。
我上周遇到了这个问题并解决了它。
与我相冲突的代码就是这个:
ul {list-style:none; }
这很可能包含在你的style.css文件中或某个网页内。
干得好
https://codepen.io/nitinmendiratta/pen/aMMeOz
CSS
#search {
width: 500px;
}
.ui-autocomplete {
display: flex;
width: auto !important;
}
.ui-autocomplete-category {
font-weight: bold;
padding: .2em .4em;
margin: .8em 0 .2em;
line-height: 1.5;
}
.ui-autocomplete-category ul{
padding:0;
}
.submenu {
font-weight: normal;
}
/* ADDED STYLE */
.ui-autocomplete>li>div>ul{
display: block !important;
position: relative !important;
border: 0 !important;
}
span.ui-menu-icon{
display:none !important;
}
/* ADDED for the SO snippet only !! Not needed on CodePen */
.ui-autocomplete>li{
display: inline-block !important;
}
HTML
<label for="search">Search: </label>
<input id="search">
<div class="ui-widget" style="margin-top:2em; font-family:Arial">
Result:
<div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
</div>
JS
$.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, submenuUl;
if ( item.category != currentCategory ) {
var elt = $("<li class='ui-autocomplete-category'>" + item.category + "</li>");
var submenu = $("<div class='submenu "+ item.category +"'><ul></ul></div>"); // Added <ul></ul>
elt.append(submenu);
ul.append(elt);
currentCategory = item.category;
}
submenuUl = ul.find("."+item.category+" ul"); // added +" ul"
li = that._renderItemData(submenuUl, item );
if ( item.category ) {
li.attr( "aria-label", item.category + " : " + item.label ).addClass("ui-menu-item"); // Added the addClass()
}
});
}
});
// Actual Code
$(function() {
var data = [
{ label: "annhhx10", category: "Products" },
{ label: "annk K12", category: "Products" },
{ label: "annttop C13", category: "Products" },
{ label: "anders andersson", category: "People" },
{ label: "andreas andersson", category: "People" },
{ label: "andreas johnson", category: "People" }
];
$( "#search" ).catcomplete({
delay: 0,
source: data,
select: function(item, ui){ // Added item, ui --- Arguments were missing.
console.log(ui.item.value);
$( "#search" ).val( ui.item.value );
$( "<div>" ).text( JSON.stringify(ui.item) ).prependTo( "#log" );
return false;
}
});
});