How to Disable or Enable a group of Components

二次信任 提交于 2020-01-03 01:51:33

问题


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.

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??


回答1:


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

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());  
    }  
} 

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");
}

This is the link to the related question




回答2:


There are a lot of ways to do that. If you are using primefaces, you can use <p:blockUI /> component. You can customize the css styles for "disable area".

Check out the official demo: https://www.primefaces.org/showcase/ui/misc/blockUI.xhtml




回答3:


If you just need to make them non-clickable maybe javascript/jquery suffices? Check out this answer to disable them with jquery.




回答4:


There are a few different approaches I've seen mentioned:

Add the disable attribute when the SystemEvent is thrown: https://stackoverflow.com/a/15031242/1981358

Create a custom component to wrap around your form fields: https://stackoverflow.com/a/11453029/1981358 https://stackoverflow.com/a/9543826/1981358

As rags mentions, PF blockUI could be used especially if you need to restrict access during client side event handling.

Similarly, you could insert your own custom "glass" over the page with jQuery which would prevent any clicks (and really annoy your users).



来源:https://stackoverflow.com/questions/16011481/how-to-disable-or-enable-a-group-of-components

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!