How can I pass javascript variable to grails controller using remoteFunction

淺唱寂寞╮ 提交于 2019-12-06 15:06:01

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>

I believe the code is written in GSP which get complied and rendered as html so it cannot read the user changes on page. For example the element #aaOrgIdAaOrg is a drop down and you want to read the changed value of dropdown then compiled value of GSP tag cannot read this.

If the value of event doesn't change with user events and passed from action to in model then you can directly use the parameter in remote function.

In your case I think if will be better if your write your own ajax code rather then using remoteFunction. RemoteFunction tag is for basic usages and it is also deprecated in newer releases of Grails so if you use this tag you will need to remove it when you decide to migrate to newer version of Grails.

Hope it helps

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: '\'orgId=\' + orgId+\'&ccId=\' + ccId', update: 'bill')}            
}

}

You should be sending this to an api endpoint on the backend.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!