[英]Grails: GSP variable substitution in g:each tag
I have a loop inside a loop and want to substitute the value of first var into second. 我在一个循环中有一个循环,想将first var的值替换为second。 Below is the code snippet.
下面是代码片段。
<g:each in="${tables}" status="i" var="table">
<div class="tabletitle">
${table.name}
</div>
<table>
<thead>
<tr>
<g:each in="${${table.name}DisplayColumns}" status="k" var="displayColumn">
<td>${displayColumn}</td>
</g:each>
</tr>
</thead>
<tbody>
....
....
</tbody>
</table>
</g:each>
${table.name} substitution in second g:each tag is not working. 第二个g:each标签中的$ {table.name}替换不起作用。 Any idea to make it work?
有什么想法可以使它起作用吗?
尝试这个:
<g:each in="${evaluate(table.name+'DisplayColumns')}" status="k" var="displayColumn">
Interesting, I've never used evaluate inside a gsp, as Kelly suggests. 有趣的是,我从来没有像Kelly建议的那样在gsp内使用评估。 But may I suggest a less optimal approach?
但是我可以建议一种不太理想的方法吗?
You can store ${table.name}
inside a variable with <g:set>
( http://grails.org/doc/2.0.x/ref/Tags/set.html ) 您可以使用
<g:set>
( http://grails.org/doc/2.0.x/ref/Tags/set.html )将${table.name}
存储在变量中。
Do you know that you can pass any object to a GSP? 您知道您可以将任何对象传递给GSP吗? Even maps (you're trying to emulate maps, I don't know why), and use it like:
甚至地图(您正在尝试模拟地图,我也不知道为什么),并像这样使用它:
<g:each in="${displayColumns[table.name]}">
where displayColumns is a Map that contains columns for each table. 其中displayColumns是一个Map,其中包含每个表的列。
Btw, more clean way, is to use special object, that includes this data. 顺便说一句,更干净的方法是使用包含此数据的特殊对象。 Something like
TableDetails
that have List<String> getColumns()
method. 具有
TableDetails
List<String> getColumns()
方法的TableDetails
东西。 So you can use 所以你可以使用
<g:each in="${tables}" var="table">
${table.name}
<g:each in="${table.columns}" var="column">
${column}
</g:each>
</g:each>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.