jQuery UI - 自动完成小部件显示不会更新 ui 菜单位置
每当我搜索时,位置都会正确显示
但经过一些滚动后,然后使用 input.autocomplete('widget').show(); 再次显示结果;
位置对齐方式仍然与之前的位置相同。在使用 input.autocomplete('widget').show(); 之前,有没有办法更新位置(使用 jQuery UI 自动完成位置计算)?
这是我的代码:
let itemTemplate = $("<li/>", {"class": "ui-menu-item"});
let itemWrapperTemplate = $("<div/>", {"class": "ui-menu-item-wrapper"});
$("input[autocomplete]").each(function() {
let input = $(this);
let autocomplete = input.attr("autocomplete");
let searchResults = [];
input.autocomplete({
source: function(request, response) {
const funcEval = function() {
const funcAjaxCompleted = function(event, data, funcAutoCompleteSelect) {
input.off('ajaxSearch', funcAjaxCompleted);
input.off("autocompleteselect");
if (typeof funcAutoCompleteSelect === "function") {
input.on("autocompleteselect", funcAutoCompleteSelect);
}
searchResults = data;
response(searchResults);
}
input.on('ajaxSearch', funcAjaxCompleted);
eval(autocomplete);
}
funcEval.call(input[0]);
},
appendTo: input.parent(),
minLength: 0
}).on("focus click", function() {
if (searchResults.length == 0 || input.val().trim() === "") {
input.autocomplete("search", input.val());
} else {
input.autocomplete('widget').show();
}
}).autocomplete("instance")._renderItem = function(ul, item) {
let itemClone = itemTemplate.clone();
let itemWrapperClone = itemWrapperTemplate.clone();
itemWrapperClone.text(item.code + " " + item.name);
return itemClone.append(itemWrapperClone).appendTo(ul);
};
});
Whenever I search, the position correctly displays
But after some scrolling then displaying the results again using input.autocomplete('widget').show();
The position alignment is still the same as the previous position. Is there a way to update the position(using jQuery UI Autocomplete position computation) before using the input.autocomplete('widget').show();
?
Here is my code:
let itemTemplate = $("<li/>", {"class": "ui-menu-item"});
let itemWrapperTemplate = $("<div/>", {"class": "ui-menu-item-wrapper"});
$("input[autocomplete]").each(function() {
let input = $(this);
let autocomplete = input.attr("autocomplete");
let searchResults = [];
input.autocomplete({
source: function(request, response) {
const funcEval = function() {
const funcAjaxCompleted = function(event, data, funcAutoCompleteSelect) {
input.off('ajaxSearch', funcAjaxCompleted);
input.off("autocompleteselect");
if (typeof funcAutoCompleteSelect === "function") {
input.on("autocompleteselect", funcAutoCompleteSelect);
}
searchResults = data;
response(searchResults);
}
input.on('ajaxSearch', funcAjaxCompleted);
eval(autocomplete);
}
funcEval.call(input[0]);
},
appendTo: input.parent(),
minLength: 0
}).on("focus click", function() {
if (searchResults.length == 0 || input.val().trim() === "") {
input.autocomplete("search", input.val());
} else {
input.autocomplete('widget').show();
}
}).autocomplete("instance")._renderItem = function(ul, item) {
let itemClone = itemTemplate.clone();
let itemWrapperClone = itemWrapperTemplate.clone();
itemWrapperClone.text(item.code + " " + item.name);
return itemClone.append(itemWrapperClone).appendTo(ul);
};
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找不到 jQuery UI Autocomplete 中是否有类似的函数,因此我尝试使用 jQuery
.offset()
和.outerHeight()
手动设置位置工作了。I couldn't find if there's a function like that in jQuery UI Autocomplete so I tried to manually set the position using jQuery
.offset()
and.outerHeight()
and it worked.