通过g:formRemote的onSuccess将模型传递给Javascript函数



我想在模板渲染后通过"onload()"函数调用JS函数,或者通过g:formRemote标记中的"onSuccess()"回调调用JS函数。在我的主要GSP中,我有以下内容。此代码对控制器进行回调,并设置要传递到模板中的模型。

GSP:
<g:formRemote name="ptoReport" url="[controller:'Report', action:'ptoBarChart']" update="barChartDiv">
    <g:submitButton name="pto" value="PTO" class="btn btn-primary"/>
</g:formRemote>
<div id="barChartDiv">
    <g:render template="barChart"/>
</div> 

我的控制器代码看起来是这样的:

Controller:
def ptoBarChart(){
    Map usersAndPTO = timesheetService.getPTOForAllUsers()
    render(template: 'barChart', model:[points: usersAndPTO])
}

我想在加载模板后立即调用JS函数,但诀窍是将"usersAndPTO"映射传递到JS函数中。我曾尝试将"onLoad="barChart(${points})"添加到渲染模板中,但从未调用过。将"onSuccess="barChart(data)"添加到g:formRemote会导致我将模板的GSP代码传递给JS函数。

非常感谢您的帮助。

检查一下:

在控制器中,我也会以json的形式返回点,如果您只需要js将其呈现为json

render(template: 'barChart', model:[points: usersAndPTO,jsonPoints: usersAndPTO as JSON])

这是你的模板_barChart.gsp的底部

...
<g:if test="${jsonPoints}">
<script>
alert("${jsonPoints}");
// you can also pass them to a function from original dom, its available cause this template now is part of original dom
myFreakyFunction("${jsonPoints}");
</script>
</g:if>

当模板加载完成时,它应该发出警报!因此

最新更新