我如何从JavaScript和Grails访问变量? [英] How I access a variable from JavaScript and Grails?
问题描述
我有一个 Grails 变量,它的类型为JASONList,它被渲染一个模板。
有没有一种方法可以从JavaScript函数中访问这个列表?
假设我想让onresize适合屏幕上的所有对象。没有进行数据库调用并从Ajax中重新整理整个列表...
让我们说模板做了这样的事情:
< g:每个var =reportin =$ {reportList?.myArrayList}>
< li style =display:inline; list-style:none;>
< img src =$ {report?.img}>
< / li>
< / g:每个>
< script type =text / javascript>
function resize(list){
if(list.size< givenSize)//伪代码
list.subList()//伪代码
}
window.onresize = resize($ {reportList})
< / script>
这个问题在于,由于某些原因,Grails gsp不会将$ {reportList}渲染为一个列表。相反,它将它呈现为字符串$ {reportList}。
我可能认为这个问题完全错误,但是有没有办法调整这些对象的大小或获取他们通过document.getElementById或那种性质?
$ reportList由 POJO 为JSON转换...
我想出了我的问题。基本上,如果你在Grails中使用POJO,那么Grails作为JSON转换不是很聪明。它所做的只是对对象的toString,而不是可能查看所有公共访问器等。
这有点令人失望,但基本上我需要创建JSON在我的POJO的toString方法中进行转换。
I have a Grails variable which is of type JASONList that is rendered in a template.
Is there a way to access this list from inside a JavaScript function?
Let's say I want onresize to fit all the objects on the screen. Without making a database call and refetching the entire list from Ajax...
Let's say the template does something like this:
<g:each var="report" in="${reportList?.myArrayList}">
<li style="display:inline; list-style:none;">
<img src=" ${report?.img}">
</li>
</g:each>
<script type="text/javascript">
function resize(list) {
if (list.size <givenSize) // Pseudocode
list.subList() // Pseudocode
}
window.onresize = resize("${reportList}")
</script>
The problem with this is that for some reason Grails gsp does not render "${reportList}" as a list. Instead it renders it as the string "${reportList}".
I am probably thinking of this problem completely wrong, but is there a way to resize these objects or get them through document.getElementById or something of that nature?
The $reportList is populated by POJO as JSON conversion...
I figured out my problem. Basically if you are using POJO in Grails the Grails as JSON conversion is not very smart. All it does is a toString on the object instead of potentially looking at all the public accessors, etc.
It is kind of disappointing, but basically I need to create the JSON conversion in the toString method of my POJO.
这篇关于我如何从JavaScript和Grails访问变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!