gpt4 book ai didi

javascript - 如何自定义 jquery ui 自动完成?

转载 作者:行者123 更新时间:2023-11-28 18:22:27 25 4
gpt4 key购买 nike

我正在使用 jquery ui 自动完成功能,对于出现在下拉列表中的术语列表,有没有一种方法可以对这些术语进行分类?基本上我想要的是:在下拉菜单中,根据术语来自哪个类别,其背景颜色将是某种颜色。像你这样定义字典的东西:

var dictionary = {"apple":"red", "banana":"yellow"};

然后,您将其传递给 jquery ui 自动完成初始化程序,下拉列表将包含具有红色背景色的术语 apple 和具有黄色背景色的 banana .

这可能吗?

谢谢。

最佳答案

这完全有可能看看这个 Jquery-ui-Autocomplete Catagories

现在的基本要点是:

<style>
.ui-autocomplete-category {
font-weight: bold;
padding: .2em .4em;
margin: .8em 0 .2em;
line-height: 1.5;
}
</style>
<script>
$.widget( "custom.catcomplete", $.ui.autocomplete, {
_renderMenu: function( ul, items ) {
var that = this,
currentCategory = "";
$.each( items, function( index, item ) {
if ( item.category != currentCategory ) {
ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
currentCategory = item.category;
}
that._renderItemData( ul, item );
});
}
});
</script>
<script>
$(function() {
var data = [
{ label: "anders", category: "" },
{ label: "andreas", category: "" },
{ label: "antal", category: "" },
{ 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
});
});
</script>
</head>
<body>
<label for="search">Search: </label>
<input id="search" />
</body>

现在您要做的是更改 .ui-autocomplete-catagory CSS 类以获得类别的一般概念,为了让特定类别具有不同的颜色,您需要添加自己的 CSS 类。您可以使用 jQuery 来执行此操作,或者将它们具体添加到上面第一个脚本中的 ul.append() 中的每个类别中。

关于javascript - 如何自定义 jquery ui 自动完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16501425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com