我遇到这个问题,我正在设计的浮动小部件,其中包含jQuery UI组合框,允许内容直接包裹在屏幕的一侧。我似乎无法控制jQuery UI动态创建的菜单容器的实际宽度。我已经阅读了一些其他选项,包括打开组合框时应该触发的“开放”事件,但是我也没有运气这么做。
这是我所拥有的一个小提琴...... http://jsfiddle.net/farina/RDd3A/14/
它说明了我的小部件中的一小段代码。如您所见,窗口小部件将右对齐,并使用初始相对项目的绝对定位浮动。其他所有工作都按预期工作,我只是努力将“选项菜单”保持在小部件容器的范围内。
首次点击下拉箭头时,您会注意到,菜单容器会烧掉页面的一侧。当它不在jsFiddle查看器中时,它会更糟糕。
非常感谢任何帮助,因为这让我发疯了!
答案 0 :(得分:1)
好的,所以...经过一番挖掘后,我意识到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和position)。
然后你可以简单地做这样的事情......
$('#myCombobox').combobox({ position: { my: "right top", at: "right bottom"} });
在进一步挖掘后,我决定做这样的事情......
$('#myCombobox').combobox({ position: { collision: "flip", offset: "-25 0"} });
这正是我所需要的!
答案 1 :(得分:0)
您可以为下拉列表指定固定宽度,例如: .ui-menu {width:100px;}
不会这样做吗?