Search code examples
jbossrichfacesseam

Seam/RichFaces: Rendering based on result of a JavaScript function or variable


I have a RichFaces component that I want to render after an Ajax call which sets a JavaScript variable to either true or false.

When the variable is false, I don't want the panel to render. Is there any way to input the result of this variable (or any JS function call) in the rendered attribute of a component?


Solution

  • Richfaces renders components on the server side. So you have to pass your parameter to server side. There are some ways to achieve this. Create a hidden input on the page and link it to a flag in your bean. Something like,

    class YourBean {
    
          private boolean visible = false;
    
          //getter,setter
    
    }
    

    On the page,

    <h:selectBooleanCheckbox id="hiddeninput" style="visibility:hidden" 
         value="#{yourBean.visible}"/> 
    <rich:component id="compid" rendered="#{yourBean.visible}" />
    
    <a:commandButton onclick="document.getElementById('hiddeninput').checked=true" 
         reRender="compid"/>
    

    Or create two methods which sets flag to true or false.

    class YourBean {
    
          private boolean visible = false;
    
          public void makeInvisible() {
               visible = false;
          }
    
          public void makeVisible() {
               visible = true;
          }
    
    }
    

    On the page,

    <rich:component id="compid" rendered="#{yourBean.visible}" />
    
    <a:commandButton action="#{yourBean.makeInvisible()}" reRender="compid"/>