0

I'm new to JSF and Primefaces and wanted to know what the best solution is to render a component according to radio selection (hiding and showing again)? I posted my ManagedBean and my JSF-Page-Excerpt. Thanks in advance.

My JSF-Page:

<p:dialog widgetVar="komponentErstellenDialogWV" modal="true"
            id="komponentErstellenDialog" header="Komponente erstellen">
            <h:form>
                <p:wizard flowListener="#{userWizard.onFlowProcess}">
                    <p:tab id="produktAuswahlTab" title="Produkt auswählen">
                        <p:panel>
                            <h:panelGrid columns="2" columnClasses="label, value">
                                <h:outputText value="Produkt:" />
                                <p:selectOneRadio id="produktAERadio"
                                    value="#{komponenteErstellenWizardController.produktAuswahl}">
                                    <f:selectItem itemValue="1" itemLabel="Neu erstellen" />
                                    <f:selectItem itemValue="2" itemLabel="Aus Liste auswählen" />
                                    <p:ajax event="click" update="produktSelect" />
                                </p:selectOneRadio>
                                <p:selectOneMenu id="produktSelect"
                                    rendered="#{komponenteErstellenWizardController.shouldRenderProduktSelect()}"
                                    value="#{komponenteErstellenWizardController.komponente.produkt}">
                                    <f:selectItems
                                        value="#{komponenteErstellenWizardController.findAllProdukt()}"
                                        var="currentProdukt"
                                        itemLabel="#{currentProdukt.hersteller.concat(' ').concat(currentProdukt.name)}"
                                        itemValue="#{currentProdukt.id}" />
                                </p:selectOneMenu>
                            </h:panelGrid>
                        </p:panel>
                    </p:tab>
                </p:wizard>
            </h:form>
        </p:dialog>

My ManagedBean:

@ManagedBean
@SessionScoped
public class KomponenteErstellenWizardController implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    // Properties
    private Komponente komponente = new Komponente();
    private String produktAuswahl;

    @PostConstruct
    public void init() {
        produktAuswahl = "1";
    }

    public String getProduktAuswahl() {
        System.out.println("GetProduktAuswahl invoked with returning value: " + produktAuswahl);
        return produktAuswahl;
    }

    public void setProduktAuswahl(String produktAuswahl) {
        System.out.println("SetProduktAuswahl invoked with Value: " + produktAuswahl);
        this.produktAuswahl = produktAuswahl;
    }

    public Komponente getKomponente() {
        return komponente;
    }

    public void setKomponente(Komponente komponente) {
        this.komponente = komponente;
    }

    // EJBs
    @EJB
    KomponenteFacade komponenteFacade;

    @EJB
    ProduktFacade produktFacade;

    @EJB
    ProduktArtFacade produktArtFacade;

    public List<Produkt> findAllProdukt() {
        return produktFacade.findAll();
    }

    public Boolean shouldRenderProduktSelect() {
        System.out.println("Wizard Produktauswahl: " + produktAuswahl);
        return "2".equals(produktAuswahl);
    }
}

I updated my code according to suggestions:

  • Replaced faces components by primefaces components
  • Added a new action method "shouldRenderProduktSelect"

Still not working...

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
LupoZ
  • 191
  • 1
  • 1
  • 12

1 Answers1

0

Few things..

1) Add this method to your controller:

public boolean shouldRenderSelect(){
   return "2".equals(produktAuswahl);
}

2) Change the rendered attribute:

<h:selectOneMenu id="produktSelect"
        rendered="#{komponenteErstellenWizardController.shouldRenderSelect()}"

3) update your html:

<p:selectOneRadio id="produktAERadio"
                                    value="#{komponenteErstellenWizardController.produktAuswahl}">
                       <f:selectItem itemValue="1" itemLabel="Neu erstellen" />
                       <f:selectItem itemValue="2" itemLabel="Aus Liste auswählen" />
                       <p:ajax event="click" update="panelSelect" />
       </p:selectOneRadio>
 <h:panelGroup id="panelSelect"> 
   <p:selectOneMenu id="produktSelect"
                rendered="#{komponenteErstellenWizardController.shouldRenderProduktSelect()}"
    ... 
   </p:selectOneMenu>
 </h:panelGroup>   

The key is to wrap the selectOneMenu within and from ajax update the panelGroup, not the menu.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
  • Thanks for suggestion but i dont ahve runtime currently and i felt quite confident about the answer. Why do you think the answer is invalid? – Maciej Kowalski Feb 01 '17 at 11:35
  • I updated my code according to suggestions: Replaced faces components by primefaces components and Added a new action method "shouldRenderProduktSelect" still not working... The selectOneMenu isn't shown... – LupoZ Feb 01 '17 at 11:39
  • Ok answer updated. If you think it brings value i would appreciate you voting it up of accepting it. Thanks – Maciej Kowalski Feb 01 '17 at 11:54
  • 1
    Please search stackoverflow for duplicate answers. Saves you creating one and it keeps it more clean. – Kukeltje Feb 01 '17 at 12:41
  • One post cannot be the ultimate answer to a problem. Each has its own unique details and i have addressed those aditionally. – Maciej Kowalski Feb 01 '17 at 13:02
  • @Kukeltje I'm so sorry when it ended in a duplicate question... If my question was concrete and I knew that rendered was the right hint to toggle components on and off I would have found a solution by now. The directed solution is not for primefaces but similar. – LupoZ Feb 01 '17 at 13:25