Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery UI组合框最大宽度选项_Jquery_Css_Jquery Ui_Combobox - Fatal编程技术网

jQuery UI组合框最大宽度选项

jQuery UI组合框最大宽度选项,jquery,css,jquery-ui,combobox,Jquery,Css,Jquery Ui,Combobox,我遇到了这个问题,我正在设计一个浮动小部件,它包含jQueryUI组合框,允许内容从屏幕的一侧包装。我似乎无法控制jQueryUI动态创建的菜单容器的实际宽度。我已经阅读了一些其他选项,包括“打开”事件,该事件应该在组合框打开时触发,但我也没有运气让它起作用 这是我的一把小提琴 它演示了我的小部件中的一小段代码。如您所见,小部件将右对齐,并使用初始相对项的绝对定位进行浮动。其他一切都按预期工作,我只是努力将“选项菜单”保持在小部件容器的范围内 您会注意到,在第一次单击下拉箭头时,菜单容器会弹出页

我遇到了这个问题,我正在设计一个浮动小部件,它包含jQueryUI组合框,允许内容从屏幕的一侧包装。我似乎无法控制jQueryUI动态创建的菜单容器的实际宽度。我已经阅读了一些其他选项,包括“打开”事件,该事件应该在组合框打开时触发,但我也没有运气让它起作用

这是我的一把小提琴

它演示了我的小部件中的一小段代码。如您所见,小部件将右对齐,并使用初始相对项的绝对定位进行浮动。其他一切都按预期工作,我只是努力将“选项菜单”保持在小部件容器的范围内

您会注意到,在第一次单击下拉箭头时,菜单容器会弹出页面的一侧。当它不在jsFiddle查看器中时,效果更糟


非常感谢您的帮助,因为这让我快疯了

您可以为下拉列表设置固定宽度,如: .ui菜单{宽度:100px;}


这难道不管用吗?

好吧,那么……经过一番挖掘之后,我意识到jQuery ui.combobox为它创建的自动完成功能提供了硬编码选项。我发现这是一个非常混乱的地方,根本不是一个完整的小部件

我修复了我的问题,从这里获取源代码,并用它自己的选项对象来扩展它,就像这样

(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'>&nbsp;</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);
$('#myCombobox').combobox({ position: { my: "right top", at: "right bottom"} });
$('#myCombobox').combobox({ position: { collision: "flip", offset: "-25 0"} });
在进一步挖掘之后,我决定这样做

(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'>&nbsp;</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);
$('#myCombobox').combobox({ position: { my: "right top", at: "right bottom"} });
$('#myCombobox').combobox({ position: { collision: "flip", offset: "-25 0"} });

这正是我所需要的

我对此有一个答案,但我不能再发布6个小时。我会把它放在注释中,但代码看起来很糟糕:(.不,自动完成小部件的本质是它实际上隐藏了基本元素,然后创建了一个带有一系列自定义属性的新UL。唯一的实际选择是修改jquery ui combobox脚本,以允许一些通常传递给用户的动态变量“自动完成”也将传递到“combobox”。我会尽快发布解决方案。这非常简单,如果你问我的话,jQuery UI只是一个明显的遗漏。