English 中文(简体)
In Grails, how to invoke a controller action from a g:select
原标题:

I m using a g:select (actually, a g:currencySelect) in my view.

I want a controller action to fire as soon as the user changes the value in the resulting select box.

How can I do this?

最佳回答

I think I m answering my own question here, but do let me know if there s a better way of doing this:

Use the onchange javascript function in the select tag:

<g:form controller="changeCurrency" action="changeCurrency">
    <g:select onchange="submit()"/>
</g:form>
问题回答

Another way can be:


    <g:select name="someName" from="${list}" onchange="goToPage(this.value)"/>

    <script type="text/javascript">

    function goToPage(requestParams){
    window.location.href="${createLink(controller: controllerName  ,action: actionName  ,params:[paramsName:""])}" + requestParams;
    }

    </script>
    

In case you want an ajax request, try this:

<g:select name="type" from="${[ import-a ,  import-b ]}"
onchange="jQuery( #addArea ).load( /app/import/chooseImportType/  +
jQuery( #type ).val())"/>

In controller:

def chooseImportType (){
 render params.id
}

Worked in Grails 2.0.4





相关问题
grails + gwt request handling via controllers

I am new to gwt. I am trying to integrate gwt+grails.Can anybody provide me a good example for handling the request using grails controllers and not a custom servlet.I will be really thankful if ...

Error loading the grails gwt module xml

I ve installed the plugin from this article by Peter http://www.cacoethes.co.uk/blog/groovyandgrails/the-command-pattern-w.... While compile time its not able to find the module file which is present ...

Sorting Objects Based on Custom Domain Class Methods

I have a domain class, in which I ve defined some methods which give the object a score based on different algorithms (eg. popularity). I now want to retrieve a list of these objects sorted by one of ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

热门标签