javascript - JQuery UI Autocomplete,如何遍历其他元素
问题描述
所以我尝试了两种方法:
<div id = "team-search-container">
<label for="team-search" class = "text-center">
<input type = "text" id = "team-search">
</label>
</div>
如果我这样做:
$( "#team-search" ).catcomplete({
delay: 0,
source: teamdata,
appendTo: '#team-search-container'
});
它将展开 div 以显示元素,如下所示:( 忽略它所说的 ComboBox 元素,我的意思是编写自动完成元素)
但如果我做这样的事情,没有appendTo
选择,
$( "#team-search" ).catcomplete({
delay: 0,
source: teamdata
});
它可以正常工作,但在结束时body
,它会使空白空间等于自动完成的高度。这是我的CSS:
.ui-autocomplete{
position: relative;
max-height: 300px;
overflow-y: auto;
overflow-x: hidden;
padding-right: 20px;
}
.ui-autocomplete-category{
font-size: 18px;
list-style: none;
width: 200px;
}
.ui-menu-item{
list-style: none;
width: 200px;
cursor: default;
background-color: #565656;
}
.ui-helper-hidden-accessible {
display: none;
}
因此,由于我有max-height
of 600px
,如果我不将其附加到任何内容,它将在页面底部创建600px
空白空间,即使它在我的搜索栏下方显示自动完成。
解决方案
使用您提供的示例,我无法按照您的描述复制该问题。请查阅:
$(function() {
$.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);
}
});
}
});
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"
}
];
$("#team-search").catcomplete({
delay: 0,
source: data,
appendTo: '#team-search-container'
});
});
.ui-autocomplete {
position: relative;
max-height: 300px;
overflow-y: auto;
overflow-x: hidden;
padding-right: 20px;
}
.ui-autocomplete-category {
font-size: 18px;
list-style: none;
width: 200px;
}
.ui-menu-item {
list-style: none;
width: 200px;
cursor: default;
background-color: #565656;
}
.ui-helper-hidden-accessible {
display: none;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div id="team-search-container">
<label for="team-search" class="text-center"><input type = "text" id = "team-search" /> </label>
</div>
数据和类别正在按预期加载。如果您需要进一步的帮助,请检查您的代码,检查任何控制台错误,并提供一个最小的、可重现的示例。您的示例不包含任何示例数据。
推荐阅读
- python - Python OpenCV 线检测以检测图像中的“X”符号
- html - Can I reliably set the subject and body of an email in a form using mailto:?
- c# - Dependency injection with the new .net-core-3 json serializer
- r - R: 2 column layout in Rmarkdown with tabset
- class - Centura 中的功能类是什么?
- ios - Copy Dragged folder(items) into temp folder in swift macOS
- hl7-fhir - Related resources in single response in FHIR
- visual-studio-code - vs 代码扩展可以更改 vs 代码设置吗?
- go - 在这个例子中爬虫多线程是如何工作的?
- spring - 使用多个内存数据库进行 Spring Boot / JUnit 集成测试