Primefaces - Ajax - Select Components - jsf-2

I'm using this primafaces example http://www.primefaces.org/showcase/ui/pprSelect.jsf,
but I'm having problem when I put the first p:selectOneMenu as required true, it isn't clean the second p:selectOneMenu when I choose the first option again.
Regards

What happens here is that when you try to "unselect" the first p:selectOneMenu, you end up triggering a validation rule.
You can do what you want by using two remoteCommand tags and JavaScript.
<p:remoteCommand name="makeSelection" process="select" update="suburbs" />
<p:remoteCommand name="clearSelection" process="#this" update="suburbs" >
<f:setPropertyActionListener value="#{null}" target="#{pprBean.city}" />
</p:remoteCommand>
Now you can decide which one to call using a javascript funcion
<p:selectOneMenu id="city" required="true" value="#{pprBean.city}" onchange="selectFunction(this)">
...
function selectFunction(el){
//if el.value is empty you call clearSelection();
//else you call makeSelection();
}
Another common solution is using a commandButton that clears the selection calling something like:
<p:commandButton update="suburbs" (...)>
<f:setPropertyActionListener target="#{pprBean.city}" value="#{null}" />
</p:commandButton>

Related

ignoreValidationFailed doesn´t work inside ui:repeat

I´m using o:ignoreValidationFailed, but it doesn´t work inside the ui:repeat. When I do the same outside, it works! I tried with mojarra ans MyFaces... I´m using primefaces. If there is another way to skip the validations only for one button...
<o:form id="rateplanEditByPeriod" prependId="false">
<p><p:messages id="mensagensDlg"/></p>
<p:tabView id="tabs">
<p:tab title="Cancelamento" id="tabCanc">
<h:panelGrid id="cancelationsTable" columns="2" cellpadding="10px" columnClasses="alignTop,alignTop">
<ui:repeat id="repeat" var="rest" value="#{rateplanByPeriodManaged.rateplanByPeriod.restriction.restTypeCancelation.restTypeCanConfs}" >
<h:panelGrid columns="8">
<p:inputText id="penaltyValue_#{loop.index}" value="#{rest.penalityValue}" style="width:28px" label="Valor" title="Valor" disabled="#{rest.noCancel}" required="true"/>
<p:commandLink id="add_#{loop.index}" actionListener="#{rateplanByPeriodManaged.addCancConf}" update=":rateplanEditByPeriod:tabs:cancelationsTable" partialSubmit="true" process=":rateplanEditByPeriod:tabs:cancelationsTable" value="+">
<o:ignoreValidationFailed />
</p:commandLink>
<p:commandLink actionListener="#{rateplanByPeriodManaged.removeCancConf(rest)}" value="-" update=":rateplanEditByPeriod:tabs:cancelationsTable" partialSubmit="true" process=":rateplanEditByPeriod:tabs:cancelationsTable">
<o:ignoreValidationFailed />
</p:commandLink>
</h:panelGrid>
</ui:repeat>
</h:panelGrid>
</p:tab>
</p:tabView>
<p:commandLink styleClass="button" onclick="dlgEdit.hide()" immediate="true" update=":msgsPanel">#{msgs['inventory.editByPeriod.cancel']}</p:commandLink>
<p:commandLink styleClass="button" actionListener="#{rateplanByPeriodManaged.editByPeriod(loginManaged.hotelSelected)}" oncomplete="if (!args.validationFailed) {dlgEdit.hide(); updateAllSearches(); updateAllNotifications();}" update="mensagensDlg, tabs" >#{msgs['inventory.editByPeriod.confirm']}</p:commandLink>
</o:form>
I have faced the same issue but with p:datatable
I solved it with
1- add a condition to the required field to know if the ajax come from submit button or not
as #Camilla said.
required="#{!empty param['trans_desc_form:savetransid']}"/>
trans_desc_form is the entire form id and savetransid is the submit button save id
2- I removed #NotNull from my JPA entity which force the validation
#JoinColumn(name = "ITEMNO", referencedColumnName = "ITEMNO")
#ManyToOne(optional = false, fetch = FetchType.LAZY)
//#NotNull
private Item item;
I have created a question for this issue
ignoreValidationFailed doesn´t work inside p:dataTable
I don´t know if it´s the best aproach but it worked for me...
<h: inputText id="text1" value="" required="#{!empty param['formName:btnSave']}" />
This is not the right way to have a "Cancel" button.
Just put process="#this" in the <p:commandLink>, or if you don't need to do any business logic, make it a normal <h:link> which reloads the page (and thus implicitly recreates the request/view scoped bean).
As to <o:ignoreValidationFailed> the failure in <ui:repeat> on its own, please create an issue.

How to refresh page in JSF on selectOneMenu selection?

