如何在 jQuery 小部件上设置选项

发布于 2025-01-02 20:21:01 字数 5546 浏览 1 评论 0原文

我正在尝试为 Wijmo 中几乎未记录的复选框小部件设置选项。这是小部件的代码:

(function ($) {
    "use strict";
    var checkboxId = 0;
    $.widget("wijmo.wijcheckbox", {
        _csspre: "wijmo-checkbox",
        _init: function () {
            var self = this,
                ele = self.element,
                o = self.options,
                checkboxElement, label, targetLabel, boxElement, iconElement;
            if (ele.is(":checkbox")) {
                if (!ele.attr("id")) {
                    ele.attr("id", self._csspre + checkboxId);
                    checkboxId += 1;
                }
                if (ele.parent().is("label")) {
                    checkboxElement = ele.parent()
                    .wrap("<div class='" + self._csspre + "-inputwrapper'></div>")
                    .parent()
                    .wrap("<div></div>").parent().addClass(self._csspre + " ui-widget");
                    label = ele.parent();
                    label.attr("for", ele.attr("id"));
                    checkboxElement.find("." + self._csspre + "-inputwrapper")
                    .append(ele);
                    checkboxElement.append(label);
                }
                else {
                    checkboxElement = ele
                    .wrap("<div class='" + self._csspre + "-inputwrapper'></div>")
                    .parent().wrap("<div></div>").parent()
                    .addClass(self._csspre + " ui-widget");
                }
                targetLabel = $("label[for='" + ele.attr("id") + "']");
//              if (targetLabel.length > 0) {
//                  checkboxElement.append(targetLabel);
//                  targetLabel.attr("labelsign", "C1");
//              }
                if (ele.is(":disabled")) {
                    self._setOption("disabled", true);
                }
                boxElement = $("<div class='" + self._csspre +
                "-box ui-widget ui-state-" +
                (o.disabled ? "disabled" : "default") +
                " ui-corner-all'><span class='" +
                self._csspre + "-icon'></span></div>");
                iconElement = boxElement.children("." + self._csspre + "-icon");
                checkboxElement.append(boxElement);
                ele.data("iconElement", iconElement);
                ele.data("boxElement", boxElement);

                boxElement.removeClass(self._csspre + "-relative")
                .attr("role", "checkbox")
                .bind("mouseover", function () {
                    ele.mouseover();
                }).bind("mouseout", function () {
                    ele.mouseout();
                });
                if (targetLabel.length === 0 || targetLabel.html() === "") {
                    boxElement.addClass(self._csspre + "-relative");
                }
                ele.bind("click.checkbox", function (e) {
                    self.refresh(e);
                }).bind("focus.checkbox", function () {
                    if (o.disabled) {
                        return;
                    }
                    boxElement.removeClass("ui-state-default").addClass("ui-state-focus");
                }).bind("blur.checkbox", function () {
                    if (o.disabled) {
                        return;
                    }
                    boxElement.removeClass("ui-state-focus").not(".ui-state-hover")
                    .addClass("ui-state-default");
                }).bind("keydown.checkbox", function (e) {
                    if (e.keyCode === 32) {
                        if (o.disabled) {
                            return;
                        }
                        self.refresh();
                    }
                });

                boxElement.bind("click.checkbox", function (e) {
                    ele.get(0).checked = !ele.get(0).checked;
                    ele.change();
                    ele.focus();
                    self.refresh(e);
                });

                self.refresh();
                checkboxElement.bind("mouseover.checkbox", function (e) {
                    if (o.disabled) {
                        return;
                    }
                    boxElement.removeClass("ui-state-default").addClass("ui-state-hover");

                }).bind("mouseout.checkbox", function (e) {
                    if (o.disabled) {
                        return;
                    }
                    boxElement.removeClass("ui-state-hover").not(".ui-state-focus")
                    .addClass("ui-state-default");
                });
            }
        },

        refresh: function (e) {
            var self = this;
            self.element.data("iconElement")
            .toggleClass("ui-icon ui-icon-check", self.element.get(0).checked);
            self.element.data("boxElement")
            .toggleClass("ui-state-active", self.element.get(0).checked)
            .attr("aria-checked", self.element.get(0).checked);
            if (e) {
                e.stopPropagation();
            }
        },

        destroy: function () {
            var self = this, boxelement = self.element.parent().parent();
            boxelement.children("div." + self._csspre + "-box").remove();
            self.element.unwrap();
            self.element.unwrap();
            $.Widget.prototype.destroy.apply(self);
        }
    });
} (jQuery));

