JSF/PrimeFaces: Selection based visibility

94 Views Asked by At

I want to show / now show (for example) a text, based on a radio selection.
In an ajax style, no roundtrip to server.
How do I have to do this with JSF/PrimeFaces?

  <h:outputText value="Show some stupid Text." id="testShowText" />

  <p:selectOneRadio id="testRadio" widgetVar="testRadio" value="True" layout="grid" columns="1">
    <f:selectItem itemLabel="Show Text" itemValue="True"/>
    <f:selectItem itemLabel="Do NOT show Text" itemValue="False"/>
  </p:selectOneRadio>

UPDATE

I managed it in my test this way, but I am not sure, if this is the "best practice", I have some doubts; do I really need a managed bean for this?:

First I created a Bean to store the selected value:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class ShowTextBean {

    private boolean showBool  = false;
    private String showString = "False";
    
    public ShowTextBean() {
    }

    public boolean isShowBool() {
        return showBool;
    }

    public void setShowBool(boolean showBool) {
        this.showBool = showBool;
    }

    public String getShowString() {
        return showString;
    }

    public void setShowString(String showString) {
        this.showString = showString;
    }
}

Then I changed the Facelets Code to:

  <p:panel id="testShowTextPanel" style="display: #{showTextBean.showString eq 'False' ? 'none' : 'block'} ">
    <h:outputText value="Show some stupid Text." id="testShowText"/>
  </p:panel>

  <p:selectOneRadio id="testRadio" widgetVar="testRadio" value="#{showTextBean.showString}" layout="grid" columns="1">
    <f:selectItem itemLabel="Show Text" itemValue="True"/>
    <f:selectItem itemLabel="Do NOT show Text" itemValue="False"/>
    <p:ajax update="testShowTextPanel" />
  </p:selectOneRadio>
0

There are 0 best solutions below