Update not understood - jsf

I have a p:selectOnelistBox component and a p:commandButton within a p:form component!
Initially the button is disabled, and I want to enable the button when the user selects an item from the list!
I've the following inside the p:selectOneListBox component to set the disabled flag to false using the projectPageBean (ViewScoped) valueChanged method:
<p:ajax event="change" listener="#{projectPageBean.valueChanged}" update="#this"/>
The p:commandButton component looks like:
<p:commandButton id="test"
value="View Instrument"
rendered="#{projectPageBean.rendered}"
disabled="#{projectPageBean.disabled}"
action="#{projectPageBean.getPage}"
update="#this,:add-instrument-dialog-form:scrolladd"/>
But this doesn't seem to work!? What am I doing wrong?
Regards

Looks like you forgot to update your <p:commandButton from your p:selectOneListBox ajax
So change your <p:ajax update="#this".... to <p:ajax update="#form" so it will update your button, you also can place your button id or some other id of that button wrapper (instead of the "#form")

Related

Primefaces - Customize p:confirmDialog content conditionally

I have a list of objects (let's call each object a record object), shown through a <p:datatable> component. Each record has a delete button column.
When a user clicks on the delete button of the record, a <p:confirmDialog> is shown, asking for user confirmation. What I want is to customize the content of the confirmDialog under corresponding circumstances (for example show/do not show a checkbox concerning the value of a property of the record, f.e if record.isPersonal, show the checkbox, else not.
Unfortunately, that does not seem to be working as the checkbox is always shown in case the first record satisfies the condition, and the opposite in case it does not. After some research I found out that especially in previous Primefaces versions, they used to use the "JS way" (creating two separate confirm dialogs and proportionally use PF('widgeName').show(), but I would like to know whether any out of the box solution exists in Primefaces 7.0 version which I use through the <p:confirm> tag or something else. Code example below:
<p:dataTable id="recordsTable" lazy="true" value="#{myBean.myList.records}" var="record...">
<p:column styleClass="deleteColumn">
<p:commandButton ...>
<p:confirm .../>
</p:commandButton>
<p:confirmDialog widgetVar="delete_record_dialog" global="true" showEffect="fade" hideEffect="fade">
<p:selectBooleanCheckbox
rendered="#{record.isPersonal}"
....>
</p:selectBooleanCheckbox>
<p:commandButton value="#{msg.yes}" type="button"
styleClass="ui-confirmdialog-yes" icon="pi pi-check"
/>
<p:commandButton value="#{msg.no}" type="button" styleClass="ui-confirmdialog-no"
icon="pi pi-times"
/>
</p:confirmDialog>
</p:column>
</p:dataTable>
Thanks in advance!
I think rather than using ConfirmDialog, you may have to revert to building you own custom dialog, either using p:dialog or using the dialog framework. Using dialog framework you can create a simple confirm dialog box and can pass the data into the dialog programatically - e.g. a flag based on the current row to indicate if the checkbox should be shown. The dialog framework also gives an easy way to return data back from the dialog to the calling page using the dialogReturn ajax event.
You could use the <p:confirmDialog message=""/> to conditionally display different messages like this:
<p:confirmDialog widgetVar="delete_record_dialog"
header="Record delete"
message="Are you sure you want to delete #{record.isPersonal ? 'your personal' : 'this'} record?">
<h:form id="recordDeleteForm">
<p:commandButton value="#{msg.yes}" update=":tableForm"
oncomplete="PF('deleteDialog').hide(); PF('recordsTableWidgetVar').filter()"/>
<p:commandButton value="#{msg.no}" type="button"
onclick="PF('delete_record_dialog').hide()"/>
</h:form>
</p:confirmDialog>
If you want to fit in more content then you could use <f:facet name="message">...</f:facet/>
See also:
<p:confirmDialog> with a parameter message

jsf valuechangelistener not firing on checkbox deselect

I have a primefaces checkbox on a form with a valuechangelistener. The backing bean method gets called on checkbox select, but doesn't get called on deselect.
I'm using the same technique with almost identical code elsewhere in my application and it works fine on both select and deselect.
<p:selectBooleanCheckbox value="#{shareoutSlip.processed}"
valueChangeListener="#{orderFormBean.handleShareoutSlipProcessed}" >
<p:ajax />
</p:selectBooleanCheckbox>
Here is the valueChangeListener method (all it does for now is print a message to the console):
public void handleShareoutSlipProcessed(ValueChangeEvent vcEvent) {
logger.info("Shareout Slip Processed");
}
Is there anything else on my form that could be interfering with the method being called on checkbox deselect? I've put a h:messages tag on the form but I'm not seeing any validation messages.
Have you tried to call this method inside p:ajax tag?
<p:selectBooleanCheckbox value="#{shareoutSlip.processed}">
<p:ajax event="change" listener="#{orderFormBean.handleShareoutSlipProcessed}" />
</p:selectBooleanCheckbox>

p:commandButton click updates h:form unwantedly

<p:commandButton id="excelAccountLevelOneAccLvl1" ajax="false" icon = "fa fa-fw fa-download" >
<f:param name="accountLevelOneFormRequest" value="accountLevelOneFormRequest" />
<p:dataExporter type="xlsx" target="baselineOneTable"
fileName="#{exportToExcelPageBean.fileName}"
postProcessor="#{exportToExcelPageBean.postProcessXLS}" />
</p:commandButton>
On clicking this button, somehow the forms seems to update and it activates the Faces validation and asks me to fill enter the mandatory field values! I can't figure out why! There is not update parameter here at all!
update is for ajax requests only. You are using ajax="false" which means the commandButton activates a
full page request. That in turn means that the whole form in which the commandButton is included is
processed. If you want to avoid this put your commandButton in a separate form.

JSF inputText reads value only with required="true"

Can anyone explain why h:inputText must have required="true" when setting the property in controller and updating with ajax(See example below)?
Does not work:
<h:inputText id="textFieldId" value="#{model.itemValue}">
Works:
<h:inputText id="textFieldId" value="#{model.itemValue}" required="true">
Action:
<p:commandLink value="edit">
<p:ajax event="click" listener="#{controller.edit(item)}" process="#this" update="#form"/>
</p:commandLink>
Idea behind is that I want to press button for item and be able to edit so I need to propagate this item to inputText.
I dont see any reason for having required set to true.
Thanks
The PrimeFaces p:commandLink is by default already ajax enabled, so there is no need to nest a p:ajax tag inside it.
<p:commandLink value="edit" actionListener="#{controller.edit(item)}"
process="#this" update="#form"/>
But keep in mind that if you add a process="#this", the input is not processed on the server, just the commandLink. If the 'item' field is passed correctly in this case is unclear to me.

Primefaces process attribute in reseting form inputs

I have a form inside a modal dialog and after closing (hiding in fact) one I wanted to reset all inputs that user might have changed. I though about something like as follow:
<p:dialog widgetVar="myDialog">
<h:form id="formId">
<!-- ... -->
<p:commandButton value="Cancel" onclick="myDialog.hide();"
update="formId">
<p:resetInput target="formId" />
</p:commandButton>
</h:form>
</p:dialog>
But the result was not that I expected. After a while of searching I found a solution that was to add process="#this" attribute to the <p:commandButton>. And my question is why it is necessary? What is really happening in backgroud that this process is desired. I don't really get the idea of process attribute at all.
I have done some work with dialog boxes and the way I did to make the form null is, when clicking the button to open dialog box, I ran a method in backing bean which cleared my pojo so my form had empty values.
In your case it could be something like this:
<h:form id="form-button">
<p:commandButton id="AddButton" value="open dialog box"
update=":form" action="#{myBean.myMethodToSetPojoNull}" immediate="true"
oncomplete="PF('myDialog').show()" />
</h:form>
When clicking this button, the called method will set to null all the fields and your dialog box will be empty. Getting back to your question of why process=#this is neccessary much better explained answer is here
What is the function of #this exactly?
You can also reset input after submitting through this method:
<p:commandButton value="Reset Non-Ajax"
actionListener="#{pprBean.reset}" immediate="true" ajax="false">
<p:resetInput target="panel" />
</p:commandButton>
If you don't add process="#this" then by default attribute value will be set to process="#form" which means all the elements in the form are processed. In command buttons process="#this" is mandatory to execute the corresponding actions associated with that button.
You can directly refer the answer from Balusc in this link
What is the function of #this exactly?

Resources