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

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.

Related

inputText enter causes page refresh

I have several inputText boxes on a page with associated edit/submit buttons (which work). I want to make it so that when the user presses enter from inside the inputText box, the form is submitted. For one of my forms, it works, for the other, it doesn't. Here's the one that doesn't work:
<h:form>
<iframe src="#{appInfo.documentation}"/>
<h:commandButton value="Edit" >
<f:ajax listener="#{appInfo.editDoc}" render="#form"/>
</h:commandButton>
<h:commandButton value="Save" >
<f:ajax listener="#{appInfo.saveDoc}" render="#form"/>
</h:commandButton>
<h:inputText value="#{appInfo.documentation}" >
<!-- pressing enter causes page refresh -->
<f:ajax/>
</h:inputText>
</h:form>
And the one that does:
<h:form>
<h:dataTable id="table" value="#{appInfo.model.apps}" var="a">
<h:column>
<f:facet name="header">URL</f:facet>
<h:outputText value="#{a.url}"/>
<h:inputText value="#{a.url}">
<!-- pressing enter does NOT causes page refresh -->
<f:ajax/>
</h:inputText>
</h:column>
</h:dataTable>
... command buttons
</h:form>
Any ideas?
Pressing "Enter" in an input field fires the same behaviour like clicking the first button of that form. In your second example there is no button at all. As soon as you add e.g. a commandButton there (like in the first example), the enter will be executed like expected.

Ajax render resets selected checkboxes

I have a list of objects with a boolean property. I'm using a list of checkboxes to select/deselect that property for every object in the list. My problem is that after rerendering the form, the already selected checkboxes appear empty. Why is that?
<h:form>
<ui:repeat value="#{bean.employeeList}" var="employee">
<h:selectBooleanCheckbox value="#{employee.selected}">
<h:outputText value="#{employee.name}" />
</h:selectBooleanCheckbox>
</ui:repeat>
<h:selectBooleanCheckbox value="#{bean.isDone}">
<f:ajax event="click" render="#form" />
</h:selectBooleanCheckbox>
</h:form>
The <f:ajax> processes by default the current component, as in execute="#this". You need to explicitly specify the entire form by #form if you intend to process the entire form.
<f:ajax execute="#form" render="#form" />
Note that I removed event="click". This is namely the default already.

JSF commanbutton refreshes the page and popup menu disappears immediately

I am trying to show a popup menu in by checking some conditions, such as if something is not selected show popup etc. and if everyhing is fine submit the page. Here is the code:
<h:commandButton onclick="if (#{moneyTransferManager.showPopup()})
#{rich:component('popup')}.show();" value="Devam" >
<rich:componentControl target="popup" operation="show" />
</h:commandButton>
<rich:popupPanel id="popup" modal="false" autosized="true" resizeable="false">
<f:facet name="header">
<h:outputText value="Simple popup panel" />
</f:facet>
<f:facet name="controls">
<h:outputLink value="#" onclick="#{rich:component('popup')}.hide();
return false;">
X
</h:outputLink>
</f:facet>
<p>
The popup panel is open and closed from the javascript function of component client side object. The following code
hide this panel:
<f:verbatim>#</f:verbatim> #{rich:component('popup')}.hide()
</p>
</rich:popupPanel>
then after button is clicked, popup shows for like a second and then page is refreshed and popup is lost. To prevent refreshing page i tried:
<f:ajax execute="#form" render="#none" />
but then if everything is right and if i don't need a popup i need to submit the page by commandbutton, but since i say render=#"none" i cannot do this. So what can i do about this situation?
Thanks
<h:commandButton> has a default button type submit which submits enclosing h:form when it is clicked. That is why the page refreshes when you click.
<h:button> should be used for java script usage.In addition, <h:commandButton> becomes <h:button> if type attribute is set to button.
<h:button onclick="if (#{moneyTransferManager.showPopup()})
#{rich:component('popup')}.show();" value="Devam"/>
Or
<h:commandButton type="button" onclick="if (#{moneyTransferManager.showPopup()})
#{rich:component('popup')}.show();" value="Devam" >
See Also :
Difference between h:button and h:commandButton

Primefaces dialogbox popup refreshes the background and disapears

I am using primefaces dialogbox for popup purpose. But everytime it gets opened the whole screen get refreshed automatically and the popup disappears.
< p:dialog id="dialog" header="Select different user" styleClass="atf-header" widgetVar="dlg" appendToBody="true">
<ui:include src="searchpopup.xhtml" />
</p:dialog>
<h:panelGroup>
<h:outputLabel value="#{I18N['Create_Ticket_for_other_users']}" styleClass="atf-header" style="width:600px"></h:outputLabel>
</h:panelGroup>
<h:panelGroup id="search_section" layout="block"
styleClass="atf-content-area atf-separarot-botton" style="width:600px">
<h:panelGroup id="input_search_section" >
<h:outputText id="name" value="Siddharth Mishra"
labelStyleClass="atf-label">
</h:outputText>
</h:panelGroup>
<h:panelGroup styleClass="atf-right atf-inline-block">
<p:commandButton id="btn_search" value="select different user"
styleClass="atf-button-search" onclick="dlg.show()">
</p:commandButton>
</h:panelGroup>
</h:panelGroup>
You p:commandButton is AJAX button (which is default in primefaces) which submits whole form. Add type="button" attribute to it so it will be just ordinary button which do some JavaScript (so called push button). Also I don't see where is h:form tag here. As you have appendToBody="true" in you p:dialog be shore that you don't encapsulate p:dialog inside h:form. You should have h:form inside p:dialog if it is necessary, and if it is not move p:dialog outside of h:form.

JSF/Primefaces- Action listener on Command button not getting called

I am using JSF2.0 with Prime Faces 3.2. I have a data table with a column that has two command buttons. The second command button is rendered based on a condition. Everything works well until the data table is filtered. Once filtered the first command button without the rendered attribute calls my actionListener without any issues, whereas the second command button with the rendered attribute does not.
PS:The reason i am using f:param intead of setPropertyActionListener is because of a bug in primefaces datatable that passes the wrong row's id when filtered.
Here is my code, any insight into this issue would be great.
<p:column headerText="Column1" filterMatchMode="contains" filterBy="#{item.name}" sortBy="#{item.name}">
<h:outputText value="#{item.name}" style="float:left" />
<span class="inlineButton">
<p:commandButton id="selectBtn" icon="select"
oncomplete="dialog1.show()" actionListener="#{controller.setItem}">
<f:param name="itemId" value="#{item.id}"/>
</p:commandButton>
<p:commandButton id="delBtn" icon="delete"
actionListener="#{controller.setItem}"
oncomplete="delConfirm.show()"
rendered="#{not empty item.id}">
<f:param name="itemId" value="#{item.id}"/>
</p:commandButton>
</span>
</p:column>

Resources