简体   繁体   English

完全禁用Jquery UI小部件

[英]Disable Jquery UI widget completely

How do I do such a thing? 我该怎么做? I need to disable the widget completely, meaning that all of the instances should be disabled and NO MORE instances can be created after the disabling. 我需要完全禁用该小部件,这意味着应禁用所有实例,并且禁用后不能再创建更多实例。 I tried searching, but nothing comes up. 我尝试搜索,但没有任何反应。

Will appreciate any help. 将不胜感激。

EDIT: 编辑:

A more lively example. 一个更生动的例子。

Say, I have three instances of my widget placed on three elements. 说,我在三个元素上放置了三个小部件实例。 Then I want to turn my widget off. 然后我想关闭我的小部件。 I invoke a static method turnOff, which leads to a) all working instances to be disabled b) prohibit any other instances of that widget to be created if they are later called via ajax ie Then I want it to work again, so i invoke a turnOn(). 我调用一个静态方法turnOff,这将导致a)禁用所有工作实例b)如果以后再通过ajax调用它们,则禁止创建该小部件的任何其他实例,即我希望它再次工作,因此我调用a打开()。

My widget is a hint pugin, so if the user switches hints off, they should be switched off everywhere, and there are places in the app where hinted parts of the page are still being loaded asynchronosly. 我的窗口小部件是提示pugin,因此,如果用户关闭了提示,则应该在所有地方都将其关闭,并且应用程序中的某些地方页面的提示部分仍会异步加载。

That's pretty much what I need to do. 那几乎就是我要做的。

The answer depends on if your widget is obvious when not active -- for example if it's a 'hint' type tooltip that shows on hover then all you need to do is not show the tool tip when it's inactive. 答案取决于您的窗口小部件在不活动时是否明显-例如,如果它是在悬停时显示的“提示”类型工具提示,那么您需要做的就是在不活动时不显示工具提示。 However if it also adds formatting to show where the hints are you need to undo that as well. 但是,如果它还添加了格式设置以显示提示位置,则也需要撤消该操作。

Let's take the simple case first -- suppose we have a simple widget filler that just adds a class ( filled ) when the mouse is over the element. 首先让我们以简单的情况为例-假设我们有一个简单的小部件filler ,当鼠标悬停在元素上时,它仅添加一个类( filled )。 Then just have a variable in global scope: 然后在全局范围内有一个变量:

enableFiller = true;

and then check that in your widget: 然后在您的小部件中进行检查:

$.widget( "spacedog.filler", {

    _create: function() {
        var progress = this.options.value + "%";
        this.element.hover(
            function() {
                if (enableFiller) { 
                    $(this).addClass("filled");
                }
            },
            function() {
                $(this).removeClass("filled");
            }
        );
    }

});

Fiddle . 小提琴

Note that I don't check the flag in the removeClass because the widget might be disabled while the mouse is over a widget and you probably don't want the hover color to accidentally 'stick on'. 请注意,我不检查removeClass的标志,因为在鼠标悬停在窗口小部件上时可能会禁用窗口小部件,并且您可能不希望悬停颜色意外地“粘上”。

From there it's pretty easy to extend to the case where your widget alters the element even when off. 从那里可以很容易地扩展到您的小部件即使在关闭时也更改元素的情况。 I'll do the logic as pseudo-code: 我将逻辑作为伪代码:

  • When creating the widget add a base-widget class to the element (even if inactive) 在创建窗口小部件时,将base-widget类添加到元素(即使处于非活动状态)
  • If the widget is on add the active-widget class and do whatever is required 如果窗口小部件已打开,请添加active-widget类并执行所需的任何操作

  • To turn off remove the active-widget class from all base widgets: $(".base-widget").removeClass('active-widget') . 要关闭,请从所有基本窗口小部件中删除active-widget类: $(".base-widget").removeClass('active-widget') You might also need to do some other changes to all the elements -- but they're easy to find. 您可能还需要对所有元素进行其他更改-但很容易找到它们。

  • To turn on add the active-widget class back and adjust everything else 要打开,请重新添加active-widget类并调整其他所有内容

You can embed the turnOn/turnOff functionality into the widget, as long as it uses the global variable as the flag. 您可以将turnOn / turnOff功能嵌入到小部件中,只要它使用全局变量作为标志即可。 See this answer for other options for storing the global: How to declare a global variable in JavaScript? 有关存储全局变量的其他选项,请参见此答案: 如何在JavaScript中声明全局变量?

If you get stuck you could post (a simplified version) of your widget code and what you've tried so far and someone can probably help further. 如果您遇到困难,则可以发布(简化版本)您的小部件代码以及到目前为止您已经尝试过的内容,然后有人可能会进一步提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM