带有片段参数的Grails render() [英] Grails render() with a fragment parameter
问题描述
有没有一种方法可以将render()与片段参数一起使用,以便在页面加载时自动将其滚动到页面的特定部分?类似于我们的呼叫方式
Is there a way to use render() with a fragment param so on page load it automatically scrolls to a specific part of the page? Similarly to how we can call
redirect(controller: "book", action: "show", fragment: "profile")
推荐答案
无法通过grails渲染调用直接指定片段,但是在我的代码中,我正在使用一种解决方法,它似乎提供了大多数所需的功能,仅需一点额外的复杂性.诀窍是将所需的片段引用作为模型的一部分传递,然后在GSP页面中对该引用进行操作.因此,控制器中的render调用如下所示:
There is no way to specify a fragment directly with a grails render call, but in my code I'm using a work around that seems to provide most of the desired functionality with only a little extra complexity. The trick is to pass in the desired fragment reference as a part of the model, and then to act upon that reference in the GSP page. Thus, the render call in my controller looks like this:
def foo() {
render(view : 'foo', model:[fragment:'myFragment'])
}
然后,在GSP中,我使用以下Javascript访问该模型:
then, down in the GSP, I access the model with the following Javascript:
<g:javascript>
if ("${fragment}") {
window.location.href = "#${fragment}";
}
</g:javascript>
然后,GSP会告诉您的浏览器向前跳转到页面中所需的锚点(如果有).
The GSP will then tell your browser to jump forward to the desired anchor in the page (if any).
这篇关于带有片段参数的Grails render()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!