Enabling and disabling components via select checkbox
I have component and I'm trying disable panelGrid below.
<h:selectBooleanCheckbox id="checkboxId" value="#{bean.checked}">
<p:ajax update="panelId" event="click"/>
</h:selectBooleanCheckbox>
<h:panelGrid id="panelId" rendered="#{!bean.checked}">
<h:outputLabel>some text</h:outputLabel>
<h:outputLabel>#registrationBB.registrationModel.homeAddress.actualAddressMathWithRegistration}</h:outputLabel>
</h:panelGrid>
As a result the clicking on checkbox doesn't take no effect. The check indicator doesn't even appear on checkbox component and value bean:checked doesn't sent to the server. I开发者_StackOverflow社区 tried to use also. Check indicator appeared but the panel is not refreshed
How to use update via checkbox right?
The example below is what I got to work:
<h:form>
<h:selectBooleanCheckbox id="checkboxId" value="#{indexBean.checked}" >
<p:ajax event="change" update="panelId" />
</h:selectBooleanCheckbox>
<h:panelGrid id="panelId" style="border:solid 1px black;" >
<h:outputLabel rendered="#{!indexBean.checked}" >some text</h:outputLabel>
<h:outputText rendered="#{!indexBean.checked}" value="some text 2" />
</h:panelGrid>
</h:form>
I had to change the <p:ajax>
event from click to change to get the checkbox working. The other issue is if you don't render the <h:panelGrid>
the id can not be found to update, so you want to move the rendered attribute to the components inside your <h:panelGrid>
but still update the <h:panelGrid>
.
A slight variation. Your backing bean field doesn't have to be a boolean. If you had a backing bean with fields:
private List<SelectItem> myStringList;
private String myString;
then you initialize myStringList like this before the form load:
myStringList = Arrays.asList(new SelectItem("one", "The Number One"),
new SelectItem("two", "The number two")
);
then you could do this:
<h:form>
<p:selectOneRadio id="ctlSearchType" value="#{mybean.myString}" layout="grid" columns="3">
<f:selectItems value="#{mybean.myStringList}" />
<p:ajax event="change" update="ctlone,ctltwo"/>
</p:selectOneRadio>
<h:panelGrid id="panelId" style="border:solid 1px black;" >
<p:outputLabel for="ctlone" value="Field one:"/>
<p:inputText value="#{mybean.whatever}" id="ctlone" size="8" maxlength="10" disabled="#{mybean.myString eq 'one'}"/>
<p:outputLabel for="ctltwo" value="Field two:"/>
<p:inputText value="#{mybean.whatevertwo}" id="ctltwo" size="8" maxlength="10" disabled="#{mybean.myString eq 'two'}"/>
</h:panelGrid>
</h:form>
精彩评论