简体   繁体   English

如何从GSP视图迭代Javascript中的Groovy集合

[英]How to iterate over a Groovy Collection in Javascript from GSP view

I'm trying to iterate over the collection "times" in the code below. 我正试图在下面的代码中迭代集合“times”。 It's a list of map objects, each map has a time, name and modified field. 它是一个地图对象列表,每个地图都有一个时间,名称和修改后的字段。

<r:script>
    <% def pos = 0%>
    for(var i = 0; i < ${jsonTimes.size()}; i++){
        console.log(${jsonTimes[pos].getAt("time")});
       <% pos++; %>
    }
</r:script>

either incrementing the "pos" variable as I am here, or jsonTimes[pos++] fails to increment the variable. 要么像我这里那样递增“pos”变量,要么jsonTimes [pos ++]无法增加变量。 Can I get some help with this please? 我能得到一些帮助吗? Thanks! 谢谢!

You're mixing groovy and js. 你正在混合groovy和js。 jsonTimes is a server side variable, so you should use groovy for iterations: jsonTimes是服务器端变量,因此您应该使用groovy进行迭代:

<g:each in="${jsontTimes}" var="jt">
  console.log(${jt.getAt("time").encodeAsJavaScript()});
</g:each>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM