Digma Chauhan
Digma Chauhan

Reputation: 195

how to render selected checkbox value in jsf when i select checkbox

i have renderd checkboxes. when i select checkbox that checkbox value shoud be displayed in panel.i have written code for this but not worked

                              <h:selectManyCheckbox id="chkedition" value="#{newspaperBean.selectedEditions}" layout="lineDirection" styleClass="nostyle">
                                  <f:selectItems value="#{newspaperBean.searchEditionByNewspaper()}" var="item" itemLabel="#{item.editionName}" itemValue="#{item.editionName}"/>
                                  <f:ajax render="display" event="select"/>
                              </h:selectManyCheckbox>
                          </td>
                        </tr>
                            <tr>
                                <td colspan="2">
                                    <p:panel id="display" header="You have selected">
                                    <c:forEach items="#{newspaperBean.selectedEditions}" var="it">
                                        <h:outputText value="#{it}"/><br/>
                                    </c:forEach>
                                    </p:panel>
                                </td>
                            </tr>

this worked when i submit form but i want that when i select checkbox.

Upvotes: 1

Views: 4218

Answers (1)

BalusC
BalusC

Reputation: 1108632

Remove the event="select" attribute. This is the incorrect event to hook on. It's only triggered when you select any text in input fields and textareas, not when you click the checkbox.

<f:ajax render="display" />

The default event is already valueChange which will resolve to click in checkboxes and this is exactly what you need. You can if necessary explicitly specify it by event="click", but as said, it's the default value already, so just omit it.

Another potential problem is the <c:forEach> in the panel. This will fail if the bean is view scoped because it runs during view build time and get its own separate instance of the view scoped bean which may not contain the submitted data. You'd need to use <ui:repeat> instead.

<ui:repeat value="#{newspaperBean.selectedEditions}" var="it">
    <h:outputText value="#{it}"/><br/>
</ui:repeat>

See also:

Upvotes: 1

Related Questions