Java Developer

Grails AJAX tutorial - remoteField tag

The remoteField tag is a simple tag used to render a text field that sends changes to it's value back to the server. Simple usage bellow:

EchoController.groovy

package tutorial
class EchoController {
    def index() {}
    def shout() {
        def message = params.value
        render "hello ${message}"
    }
}

index.gsp

<!DOCTYPE html>
<html>
	<head>
		<meta name="layout" content="main"/>
		<title>Simple Ajax</title>
        <g:javascript library="jquery"/>
        <style>
            #echoBox {
                border: 1px solid #000; width: 300px; height: 100px; background-color: #c0c0c0;
            }
        </style>
	</head>
	<body>
        <div id="echoBox">
        </div>
        <g:remoteField controller="echo" action="shout" update="echoBox"/>
	</body>
</html>

If you have read my previous posts on remoteLink and remoteFunction, the controller and action specifies which code to invoke in the back end. The update field specifies which HTML element to update.

The thing to note is that the value is stored inside params.value when you are inside the controller.

    def shout() {
        def message = params.value
        render "hello ${message}"
    }

You can checkout the source code for this example here or download the zip file here.
List of Grails AJAX Tutorials

Tags: ajax, grails, remoteField, tag