Grails: Updating multiple page elements with ajax - grails

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.

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');
});

Ways for AJAX / remoteFunction to update two or more unrelated divs (on success)?

The Grails current remoteFunction can only update one div on success (and one on error). There are times where I want to update multiple divs (on success) using a single Ajax call. How can one do this, other than to chain the remoteFunctions together (i.e. where the first one calls a Javascript function on success, which invokes the second remote function). ... Is there a way to update multiple parts of a page via ajax without this type of chaining?
Thanks
You can do this by getting your onSuccess function in javascript to set the html of the resulting divs. Here is a good example of how to do this - http://www.webdeveloperjuice.com/2010/04/01/filling-multiples-divs-by-single-ajax-call-using-jquery/
Cannot be done automatically.
You could call two remote functions separated by semi colon.
<g:select id='releaseVersion' name="releaseVersion" noSelection="['0':'--Select--']" from='${new Alarm().constraints.releaseVersion.inList}' onchange="${remoteFunction(controller:'alarm', action:'updateComponentByRelease',params:'\'selectedValue=\' + this.value', update:'component')};
${remoteFunction(controller:'alarm',action:'updateDiscriminatorByComponent', params:'\'selectedValue=\' + this.value',update:'subsystem')};"></g:select>
There are events you can set for when the AJAX request finishes. For example:
<g:remoteLink action="show"
id="1"
update="success"
onLoading="showProgress()"
onComplete="updateContent()">Show Book 1</g:remoteLink>
Will trigger function updateContent when it completes http://grails.org/doc/2.0.x/guide/single.html#ajax
It doesn't look like there's a way to automatically update two divs. The only possible way it could do it is by updating both with the same content.

What is a good way of tracking link clicks in Asp.Net Mvc app?

I have an MVC app with several <a> tags, and I want to have a way of tracking link clicks, preferably without doing redirects (i.e., without changing link addresses). Is there any way this can be done? And, if not, is there any way one can get this functionality automatically (e.g., with javascript)?
Thanks.
UPDATE
Taking your other wishes in consideration, this should be your way to go:
<script type="text/javascript">
$(document).ready(function() { //On document ready
$('a').click(function(e) { //For each a (anchor), set the following function
urchinTracker (this.href);
pageTracker._trackPageview (this.href);
//Custom tracker here?
return true; //To keep following href definitions
});
});
</script>
Consider Woopra
Since you are already using Google Analytics, they have a javascript function to put in the onclick event.
The method name is either urchinTracker or pageTracker._trackPageview depending on whether you are using urchin.js or ga.js as your Analytics script include.
An example (from here) in both formats:
<a href="/files/map.pdf" onClick="javascript:urchinTracker ('/downloads/map');">
<a href="/files/map.pdf" onClick="javascript:pageTracker._trackPageview ('/downloads/map');">
Basically what that does is call the javascript method (which does the 'analytics call' to Google's server) then does the <a> action as normal (ie. whatever the link was supposed to do - in this case open PDF files).
Is that what you are looking for - or is the jQuery answer what you wanted?
EDIT: in response to your question, this post contains a non-jQuery javascript include (here) that looks for 'downloadable files' and adds tracking. If you want to use jQuery, check out the other answer by ropstah since he/she has updated their answer to be Google Analytics-specific.
Consider using Google Analytics: you can't do any better.

RJS method outputting raw javascript

I've got a page where I'm dynamically adding elements to a from a series of select boxes. I'm using RJS to do this and it's working great. Now, these elements in the div are a series of that are accompanied by Delete buttons, in case the user wants to remove a textarea. Here's where I'm hitting a wall.
Here's the code that runs the Delete button. This is working well to my knowledge:
<%= link_to image_tag("/images/button_delete.gif", :alt=>"Delete"), :controller=>"report", :action=>"remove", :id=>#sentence.id %>
In my report controller, I've got this very simple method being called by the above code:
def remove
#sentence_id = params[:id]
end
Again, I think that's working. Now, when I activate this action by hitting the button, off we go to the RJS file, remove.rjs:
page.remove 'sentence_'+#sentence_id
And here's what I get in my browser, instead of a happily removed element!
try
{
Element.remove("sentence_63");
}
catch (e)
{
alert('RJS error:\n\n' + e.toString());
alert('Element.remove(\"sentence_63\");');
throw e;
}
My understanding is that this happens when there's no page in the current context. Online documentation on this is kind of thin.
Any help appreciated!
Cheers,
Aaron.
Try link to remote. That will build the ajax call for you and should remove the element from the page.
Then link_to_remote syntax is slightly different than the link_to syntax, so don't let that trip you up either.
Since your remove function doesn't seem to actually delete a record, if you just want to remove an HTML element from a page you can use link_to_function with the Prototype remove() method for Elements. In addition, if you've got a recent version of Rails (for example, 2.3.2) you can take advantage of the dom_id helper to auto generate the sentance_id ID attribute
<%= link_to_function(image_tag("button_delete.gif", :alt=>"Delete"), "$('#{dom_id(#sentence}').remove();" %>
An approach like this could help keep the number of methods down in your controller (unless you intend on doing something else in the controller)
Your Delete link is setup as a normal link, i.e.
<a href="/report/remove" id="sentence_63">
<img src="/images/button_delete.gif" alt="Delete" />
</a>
which triggers a normal HTTP request. Since your intent is to trigger an AJAX request, try PJ Davis' recommendation and use link_to_remote

How do I submit a form with a link in ASP.Net MVC?

I have a HTML form, and I have a Controller Action that accepts the POST request. Everything works with a regular submit button, but I would like to submit the form with a link (<a>-tag) instead, to be able to further control the formatting. Is there any way of doing this nicely built into the ASP.NET MVC Framework, or should I write my own extension method? Is it even possible to do this without javascript (I will use AJAX in the future, but it has to work without).
Here is a complete example. Note that this particular example does something fairly important: it has a fallback for browsers with JavaScript disabled.
If javascript and jQuery is enabled this effectively replaces all submit-buttons with links:
$("input:submit").hide().each(function (index, Element) {
var elm = $(Element);
elm.after($("<a href=#>" + elm.val() + "</a>")
.click(function () { elm.click(); })
);
});
Based on post linked to in the accepted answer.
I'm not aware of a helper and as far as I know it is impossible to submit a form using an anchor tag without using javascript.
You cannot 'submit a form' using a link (<a> tag) without Javascript. The javascript is going to generate a standard POST request (same as clicking a submit form button) behind the scenes.
There are other workarounds for those with JS disabled, look at what #Craig Stuntz submitted.

Resources