如何禁用或启用一组组件 [英] How to Disable or Enable a group of Components

查看:81
本文介绍了如何禁用或启用一组组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是一次禁用或启用一组组件的任何方法. 例如:我想禁用整个窗体,该窗体又具有几个InputTexts,Dropdowns,..... 我希望能够一次禁用或启用所有这些功能.

Is the any way to Disable or Enable a group of components at once. For example : I want to disable an entire Form, which in turn have couple of InputTexts,Dropdowns,..... I want to be able to Disable or Enable all of them at once.

该怎么做?而不是使用布尔变量,而是向所有组件添加disable =#{boolean variable}. 还有其他方法可以将所有这些组件放到一个组件中并使其启用或禁用吗?

How to do it? Instead of using a boolean variable and adding disable="#{boolean variable} to all components. Is there any other way to put all of them in one component and make it enable or disable??

推荐答案

就像@Kukeltje在类似的问题中告诉我的那样:对于每个jsf的较新版本,请检查

As @Kukeltje has told me in a similar question: "For newer versions of every jsf, check massAttribute (from omnifaces)".

在我意识到自己使用自己的代码一次禁用一组组件之前. 这是我的代码,但是我建议像以前所说的去使用"massAttribute":

Before I realized that I used my own code to disable a group of components at once. Here is my code but I would recommend going for "massAttribute" as said before:

public class UtilsPrimefaces { 

/**
 * Disable all the children components
 * @param uiComponentName
 */
public static void disableUIComponent(String uiComponentName) {  
    UIComponent component = FacesContext.getCurrentInstance()  
            .getViewRoot().findComponent(uiComponentName);
    if(component!=null) {
        disableAll(component.getChildren());
    } 
}  

/**
 * Recursive method to disable the list
 * @param components Widget PD list
 */
private static void disableAll(List<UIComponent> components) {  

    for (UIComponent component : components) {  
        logger.info(component.getClass().getTypeName());            

        if (component instanceof InputText) {  
            ((InputText) component).setDisabled(true);

        } else if (component instanceof InputNumber) {  
            ((InputNumber) component).setDisabled(true);

        } else if (component instanceof InputTextarea) {  
            ((InputTextarea) component).setDisabled(true);

        }  else if (component instanceof HtmlInputText) {  
            ((HtmlInputText) component).setDisabled(true);

        }  else if(component instanceof SelectOneMenu) {  
            ((SelectOneMenu) component).setDisabled(true);

        } else if(component instanceof SelectBooleanCheckbox) {  
            ((SelectBooleanCheckbox) component).setDisabled(true);

        } else if(component instanceof CommandButton) {  
            ((CommandButton) component).setDisabled(true);              
        }
        disableAll(component.getChildren());  
    }  
} 

然后,您可以在豆子中使用它.这是一个页面的示例,该页面具有3个scrollPanels并只想禁用panel1和panel3:

Then you could use it in your beans. This is an example for a page that had 3 scrollPanels and wanted to disable only panel1 and panel3:

@PostConstruct
public void init() {        
    super.init();        
    Utils.disableUIComponent(":form:panel1");
    Utils.disableUIComponent(":form:panel3");
}

这是与相关的链接问题

这篇关于如何禁用或启用一组组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