Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call remoteForm to update only one div with onChange event

I want to update div contents upon entering data in a textField dynamically. I have tried the following:

<g:formRemote name="changeText" on404="alert('not found!')" update="resultDiv"
    url="[ controller: 'results', action:'results' ]">
        <g:textField onChange="submit();" name="text"/>
</g:formRemote>

Everything work except a new page is render and the div is not updated...

def results = {
   render "new content"
}
like image 336
Jacob Avatar asked Nov 20 '13 10:11

Jacob


Video Answer


2 Answers

The following GSP code:

<g:formRemote name="changeText" on404="alert('not found!')" update="resultDiv"
          url="[ controller: 'results', action:'results' ]">

Will generate a new HTML form with an onSubmit method like that:

<form id="form_id" action="/action" method="post" onsubmit="jQuery.ajax({type:'POST',data:jQuery(this).serialize(), url:'/action',success:function(data,textStatus){jQuery('#resultDiv').html(data);},error:function(XMLHttpRequest,textStatus,errorThrown){}});return false">

So the Javascript code that will trigger the AJAX call is on the onSubmit() method.

The problem here is that you are calling submit() action using Javascript and this doesn't trigger the onSubmit() method.

A solution would be to call the onSubmit() method directly in the onChange event:

<g:textField onChange="this.form_id.onsubmit();" name="text"/>
like image 194
Benoit Wickramarachi Avatar answered Oct 09 '22 21:10

Benoit Wickramarachi


Grails provides an awesome tag for this: remoteField

<g:remoteField name="input" controller="results" action="results" onSuccess="jQuery('#resultDiv').html(data)"/>

<div id="resultDiv"></div>
like image 1
MKB Avatar answered Oct 09 '22 21:10

MKB