Grails form submit without changing the view - grails

I want to call an action (it does some filtering and send mails) from my GSP but I want to stay on that GSP. How to acoplish this? Thanks.

You have to use Ajax to submit your form.
Use the formRemote tag to achieve an Ajax submit.
Alternatively you could build it on your own by hooking into the onSubmit event of you form.

If you want to do this with a form, you can take a look on 'submitToRemote' tag: submitToRemote
but if you want to do it with a hyperlink... take a look on 'remoteLink' tag: remoteLink

Related

How to make form elements read only in GSP?

Hi i have a form in GSP and I want to make all the form elements read only after submit.
Is there any way to do it. I have form elements like textboxes, dropdowns attachment field......
I am using G:Form
I am also using java script in my GSP.
Please help me out
Thanks.
Keep in mind that even if you set the tags as readonly on the server side, users can still change them through a variety of means, and whatever the value on the form is before it gets sent back to you.
You can use an onsubmit event in the form tag, calling a JavaScript function which will disable any form elements you want to affect. Since GSP is server pages, not the browser, it will not normally be able to help you in this respect.
Certainly the easiest way is client-side with jQuery:
$(function() {
$('input, select, textarea').attr('disabled', 'disabled');
});

Rails form with ajax and non-ajax buttons

I have a form_for an #object with two buttons.
While the first button renders the 'show action', the second button renders the same form again. So I'd like the latter to be ajax-handled.
Is it possible to have a non-ajax button and an ajax button in the same form or do I have to change strategy?
Maybe I need a form_for with 'remote: true' so that both the buttons are ajax but then, how would I manage the first button to render the proper 'show view'?
Or maybe the only real solution is to have two different forms?
Thank you.
You could try to hook onto the buttons onClick event, remove the data-remote="true" attribute, submit the form and add data-remote="true" again. I dont know if this is really the best way but it should work.
function sendWithoutAjax() {
$('my_form_id').removeAttr("data-remote");
$('my_form_id').submit();
$('my_form_id').data( "remote", "true" );
}
Something like this...
I think the easiest approach would be to use the jQuery Form plugin. Then you can just create the form to submit to your non-ajax action, and the ajax functionality will be attached to the submit button itself:
$(".ajax_submit_button").click(function() {
$(this).closest("form").ajaxSubmit({
// options go here
...
});
return false;
});

Ajax.BeginForm in ASP.MVC2

I'd like to capture submit action from "Ajax.BeginForm (...)" and asynchronously fetch some data from controler and put it into some div...
How can i do it?
I've tried
Ajax.BeginForm(..., new AjaxOption( UpdateTriggerId = "", ) but as i noticed it is used for online checking form or something like this...
How can i disable reload whole page while pressing "submit" ?? Mayby i have add something to controler?
Btw. What is better to use with forms (in AJAX context)? Pure Jquery or those Ajax.BeginForm?
You should be able to achieve what you need using Ajax.BeginForm and with a partial view...
http://davidhayden.com/blog/dave/archive/2009/05/19/ASPNETMVCAjaxBeginForm.aspx

How can i return a form using ajax into a view ASP.Net MVC

i just started building a small test app to help me learn MVC. i have a view that displays user comments. Under each comment i would like to have a reply action link. Clicking on the link should return a small form for adding a comment directly above the reply link.
What is the general approach for this? I'm imaging the form would be a partial view that i can somehow return using the reply link. Thanks for any help!
Using jQuery to retrieve and post the forms in partial views is how I would do it.
Just return partial view to be loaded by jQuery load:
$('#comment').load('/MyController/ActionReturnsPartial');
You should not have to retrieve anything from the server if the user is not providing any extra information along with the retrieval.
Instead of retrieving the form when the user clicks, just let the page render a form below each comment. Put the form in a div with style="display: none;". Then, when the user cliks the link, use jQuery to show the form. Something like
$('.commentlink').click(function(){
$(this).closest('div').find('.formdiv').show();
});
You may also be able to use jQuery's .toggle() method.

Grails: Updating multiple page elements with ajax

It seems that with g:formRemote and g:submitToRemote, I can only specify one update target div. What would be a viable strategy for updating multiple divs?
if you want to stick with using the g:formRemote tags to perform your ajax, it might not be possible. Why not write some jQuery, and roll a custom ajax update? it couldnt be easier!
You could instead of using update, use onSuccess and parse the response and update the elements you need to update like this:
<g:formRemote name='loginForm' url="[action:'login']"
onSuccess='loginOK(e)' >
<!-- form fields -->
</g:formRemote>
<g:javascript>
function loginOK( resp ) {
// parse the resp.responseText and update
}
</g:javascript>
Or you could just use jQuery and roll your own like the previous answer suggests.
If you want to update one element based on success vs another on failure you can use a map like this:
<g:formRemote name='loginForm' url="[action:'login']" update="[success:'message',failure:'error']">
there is asolution to this grails problem, you can use taconite, where taconite allows to update muliple elements with one single ajax call
http://malsup.com/jquery/taconite/
and there is another solution posted at someone's blog, but my reputation does only allow me to post one link here on stackoverflow!!!
so i give the title of the blog post "Updating multiple page elements with Grails and Ajax"
From the grails doc for g:formremote, http://grails.org/doc/2.2.0/ref/Tags/formRemote.html , you can use the onSuccess callback to define a js function for a succesfull submit, so you can update all your target elements within that function, otherwise making an ajax call yourself also is a good option.

Resources