I have a page containing a PrimeFaces (2.2.1) Editor component, a Refresh button and a selectOneMenu, whose selection affects the contents of the Editor, as follows:
<p:editor id="uploadedText" value="#{facilityDataUploadBean.uploadedText}"
width="600" height="180" disabled="true" controls="" />
<h:commandButton value="Refresh" immediate="true" />
<h:selectOneMenu id="skipLines" styleClass="dropdown"
value="#{facilityDataUploadBean.skipLines}">
<f:selectItems value="#{facilityDataUploadBean.skipLinesList}" />
<f:ajax listener="#{facilityDataUploadBean.importParameterChanged}" />
</h:selectOneMenu>
facilityDataUploadBean.importParameterChanged updates facilityDataUploadBean.uploadedText. After changing the selectOneMenu value, the operator presses the Refresh button to refresh the page, including the contents of the p:editor. (I cannot simply refresh the p:editor using AJAX, because it doesn't re-render correctly, at least in PF 2.2.1.)
It seems like I ought to be able to accomplish the page refresh automatically when the selectOneMenu value is changed, but I've been unable to come up with a combination of attributes and events that will do that. I've tried various combinations of onchange="submit();", immediate="true" and valueChangeListener on the selectOneMenu, as well as execute="#all/#form", render="#all/#form" on the f:ajax event, all to no avail. My current workaround is to display a message asking the user to press the Refresh button whenever they change the selectOneMenu selection - pretty hokey.
Invoke window.location.replace(window.location.href) rather than submit() in the onchange event, as in:
<h:selectOneMenu id="skipLines" ... onchange="window.location.replace(window.location.href);">
<f:selectItems ... />
<f:ajax ... />
</h:selectOneMenu>
please try the onchange="window.location.reload();" approach like:
<h:selectOneMenu id="skipLines" ... onchange="window.location.reload();">
<f:selectItems ... />
<f:ajax ... />
</h:selectOneMenu>
This works just fine in my environment (GF 3.1.1, PF 3.2) but please be aware that there is the possibility of interrupting some ajax functinality.
Hope this helpes, have Fun!

Create new data objects with a popup panel fails

Here I am again with a JSF problem, this time it is more about richfaces (4.2.1 final, JSF 2). I have an eytended DataTable and want to add new items. There ist the List of Journey-objects, every attribute have getter and setter. The table is created well (there are already some items in the list). And now I click the "add new item" button:
<a4j:commandButton ajax="true" value="Neue Fahrt hinzufügen"
actionListener="'{editorBean.prepareCreateJourney}" render="createJourneyPopupForm"
oncomplete="#{rich:component('createJourneyPopup')}.show()" />
The editorBean is SessionScoped, the method is properply called and create a new object of Journey called currentJourney. A popup comes up and should allow me to insert some input:
<rich:popupPanel id="createJourneyPopup" modal="true">
<h:panelGrid id="createJourneyPopupForm" columns="3">
<h:outputLabel for="currentId" value="ID" />
<h:inputText id="currentId"
value="#{editorBean.currentJourney.journeyNumber}" />
<rich:message for="currentId" />
[...and more...]
</h:panelGrid>
<a4j:commandButton value="Save"
execute="currentId currentTrainType operator">
<f:ajax event="click" listener="#{editorBean.doCreateJourney}"
render=":vmsEditorForm:resultTable" />
<rich:componentControl target="createJourneyPopup" operation="hide" />
</a4j:commandButton>
<a4j:commandButton value="Abbrechen"
onclick="#{rich:component('createJourneyPopup')}.hide();" />
</rich:popupPanel>
When i clicked the button, the method doCreateJourney is called, but the currentJourney is empty. It is the default Journey object after creating it. No setter was called, do a hint on the inputfields.
I checked the names twice, there are getters and setters for alle needed objects. I tried to change the scope, but no effect. Objects are the same, in the prepare method i created Journey(id=151) and in the save method it is still Journey(id=151).
What is going on here? I can't be this difficult to make some dialogs to create and edit data objects. Thank you for listening and for your help!
You need to wrap both your inputs and your commandButtons in a form in order to correctly submit the data.
<rich:popupPanel id="createJourneyPopup" modal="true">
<h:form>
... the content of your popupPanel
</h:form>
</rich:popupPanel>
Regards,

page navigation not occuring after enabling button from row select using ajax

I am using JSF 2.0 and primefaces 3.0
I am trying to enable a command(Submit) button after a row select from datatable
<p:datatable ....>
<p:ajax event="rowUnselect" onstart="showImageButton.disable();" />
<p:ajax event="rowSelect" onstart="showImageButton.enable();" />
</p:datatable>
<p:commandButton id="commandbuttonid" widgetVar="showImageButton" action="#{bean.methodreturningapage}" value="Submit"
ajax="false" disabled="true" />
after selecting the rows command button gets enabled but the action method is not firing.
Change the disabled attribute into something like #{bean.buttonDisabled}
take a look on this question
p:commandButton not working when disable=“true” initially
and this one
Re-enabled p:commandButton not firing ajax

rich:componentControl and setPropertyActionListener wont work together

<h:commandLink action="#" value="some value">
<f:setPropertyActionListener target="#{someBean.id}" value="#{var.id}"/>
<rich:componentControl target="popup" operation="show" />
</h:commandLink>
Anybody has idea why this composition wont work. To be precise property action listener
does not do a job, and popup panel was shown. When I do something like this
<h:commandLink action="#{someBean.someAction}" value="some value">
<f:setPropertyActionListener target="#{someBean.id}" value="#{var.id}"/>
</h:commandLink>
property action listener works fine.
Any idea?
It will work fine if you use a4j:CommandButton and use oncomplete attribute of this tag and invoke
oncomplete=" #{rich:component('panelid')}.show();"

Resources