1

I have a <p:selectOneRadio> with two <f:selectItem> and I have two <h:panelGrid>. I want to hide one of the <h:panelGrid> depending of the <f:selectItem> selected.

<p:selectOneRadio>
    <f:selectItem itemLabel="Hide pnl1" itemValue="1" />
    <f:selectItem itemLabel="Hide pnl2" itemValue="2" />
</p:selectOneRadio>

<h:panelGrid id="pnl1">
    //More stuff here...
</h:panelGrid>

<h:panelGrid id="pnl2">
    //More stuff here...
</h:panelGrid>

In this example how can i hide pnl1 if the option 1 is selected in the radio?

How can i hide pnl2 if the option 2 is selected in the radio?

Please show me the Bean code needed too.

Only one pnl should be showed

Kaz Miller
  • 949
  • 3
  • 22
  • 40

1 Answers1

4

You can do it through ajax.

<h:form id="frmPanels">
    <p:selectOneRadio id="sorPanelShow" binding="#{sorPanelShow}">
        <f:selectItem itemLabel="Hide pnl1" itemValue="1" />
        <f:selectItem itemLabel="Hide pnl2" itemValue="2" />
        <p:ajax update="pnlContainer" />
    </p:selectOneRadio>

    <h:panelGroup id="pnlContainer" layout="block">
        <h:panelGrid id="pnl1" rendered="#{sorPanelShow.value eq '2'}">
            //More stuff here...
        </h:panelGrid>

        <h:panelGrid id="pnl2" rendered="#{sorPanelShow.value eq '1'}">
            //More stuff here...
        </h:panelGrid>
    </h:panelGroup>
</h:form>

Note that you have to use a <h:panelGroup> to wrap the <h:panelGrid> to re-render. This is because each non-rendered UIComponent (rendered="false") won't be part of the tree component stored in the view and won't be able to be updated by ajax operations. So, instead rendering each <h:panelGrid> on their own, it is better to update the wrapper UIComponent. Also, <h:panelGroup layout="block"> will use a <div> when generating the HTML.

Associated info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332