我是 grails 新手,如果有人能帮助我就好了。
我有一个 js 函数,在 Controller 中执行一些操作后,我会在 Controller 中调用该函数来打开弹出窗口。
我有这个 gsp 文件:
<g:form controller="admin" method="post">
<table summary="generer PDF">
..
..
<td>
<g:actionSubmit value="generate" action="genererAction"/>
</td>
</tr>
</table>
</g:form>
当用户单击“生成”(按钮)时,必须调用此函数:
def generateAction = {
def result
...
if (result){
//here, if result=true a popup will be displayed
}
else {
redirect(action: 'index')
}}
我希望你能帮助我。谢谢。
最佳答案
你可以使用ajax来做到这一点,这是一个例子:
查看:
<g:form controller="admin" method="post">
<table id="tableID" summary="generer PDF">
..
..
<td>
<!--<g:actionSubmit value="generate" action="genererAction"/>-->
<button onclick="preformAction(); return false;">generate</button>
</td>
</tr>
</table>
</g:form>
Controller :
def generateAction = {
def result
...
if (result){
//here, if result=true a popup will be displayed
render "success"
}
else {
//redirect(action: 'index')
render "index"
}}
Script ://将此脚本复制到您的 View 中
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
function preformAction(){
$.ajax({
url:'${createLink(controller:"admin",action:"generateAction")}', // here you can pass the controller url
data: {
// data to be passed to controller
},
success : function(response){
// this block is called after successful processing of controller
// here you can call your js function after controller has finished its processing
// consider response has 'success'
if(response == 'success'){
OpenSAGPopup();
} else if(response == 'index'){
window.location = '${createLink(controller:"admin",action:"index")}';
}
},
error : function(response){
// this block executes if any error occurs with the processing of controller
alert("Error while generating pdf");
},
});
}
</script>
试试这个,如果有效请告诉我......
祝你编码愉快。
关于javascript - 如何从我的 Controller grails 调用 js 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31696822/