简体   繁体   中英

Commandbutton works only on second click until a <h:selectBooleanCheckbox> on a HashMap is removed

I'm working with JSF 2.2.9, i have the following dataTable and buttons:

<h:commandButton id="commandButtonRemoverSelected"
                        actionListener="#{managedBeanName.removeSelected()}"
                        class="btn btn-primary" value="Sim">
                    </h:commandButton>

    <h:dataTable var="bean" value="#{managedBeanName.beans}" styleClass="table table-hover" 
            binding="#{managedBeanName.dataTable}">
                <h:column headerClass="smallColumn">
                    <f:facet name="header">
                        <h:selectBooleanCheckbox valueChangeListener="#{managedBeanName.selectAll}">
                            <f:ajax execute="@form" render="@all" />
                        </h:selectBooleanCheckbox>
                    </f:facet>
                    <h:selectBooleanCheckbox value="#{managedBeanName.registrosSelecionados[bean]}" />
                </h:column>

            </h:dataTable>

The button "commandButtonRemoverSelected" just call actionListener on second click. When i remove the following line from datatabe everything works fine (the commandButton is called on first click):

<h:selectBooleanCheckbox value="#{managedBeanName.registrosSelecionados[bean]}" />

So, my managedBean have a MAP called 'registrosSelecionados' that should store a pair "Bean,Boolean". See:

private Map<Bean, Boolean> registrosSelecionados = new HashMap<Bean, Boolean>();

SOLUTION

I solved the problem with a workaround and i would share this:

1 - I noted that every first commandButton click the 'getRegistrosSelecionados()' is called and actionListener don't. So i imagine that JSF is processing getRegistrosSelecionados() and don't processing actionListener, i don't know why and this is the real problem. In second click the actionListener is processed because all checkboxs already sent to ManagedBean, in first click.

2 - So, my workaround is force that each click in checkbox call JSF request and don't wait for commandButton submit. In this way when commandButton is clicked all checkbox was processed.

<h:selectBooleanCheckbox value="#{cc.attrs.managedBeanName.registrosSelecionados[bean]}">
                  <f:ajax execute="@this" render="@this" />
                </h:selectBooleanCheckbox>

My checkbox received the ajax to execute only itself and render itself, when i click in commandButton i just need execute the commandButton and render the form again:

<h:commandButton id="commandButtonRemoverSelected"
                        actionListener="#{cc.attrs.managedBeanName.removeSelected()}"
                        class="btn btn-primary" value="Sim">
                        <f:ajax execute="@this" render="@form" />
                    </h:commandButton>

This workaround works for me, as i didn't found a better solution for this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM