简体   繁体   English

谷物嵌套g:每个

[英]Grails nested g:each

I am trying to loop through a list of fields in my index view. 我试图遍历索引视图中的字段列表。 Lets say I pass the following to index.gsp: 可以说我将以下内容传递给index.gsp:

[SomeDomainList: SomeDomain.list(), fields: ['field1', 'field2', 'field3']]

And then in index.gsp I have: 然后在index.gsp中,我有:

<g:each in="SomeDomainList" var="SomeDomainInstance">
    <g:each in="fields" var="field">
        <td>${SomeDomainInstance.field}</td>
    </g:each>
</g:each>

I would think that since the SomeDomainInstance variable gets evaluated that the field variable would get evaluated also. 我认为,由于对SomeDomainInstance变量进行了求值,因此该字段变量也将得到求值。 However, I get a "No such property: field for class: SomeDomain". 但是,我得到一个“没有这样的属性:类的字段:SomeDomain”。 I also tried: 我也尝试过:

<td>${SomeDomainInstance.${field}}</td>

But that results in a "Unclosed GSP expression" error. 但这会导致“未封闭的GSP表达式”错误。 Can anyone tell me how I can achieve this? 谁能告诉我如何实现这一目标?

Try adding double quotes to your example: 尝试在示例中添加双引号:

<td>${SomeDomainInstance."${field}"}</td>

You can just select the fields you're interested in using a criteria query and projections: 您可以使用条件查询和预测选择感兴趣的字段:

def domainList = SomeDomain.createCriteria().list {
    projections {
        property('field1')
        property('field2')
        property('field3')
    }
}

[SomeDomainList: domainList]

Or if you need the column names: 或者,如果您需要列名:

def domainList = SomeDomain.withCriteria{
    resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP)
    projections {
        property( 'field1', 'field1')
        property( 'field2', 'field2')
        property( 'field3', 'field3')
    }
}

[SomeDomainList: domainList]

Why people love GStrings so much? 人们为什么如此爱GStrings? A simple subscript operator would do: 一个简单的subscript operator可以做到:

<td>${SomeDomainInstance[ field ]}</td>

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

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