In my view I have a disabled p:inputText
showing some adress information. The value of the p:inputText
is set by the dialogReturn
listener onAdresseDialogReturn()
. Besides this inputText I have several other input fields (p:selectOneMenu
, p:inputText
).
Whenever I change some values of the other input components and try to leave the page, I get a warning dialog from the detectUnsavedChanges
JavaScript.
My problem now is, that I want this warning message also, when the dialogReturn
listener changes the value of the inputText field - but this does not happen!
So my question is, how can I achieve this?
Here the p:inputText
with a p:commandButton
using the primefaces dialog framework:
<ui:composition>
<ui:define name="head">
<h:outputScript name="js/detectUnsavedChanges.js"/>
</ui:define>
<ui:define name="content">
<h:form id="form">
<p:panelGrid>
<p:row>
<p:column>
<p:commandButton id="save" value="Save" ajax="false" action="#{bean.save}"/>
</p:columns>
</p:row>
</p:panelGrid>
<p:panelGrid>
<p:row>
<p:column>
<p:inputText id="adresse" disabled="true" size="50"
value="#{bean.adresse}"/>
<p:commandButton id="adresseAuswahl"
title="Adresse bearbeiten"
process="@this"
actionListener="#{bean.showAdresseDialog}">
<p:ajax event="dialogReturn" update="adresse"
listener="#{bean.onAdresseDialogReturn}"/>
</p:commandButton>
</p:columns>
</p:row>
</p:panelGrid>
</h:form>
</ui:define>
</ui:composition>
Here the dialogReturn
listener in my bean:
public void onAdresseDialogReturn(SelectEvent e) {
final Adresse adresse = (Adresse) e.getObject();
boolean changedAdresse = Optional.ofNullable(adresse).isPresent()
&& !getAdresse().equals(adresse);
if (changedBauvorhabenAdresse) {
// Adresse has been changed
setAdresse(adresse);
}
}
Here the JavaScript code as mentioned by BalusC from How to detect unsaved data in form when user leaves the page?:
$(function() {
// Set the unload message whenever any input element get changed.
$(':input').on('change', function() {
setConfirmUnload(true);
});
// Turn off the unload message whenever a form get submitted properly.
$('form').on('submit', function() {
setConfirmUnload(false);
});
});
function setConfirmUnload(on) {
var message = "You have unsaved data. Are you sure to leave the page?";
window.onbeforeunload = (on) ? function() { return message; } : null;
}
My Environment: Primefaces 6.0 on Wildfly-10.0.0.Final
Any hints welcome - Thank you!