简体   繁体   中英

grails select with remoteFunction, can it update a g:textField?

I need to update the value of a textField, using a value on the server, based on the value the user chooses in ag:select. In code:

 <g:select name="description" from="${momentum.MoneyTransType.prodList}" value="${moneyInstance?.description}"
               noSelection="['':'-Select Description-']" onChange="${remoteFunction(action:'setHowMuch', update:[success:'howMuch', failure:'failure'],
               params:'\'selection=\' + this.value', options=[asynchronous:false])}"/>

 <g:textField id="howMuch" name="howMuch" value="${moneyInstance?.howMuch}"/>

It's not working. If I give the "update:[success:" a div id, all is good, but that's not what I want. I need to allow the user to enter in a free flow description (which I'll have in another textfield), and a free flow amount. I guess I could hide a div and then listen for changes to that div via jQuery, and then update the amount textField. Should I be able to update a textField using the remoteFunction "update" capability or using another grails capability?

Strangely, putting in a temporary 'toHide' div with a jQuery change function isn't working to update the textField, ie the following alert, etc, isn't firing:

        $('#toHide').change(function() {
            alert(" I got changed, value:");
            $("#howMuch").text($(this).val());
        });

Well, after writing all the below, I reread your question and see you stated you know it works with a div. So the rest of my answer might not be helpful, but what's wrong with using a div? An empty div won't display anything, so you don't need to hide it. So FWIW:

  1. Put your <g:textField ...> in a template.
  2. Add a div where where you want the template to be rendered. In other words, replace the current <g:textField ..> with <div id=updateme name=updateme></div>

  3. In your setHowMuch action, render the template.

For example, I do:

In view:

<g:select class='setTagtypeValue-class'
            name='tagtype-${i}-header'
            from="${org.maflt.ibidem.Tagtype.list(sort:'tagtype').groupBy{it.tagtype}.keySet()}"
            value="${setTagtypeValue?.tagtype?.tagtype}"
            valueMessagePrefix="tagtype"
            noSelection="${['null':'Select One...']}"
            onchange="${remoteFunction(action:'options', update:'tagtype-options-${i}',  
                    params:'\'tagtype=\' + this.value +\'&i=${i}\'' )}" />

Controller action:

def options = {
    def i = params.i ?: 0
    def tagtypes = Tagtype.findAllByTagtype(params.tagtype)

    render(template:"tagtypeList", model:[tagtypes:tagtypes,i:i])

}

tagypeList Template:

<table>
  <tr>
    <th></th>
    <th><g:message code="tagtype.lookup" 
                    default="Lookup Table" /></th>
    <th><g:message code="tagtype.regexpression" 
                    default="Field Rule" /></th>
    <th><g:message code="tagtype.uicomponent" 
                    default="UI Component" /></th>
  </tr>
  <g:each in="${tagtypes}" var="tagtype" status="j">
  <tr>
    <td><g:radio name="setTagtypesList[${i}].tagtype.id" value="${tagtype.id}" 
            checked="${(setTagtypeValue?.tagtype?.id == tagtype.id ||
                            (!setTagtypeValue?.tagtype?.id && j==0))}"></g:radio></td>
    <td>${tagtype.lookupTable}</td>
    <td>${tagtype.regexpression}</td>
    <td><g:message code="${'uicomponent.' + tagtype.uicomponent.id}" 
                    default="${tagtype.uicomponent.uicomponent}" />
    </td>                               
  </tr>
  </g:each>
</table>

This code is from the metadataset (called field set in the UI) screen in http://www.maflt.org/products/Ibidem .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM