我遇到了这个问题,我正在设计一个包含 jQuery UI 组合框的 float 小部件,它允许内容从屏幕的一侧自动换行。我似乎无法控制 jQuery UI 即时创建的菜单容器的实际宽度。我已经阅读了一些其他选项,包括应该在组合框打开时触发的“打开”事件,但我也没有运气让它起作用。
这是我的 fiddle ... http://jsfiddle.net/farina/RDd3A/14/
它说明了我的小部件中的一小段代码。如您所见,小部件将右对齐,并使用初始相对元素的绝对定位 float 。其他一切都按预期工作,我只是努力将“选项菜单”保持在小部件容器的范围内。
您会注意到,在第一次单击下拉箭头时,菜单容器超出了页面的一侧。当它不在 jsFiddle 查看器中时,它的效果会更糟。
非常感谢任何帮助,因为这让我发疯!
最佳答案
好的,所以...经过一些挖掘后,我意识到 jQuery ui.combobox 为其创建的自动完成设置了硬编码选项。我发现那是一团糟,绝不是一个完整的小部件。
我通过从这里获取源代码解决了我的问题 jQuery UI Source并用它自己的选项对象来扩充它......
(function ($) {
$.widget("ui.combobox", {
options: {
appendTo: "body",
position: {
my: "left top",
at: "left bottom",
collision: "none"
}
},
_create: function () {
var self = this,
select = this.element.hide(),
selected = select.children(":selected"),
value = selected.val() ? selected.text() : "";
var input = this.input = $("<input>")
.insertAfter(select)
.val(value)
.autocomplete({
appendTo: self.options.appendTo,
position: self.options.position,
delay: 0,
minLength: 0,
source: function (request, response) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
response(select.children("option").map(function () {
var text = $(this).text();
if (this.value && (!request.term || matcher.test(text)))
return {
label: text.replace(
new RegExp(
"(?![^&;]+;)(?!<[^<>]*)(" +
$.ui.autocomplete.escapeRegex(request.term) +
")(?![^<>]*>)(?![^&;]+;)", "gi"
), "<strong>$1</strong>"),
value: text,
option: this
};
}));
},
select: function (event, ui) {
ui.item.option.selected = true;
self._trigger("selected", event, {
item: ui.item.option
});
},
change: function (event, ui) {
if (!ui.item) {
var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i"),
valid = false;
select.children("option").each(function () {
if ($(this).text().match(matcher)) {
this.selected = valid = true;
return false;
}
});
if (!valid) {
// remove invalid value, as it didn't match anything
$(this).val("");
select.val("");
input.data("autocomplete").term = "";
return false;
}
}
}
})
.addClass("ui-widget ui-widget-content ui-corner-left");
input.data("autocomplete")._renderItem = function (ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + item.label + "</a>")
.appendTo(ul);
};
this.button = $("<button type='button'> </button>")
.attr("tabIndex", -1)
.attr("title", "Show All Items")
.insertAfter(input)
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass("ui-corner-all")
.addClass("ui-corner-right ui-button-icon")
.click(function () {
// close if already visible
if (input.autocomplete("widget").is(":visible")) {
input.autocomplete("close");
return;
}
// work around a bug (likely same cause as #5265)
$(this).blur();
// pass empty string as value to search for, displaying all results
input.autocomplete("search", "");
input.focus();
});
},
destroy: function () {
this.input.remove();
this.button.remove();
this.element.show();
$.Widget.prototype.destroy.call(this);
}
});
})(jQuery);
这至少允许您将两个更重要的样式选项传递给自动完成(appendTo 和位置)。
然后你可以简单地做这样的事情......
$('#myCombobox').combobox({ position: { my: "right top", at: "right bottom"} });
在进一步挖掘之后,我决定做这样的事情......
$('#myCombobox').combobox({ position: { collision: "flip", offset: "-25 0"} });
这正是我所需要的!
关于jQuery UI 组合框最大宽度选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9120296/