有人可以帮助我告诉我如何在小部件工厂创建的小部件上设置选项吗?我找不到任何可以给我建议的东西。

更新:

我发布了完整的代码。评论区都是我的评论。

I am trying to set the options for the almost UNDOCUMENTED checkbox widget from Wijmo. Here's the code for the widget:

(function ($) {
    "use strict";
    var checkboxId = 0;
    $.widget("wijmo.wijcheckbox", {
        _csspre: "wijmo-checkbox",
        _init: function () {
            var self = this,
                ele = self.element,
                o = self.options,
                checkboxElement, label, targetLabel, boxElement, iconElement;
            if (ele.is(":checkbox")) {
                if (!ele.attr("id")) {
                    ele.attr("id", self._csspre + checkboxId);
                    checkboxId += 1;
                }
                if (ele.parent().is("label")) {
                    checkboxElement = ele.parent()
                    .wrap("<div class='" + self._csspre + "-inputwrapper'></div>")
                    .parent()
                    .wrap("<div></div>").parent().addClass(self._csspre + " ui-widget");
                    label = ele.parent();
                    label.attr("for", ele.attr("id"));
                    checkboxElement.find("." + self._csspre + "-inputwrapper")
                    .append(ele);
                    checkboxElement.append(label);
                }
                else {
                    checkboxElement = ele
                    .wrap("<div class='" + self._csspre + "-inputwrapper'></div>")
                    .parent().wrap("<div></div>").parent()
                    .addClass(self._csspre + " ui-widget");
                }
                targetLabel = $("label[for='" + ele.attr("id") + "']");
//              if (targetLabel.length > 0) {
//                  checkboxElement.append(targetLabel);
//                  targetLabel.attr("labelsign", "C1");
//              }
                if (ele.is(":disabled")) {
                    self._setOption("disabled", true);
                }
                boxElement = $("<div class='" + self._csspre +
                "-box ui-widget ui-state-" +
                (o.disabled ? "disabled" : "default") +
                " ui-corner-all'><span class='" +
                self._csspre + "-icon'></span></div>");
                iconElement = boxElement.children("." + self._csspre + "-icon");
                checkboxElement.append(boxElement);
                ele.data("iconElement", iconElement);
                ele.data("boxElement", boxElement);

                boxElement.removeClass(self._csspre + "-relative")
                .attr("role", "checkbox")
                .bind("mouseover", function () {
                    ele.mouseover();
                }).bind("mouseout", function () {
                    ele.mouseout();
                });
                if (targetLabel.length === 0 || targetLabel.html() === "") {
                    boxElement.addClass(self._csspre + "-relative");
                }
                ele.bind("click.checkbox", function (e) {
                    self.refresh(e);
                }).bind("focus.checkbox", function () {
                    if (o.disabled) {
                        return;
                    }
                    boxElement.removeClass("ui-state-default").addClass("ui-state-focus");
                }).bind("blur.checkbox", function () {
                    if (o.disabled) {
                        return;
                    }
                    boxElement.removeClass("ui-state-focus").not(".ui-state-hover")
                    .addClass("ui-state-default");
                }).bind("keydown.checkbox", function (e) {
                    if (e.keyCode === 32) {
                        if (o.disabled) {
                            return;
                        }
                        self.refresh();
                    }
                });

                boxElement.bind("click.checkbox", function (e) {
                    ele.get(0).checked = !ele.get(0).checked;
                    ele.change();
                    ele.focus();
                    self.refresh(e);
                });

                self.refresh();
                checkboxElement.bind("mouseover.checkbox", function (e) {
                    if (o.disabled) {
                        return;
                    }
                    boxElement.removeClass("ui-state-default").addClass("ui-state-hover");

                }).bind("mouseout.checkbox", function (e) {
                    if (o.disabled) {
                        return;
                    }
                    boxElement.removeClass("ui-state-hover").not(".ui-state-focus")
                    .addClass("ui-state-default");
                });
            }
        },

        refresh: function (e) {
            var self = this;
            self.element.data("iconElement")
            .toggleClass("ui-icon ui-icon-check", self.element.get(0).checked);
            self.element.data("boxElement")
            .toggleClass("ui-state-active", self.element.get(0).checked)
            .attr("aria-checked", self.element.get(0).checked);
            if (e) {
                e.stopPropagation();
            }
        },

        destroy: function () {
            var self = this, boxelement = self.element.parent().parent();
            boxelement.children("div." + self._csspre + "-box").remove();
            self.element.unwrap();
            self.element.unwrap();
            $.Widget.prototype.destroy.apply(self);
        }
    });
} (jQuery));

