# remoteField

## Purpose

Creates a text field that sends its value to a remote link when it changes. By default the parameter name sent is called 'value', this can be changed by specifying a 'paramName' attribute.

## Examples

Example controller for an application called "shop":

    class BookController {
        def changeTitle = {
           def b = Book.get(params.id)
           b.title = params.value
           b.save()
        }
    }

Example usages for above controller:

    <g:remoteField action="changeTitle" update="titleDiv"  name="title" value="${book?.title}"/><div id="titleDiv">I'm updated with the new title!</div>

## Description

Attributes

- `name` (required) - the name of the field
- `value` (optional) - The initial value of the field
- `paramName` (optional) - The name of the parameter send to the server
- `action` (optional) - the name of the action to use in the link, if not specified the default action will be linked
- `controller` (optional) - the name of the controller to use in the link, if not specified the current controller will be linked
- `id` (optional) - The id to use in the link
- `update` (optional) - Either a map containing the elements to update for 'success' or 'failure' states, or a string with the element to update in which cause failure events would be ignored
- `before` (optional) - The javascript function to call before the remote function call
- `after` (optional) - The javascript function to call after the remote function call
- `asynchronous` (optional) - Whether to do the call asynchronously or not (defaults to true)
- `method` (optional) - The method to use the execute the call (defaults to "post")

Events

- `onSuccess` (optional) - The javascript function to call if successful
- `onFailure` (optional) - The javascript function to call if the call failed
- `on_ERROR_CODE` (optional) - The javascript function to call to handle specified error codes (eg on404="alert('not found!')")
- `onUninitialized` (optional) - The javascript function to call the a ajax engine failed to initialise
- `onLoading` (optional) - The javascript function to call when the remote function is loading the response
- `onLoaded` (optional) - The javascript function to call when the remote function is completed loading the response
- `onComplete` (optional) - The javascript function to call when the remote function is complete, including any updates

## Source

Show Source

    def remoteField = { attrs, body ->
            def paramName = attrs.paramName ? attrs.remove('paramName') : 'value'
            def value = attrs.remove('value')
            if (!value) value = ''        out << "<input type=\"text\" name=\"${attrs.remove('name')}\" value=\"${value}\" onkeyup=\""        if (attrs.params) {
                if (attrs.params instanceof Map) {
                    attrs.params[paramName] = new JavascriptValue('this.value')
                }
                else {
                    attrs.params += "+'${paramName}='+this.value"
                }
            }
            else {
                attrs.params = "'${paramName}='+this.value"
            }
            out << remoteFunction(attrs)
            attrs.remove('params')
            out << "\""
            attrs.remove('url')
            attrs.each { k,v->
                out << " $k=\"$v\""
            }
            out <<" />"
        }