Process onclick function after ajax call <f:ajax> - ajax

I'm trying to select and focus to choosed component ID after submit a form (ajax call).
<script>
var myFunc = function() {
document.getElementById('form:#{bean.componentId}').focus();
document.getElementById('form:#{bean.componentId}').select();
};
$(document).ready(function() {
myFunc();
});
</script>
<h:form id="form">
<h:commandButton action="#{bean.save}" onclick="return myFunc();" ...>
<f:ajax execute="#form" render="#form"/>
</h:commandButton>
...
</h:form>
This solution is working, but problem is, that <f:ajax> is called AFTER onclick, so the the form is rendered after component selection, and focus is cleared.
How can I call my function AFTER the form is rendered?
update: (I've tried for example)
add onevent="myFunc();" to f:ajax => leads to refreshing page
add onevent="myFunc()" to f:ajax => same behaviour as onclick attribute
next f:ajax with onevent attr. => still the same
update2 (how it should works):
submit button is ajax called
form is cleaned as needed
appropriate field is focused (depended on some user choosed factors)

The onevent handler will actually be invoked three times and it should point to a function name, not the function itself. One time before the ajax request is been sent, one time after the ajax response is been arrived and one time when the HTML DOM is successfully updated. You should be checking the status property of the given data argument for that.
function listener(data) {
var status = data.status; // Can be "begin", "complete" or "success".
switch (status) {
case "begin": // Before the ajax request is sent.
// ...
break;
case "complete": // After the ajax response is arrived.
// ...
break;
case "success": // After update of HTML DOM based on ajax response..
// ...
break;
}
}
In your particular case, you thus just need to add a check if the status is success.
function myFunc(data) {
if (data.status == "success") {
var element = document.getElementById('form:#{bean.componentId}');
element.focus();
element.select();
}
}
And you need to reference the function by its name:
<f:ajax ... onevent="myFunc" />

Related

TinyMCE's value in bean on second submit (using jsf.ajax.addOnEvent)

I have set up a basic testcase in which I'm experience some (to me) weird behaviour. When using the setup below, the typed value in the editor will only be visible by h:outputText on the second submit. E.g.
Change value in editor to "myvalue"
Send Ajax-request
h:outputText shows "test" (default value from bean)
Change value in editor to "anothervalue"
Send Ajax-request
h:outputText shows "myvalue"
Send Ajax-request
h:outputText shows "anothervalue"
Note: there's a custom composite, please ask for code if needed (it simply creates textarea for TinyMCE and loads .js file from below)
index.xhtml
<h:body>
<h:form>
<mh:editor id="tinymceEditor"
value="#{bean.value}" />
<h:commandButton value="Ajax">
<f:ajax execute="tinymceEditor"
render="show" />
</h:commandButton>
<h:outputText id="show" value="#{bean.value}" />
</h:form>
</h:body>
jsfhandler.js -> included on header in custom composite mh:editor
jsf.ajax.addOnEvent(function(data) {
switch(data.status) {
case "begin":
tinyMCE.execCommand('mceRemoveControl',true,"tinymceEditor");
tinyMCE.triggerSave();
break;
case "complete":
tinyMCE.execCommand('mceAddControl',true,"tinymceEditor");
break;
case "success":
break;
}
});
Bean.java
#Named
#RequestScoped
public class Bean {
private String value = "test";
}
The JSF ajax begin event is too late in order to take changes in form data into account. The ajax request is already prepared based on form data before this event.
Effectively, the sequence is as follows:
User enters input (and leaves field).
HTML DOM "change" event is triggered on input field.
User clicks submit button.
HTML DOM "click" event is triggered on submit button.
JSF prepares ajax request.
JSF ajax "begin" event is triggered on ajax request.
JSF sends ajax request.
...
Basically, you should be doing tinyMCE.triggerSave() during the HTML DOM "click" event.
<h:commandButton ... onclick="tinyMCE.triggerSave()">
Or, better, during the HTML DOM "change" event of the tinyMCE textarea.

Reload parts of a page before action is executed?

I have a h:commandButton calling some action="#{bean.do}". And also some ajax request for that button which should restyle a part of the website. This restyling should take place before the action is executed. How can I do this?
<h:commandButton action="#{bean.do}">
<f:ajax execute="#form" render="specificId" />
</h:commandButton>
As the action do sometimes takes a long time, I want the page first to display a message/image or likewise. And after that frist execute the action. But how?
You can use the onclick attribute to execute some JavaScript code right before the action is invoked.
E.g.:
<h:commandButton ... onclick="document.body.style.background='pink'">
It's however better to use onevent attribute of <f:ajax> for this, so that you can revert the change:
<h:commandButton ...>
<f:ajax ... onevent="handleDo" />
</h:commandButton>
<img id="progress" src="progress.gif" class="hidden" />
with e.g. this which disables/enables the button and displays/hides the progress image:
function handleDo(data) {
var status = data.status; // Can be 'begin', 'complete' and 'success'.
var button = data.source; // The HTML element which triggered the ajax.
var image = document.getElementById("progress"); // Ajax loading gif?
switch (status) {
case 'begin': // This is called right before ajax request is been sent.
button.disabled = true;
image.style.display = "block";
break;
case 'complete': // This is called right after ajax response is received.
image.style.display = "none";
break;
case 'success': // This is called when ajax response is successfully processed.
button.disabled = false;
break;
}
}

Why is a form's submit event not firing (jQuery)?

I have a form + layout like so:
<form ...>
<div id="editor">
[form html]
<input type="submit" value="Submit form" />
</div>
</form>
And the following javascript:
$(function() {
var form = $('#editor').parents('form');
alert(form.length); // this alerts "1"
$(document).on('submit', 'form', function() {
alert('document form submit fired'); // this works as expected (alerts)
});
form.on('submit', function() {
alert('selected form submit fired'); // this is never alerted
});
});
This form is not loaded via ajax. When the page loads, the first dialog alerts "1". However when submitting the form, only one alert is fired -- the one that triggers submit for all forms in the document.
Why would this happen?
It does work. Something else is happening which is preventing the second alert from firing.
Your form selector is incorrect.
Try and do this
$("form").on('submit', function() {
Pretty sure this should work
Actually if nothing is being loaded through ajax or dynamically through javascript
You can just do
$("form").submit(function() {
EDIT
Scratch my above. Didn't see you set the value of form. Check out http://jsfiddle.net/s3fvM/1/. Seems to be working fine to me. both are firing and alerting.

Keep p:dialog open when a validation error occurs after submit

Minimal example dialog:
<p:dialog header="Test Dialog"
widgetVar="testDialog">
<h:form>
<p:inputText value="#{mbean.someValue}"/>
<p:commandButton value="Save"
onsuccess="testDialog.hide()"
actionListener="#{mbean.saveMethod}"/>
</h:form>
</p:dialog>
What I want to be able to do is have the mbean.saveMethod somehow prevent the dialog from closing if there was some problem and only output a message through growl. This is a case where a validator won't help because there's no way to tell if someValue is valid until a save is submitted to a back end server. Currently I do this using the visible attribute and point it to a boolean field in mbean. That works but it makes the user interface slower because popping up or down the dialog requires hitting the server.
The onsuccess runs if ajax request itself was successful (i.e. there's no network error, uncaught exception, etc), not if action method was successfully invoked.
Given a <p:dialog widgetVar="yourWidgetVarName">, you could remove the onsuccess and replace it by PrimeFaces RequestContext#execute() inside saveMethod():
if (success) {
RequestContext.getCurrentInstance().execute("PF('yourWidgetVarName').hide()");
}
Note: PF() was introduced in PrimeFaces 4.0. In older PrimeFaces versions, you need yourWidgetVarName.hide() instead.
If you prefer to not clutter the controller with view-specific scripts, you could use oncomplete instead which offers an args object which has a boolean validationFailed property:
<p:commandButton ...
oncomplete="if (args && !args.validationFailed) PF('yourWidgetVarName').hide()" />
The if (args) check is necessary because it may be absent when an ajax error has occurred and thus cause a new JS error when you try to get validationFailed from it; the & instead of & is mandatory for the reason explained in this answer, refactor if necessary to a JS function which you invoke like oncomplete="hideDialogOnSuccess(args, 'yourWidgetVarName')" as shown in Keep <p:dialog> open when validation has failed.
If there is however no validation error and the action method is successfully triggered, and you would still like to keep the dialog open because of e.g. an exception in the service method call, then you can manually trigger validationFailed to true from inside backing bean action method by explicitly invoking FacesContext#validationFailed(). E.g.
FacesContext.getCurrentInstance().validationFailed();
Using the oncomplete attribute from your command button and really simple script will help you a lot.
Your dialog and command button would be something similar to this:
<p:dialog widgetVar="dialog">
<h:form id="dialogView">
<p:commandButton id="saveButton" icon="ui-icon-disk"
value="#{ui['action.save']}"
update=":dataList :dialogView"
actionListener="#{mbean.save()}"
oncomplete="handleDialogSubmit(xhr, status, args)" />
</h:form>
</p:dialog>
An the script would be something like this:
<script type="text/javascript">
function handleDialogSubmit(xhr, status, args) {
if (args.validationFailed) {
dialog.show();
} else {
dialog.hide();
}
}
</script>
I've just googled up this solution. Basically the idea is to use actionListener instead of button's action, and in backing bean you add callback parameter which will be then check in button's oncomplete method. Sample partial code:
JSF first:
<p:commandButton actionListener="#{myBean.doAction}"
oncomplete="if (!args.validationFailed && args.saved) schedulesDialog.hide();" />
Backing bean:
public void doAction(ActionEvent actionEvent) {
// do your stuff here...
if (ok) {
RequestContext.getCurrentInstance().addCallbackParam("saved", true);
} else {
RequestContext.getCurrentInstance().addCallbackParam("saved", false);
}
}
Hope this helps someone :)
I use this solution:
JSF code:
<p:dialog ... widgetVar="dlgModify" ... >
...
<p:commandButton value="Save" update="#form" actionListener="#{AdminMB.saveTable}" />
<p:commandButton value="Cancel" oncomplete="PF('dlgModify').hide();"/>
Backing bean code:
public void saveTable() {
RequestContext rc = RequestContext.getCurrentInstance();
rc.execute("PF('dlgModify').hide()");
}
I believe this is the cleanest solution.
Doing this you don't need to change your buttons code.
This solution overrides the hide function prototype.
$(document).ready(function() {
PrimeFaces.widget.Dialog.prototype.originalHide = PrimeFaces.widget.Dialog.prototype.hide; // keep a reference to the original hide()
PrimeFaces.widget.Dialog.prototype.hide = function() {
var ajaxResponseArgs = arguments.callee.caller.arguments[2]; // accesses oncomplete arguments
if (ajaxResponseArgs && ajaxResponseArgs.validationFailed) {
return; // on validation error, prevent closing
}
this.originalHide();
};
});
This way, you can keep your code like:
<p:commandButton value="Save" oncomplete="videoDetalheDialogJS.hide();"
actionListener="#{videoBean.saveVideo(video)}" />
The easiest solution is to not have any "widget.hide", neither in onclick, neither in oncomplete. Remove the hide functions and just put
visible="#{facesContext.validationFailed}"
for the dialog tag

Disable/enable commandbutton on ajax event

I want to be able to disable the commandbutton below once it's hit and enable it once the event listener runs and msg1 is rendered.
<h:commandButton value="Submit">
<f:ajax execute="#form" render="msg1" listener="{bean.method}" />
</h:commandButton>
How could I do this?
UPDATE: I found out that I can attach onclick event to the commandButton element itself to disable it. How can I detect the listener method has returned so I can enable the button again?
You could do it with help of the onevent attribute of <f:ajax> which points to a JavaScript function which handles the JSF Ajax events.
E.g.:
<h:commandButton value="Submit">
<f:ajax execute="#form" render="msg1" listener="#{bean.method}" onevent="handleDisableButton" />
</h:commandButton>
(note that I fixed the wrong EL in listener as well)
with this JS:
function handleDisableButton(data) {
var buttonElement = data.source; // The HTML DOM element which invoked the ajax event.
var ajaxStatus = data.status; // Can be "begin", "complete" and "success".
switch (ajaxStatus) {
case "begin": // This is called right before ajax request is been sent.
buttonElement.disabled = true;
break;
case "complete": // This is called right after ajax response is received.
// We don't want to enable it yet here, right?
break;
case "success": // This is called when ajax response is successfully processed.
buttonElement.disabled = false;
break;
}
}
If you use richfaces, the a4j:commandButton has the status attribute which prevent this.

Resources