Can someone help me by telling me how to set options on a widget created by the widget factory. I can find nothing out there that gives me advice.

Update:

I posted the complete code. The commented area are my comments.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮惑 2025-01-09 20:21:01

查看插件(小部件)的源代码,您似乎只有一个可配置的选项参数。它被称为 disabled 并且您可以这样使用它...

$(document).ready(function () {
    $(":input[type='checkbox']").wijcheckbox({
        disabled: true
    });
});

文档中可能没有提及,因为如果元素的 ,插件会自动将其设置为“true”禁用 属性已在其他地方设置。

if (ele.is(":disabled")) {
    self._setOption("disabled", true);
}

根据OP对原始帖子的评论进行编辑:

“...我检查了代码,我看到 o = self.options, checkboxElement,
label, targetLabel, boxElement, iconElement;
如果我知道如何设置这些我就可以了......”

逗号后面的项目列表不是 o< 的任何部分/code>、self.options 或任何用户可配置的选项或参数。它们只是插件内部的附加变量声明。

换句话说,这...

var self = this,
    ele = self.element,
    o = self.options,
    checkboxElement, label, targetLabel, boxElement, iconElement;

只是简写。更高效的代码)为此......

var self = this;
var ele = self.element;
var o = self.options;
var checkboxElement;
var label;
var targetLabel;
var boxElement;
var iconElement;

o是表示插件的选项或参数的变量。

Looking at the source code of the plugin (widget), it appears that you only have one configurable option parameter. It's called disabled and you could use it like this...

$(document).ready(function () {
    $(":input[type='checkbox']").wijcheckbox({
        disabled: true
    });
});

It's probably not mentioned in the documentation because the plugin sets it to "true" automatically if the element's disabled property is already set elsewhere.

if (ele.is(":disabled")) {
    self._setOption("disabled", true);
}

EDIT based on OP's comments on original posting:

"...I checked the code and I see o = self.options, checkboxElement,
label, targetLabel, boxElement, iconElement;
If I knew how to set these I'd be okay...."

The list of items after the comma are not any part of o, self.options, or any user configurable options or parameters. They are just additional variable declarations internal to the plugin.

In other words, this...

var self = this,
    ele = self.element,
    o = self.options,
    checkboxElement, label, targetLabel, boxElement, iconElement;

is simply shorthand (and more efficient code) for this...

var self = this;
var ele = self.element;
var o = self.options;
var checkboxElement;
var label;
var targetLabel;
var boxElement;
var iconElement;

o is the variable that represents the options or parameters for the plugin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文