Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grails remote form, multiple submits, with javascript

I have a situation where I have a form with multiple submit buttons and I want to update a remote frame. I've tried using a g:formremote with 2 g:actionsubmit buttons (which support javascript) but the multiple submit buttons have a glitch (described here: http://www.grails.org/Ajax under "Multiple buttons with formRemote").

I took the workaround, using 2 g:submittoremote buttons, which work the way I expect, but doesn't accept the javascript parameters like onClick (the buttons in question are accept/reject and I want to put an AYS on the reject so it isn't used accidentally).

Is there a way for javascript and multiple submit buttons in a remote form to exist peacefully?

Thanks in advance for your help...

like image 211
pennstatephil Avatar asked Feb 27 '23 19:02

pennstatephil


2 Answers

Did you try the before parameter? It takes a JavaScript function, which will be executed before the remote function call. Just use it like this:

<g:submitToRemote value="Reject" update="feedback" 
                  controller="test" action="reject"
                  before="if (!confirm('sure?')) {return false;}" />

Whatever JavaScript you put in the before parameter will be inserted into the onclick attribute right before your Ajax updater call. This way you can easily do validation, get confirmations etc. and even break from onclick handling before submitting the Ajax call. There is a similar after parameter.

like image 99
hlg Avatar answered Mar 08 '23 12:03

hlg


Okay, I'm not saying this is beautiful, but I just messed around with this for a few minutes and have something that might work for you. Like I said... not the prettiest solution, but workarounds rarely are...

 <html>
<head>
<g:javascript library="prototype" />
</head>
<body>
<script type="text/JavaScript">
function setReject()
{
  document.getElementById('reject').value='true'
}
</script>

<g:formRemote name="test" update="updater" url="[ controller: 'date', action: 'test']" >
    <g:hiddenField name="reject" value="false" />
    <g:submitButton name="submit" value="something" onclick="setReject();return confirm('Are you sure???')" />
    <g:submitToRemote update="updater" action="otherTest" controller="date" value="Go Ahead"/>
</g:formRemote>


<div id="updater">

</div>
</body>
</html>
like image 38
proflux Avatar answered Mar 08 '23 14:03

proflux