I need to detect unsaved data in form when user leaves the page without submitting the form. I would like to implement that without adding a value change listener to each input.
This is the functional requirement:
"User open a page than click on any link if values in the page changed an alert message popup to notify user that he need to save changed data, but if did not change any thing system continue without notify user".
I tried to compare array method to compare both DTO coming for DB and the bind DTO, but it gives me a lot of problems in array length, and byte comparison.
thisPage. closeEditorWarning = function (event) { if (window. thisPage. isDirty) return 'It looks like you have been editing something' + ' - if you leave before saving, then your changes will be lost.
1. You can use vue lifecycle method - "beforeDestroy " hook as well and add a popup/dailog box before leaving page. 2 . You can use Vue-router BeforerouteLeave or use watch property on path.
If the user doesn’t change anything, meaning the form data is identical to the data received from the server, the save button should not be displayed. If the user changes the data and leave the page without saving, a pop-up window is displayed, warning that there is unsaved data on the page, in order to prevent accidental loss of data.
When a user fills in many details in a form and then clicks back, or closes/refreshes the tab, we would like to warn the user that there is unsaved data on the page. We’d like the user to leave only if he agrees to the message below. The message will be looking like this: 1. When a user closes or refreshes the tab.
11-14-2018 09:08 PM The Unsaved property of the Edit form control determines if the Edit form control contains user changes that have not been saved. If the EditForm1.Unsaved formula return true, it means that your Edit form contains some changes have not be saved.
If the user changes the data and leave the page without saving, a pop-up window is displayed, warning that there is unsaved data on the page, in order to prevent accidental loss of data. Here’s an illustration of what we want to achieve:
This is normally implemented in the client side with help of JavaScript, because it's not nicely possible to intercept on beforeunload
event from the server side on when the enduser leaves the page.
Here's a concrete example with help of the JavaScript library jQuery (otherwise you would end up with 10 times as much code to make it properly crossbrowser compatible and work seamlessly together with ajax re-renders):
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(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;
}
</script>
Just paste this in your <h:head>
template or just put it in some script.js
file which you include by <h:outputScript>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With