Search code examples
javascriptgrailsgrails-2.0parameters

How can I pass JavaScript variable to Grails controller using remoteFunction


I am working with Grails 2.1.1. I have a remote function from where I want to pass more than one JavaScript variable through g:remoteFunction. But no JS variable is going to controller. My JS variables are underlined and are not recognized.

Here are my attempts:

function addNewBill(){
    var orgId = $('#aaOrgIdAaOrg').val();
    var ccId = $('#costCenter').val();
    if(orgId != null && orgId != "null" && orgId != "" && ccId != null && ccId != "null" && ccId != ""){
        $('#ccModal').modal('hide');
        ${remoteFunction(action: 'createBill', params: [purchaseOrderId: purPoMstInstance.id, aaOrgIdAaOrg: orgId, costCenter: ccId], update: 'bill')}            
    }
}

What can I try next?


Solution

  • As remoteFunction finally renders a jQuery function, you can use the params option, to include your variables using jQuery selectors or the Js vars or functions you need.

    Here is an example. As you can see, the params value is just an string, but after renderer, it will be run cause after that is js ;)

    <button type="button" onclick="${remoteFunction(controller: 'category', action: 'save', update: "eventsDiv",
       params: '\'type=\' + myJSvar)}">
             ${message(code: 'default.button.save.label')}
    </button>